You need to enable JavaScript to run this app. 基于@expo/next-adapter的NextJS用户页刷新时水合失效问题
优惠活动
大模型
产品
解决方案
定价
更多

基于@expo/next-adapter的NextJS用户页刷新时水合失效问题

项目基本情况
  • 使用@expo/next-adapter开发Next.js博客站点,共包含两个页面:user页、post页
  • 页面实现逻辑:
    • user页接入getServerSideProps,用于从Firebase获取数据
    • post页通过next/link提供的<Link />组件实现导航,跳转目标为/user/[uid]路径
遇到的问题
  • 从post页通过<Link />组件导航到user页时,所有功能可正常运行
  • 在user页执行刷新操作时,页面会进入无响应状态,所有和样式渲染相关的客户端JS都无法执行
  • 补充说明:post页面未实现SSR逻辑,本人为开发新手,若问题属于新手常犯的错误还请见谅
部署环境差异
  • 项目托管在Firebase平台,通过云函数处理SSR逻辑
  • 本地执行生产构建命令next build && next start运行项目时,上述问题不会复现

提前感谢大家的帮助...

控制台报错信息

手动访问/user/[uid]路径,或是从post页导航到user页后刷新页面时,浏览器控制台会抛出如下错误:

main-590e886005382c6f8e8a.js:1 Uncaught TypeError: Cannot read property 'map' of undefined
at DqTX.t.default (main-590e886005382c6f8e8a.js:1)
at Object.IKlv (main-590e886005382c6f8e8a.js:1)
at l (webpack-288ceb4b356766e18474.js:1)
at Object.BMP1 (main-590e886005382c6f8e8a.js:1)
at l (webpack-288ceb4b356766e18474.js:1)
at t (webpack-288ceb4b356766e18474.js:1)
at Array.r [as push] (webpack-288ceb4b356766e18474.js:1)
at 0c947092cfe8a35964e0670062fe3410218106ea.62154c5756c292e8972d.js:1
问题相关代码

对应[user].js文件中的getServerSideProps逻辑如下:

export async function getServerSideProps(context) {
  if(admin.apps.length == 0){
    admin.initializeApp({
        credential: admin.credential.cert(serviceAccount),
        storageBucket: '*************.appspot.com',
    });
  }
  try {
    const fireStore = admin.firestore();
    const userDataRef = await fireStore.collection('users').where('uid', '==', `${context.query.user}`).get();
    const postDataRef = await fireStore.collection('posts').where('uid', '==', `${context.query.user}`).get();
    let userData;
    let postData = [];
    if(!userDataRef.empty){
        if(userDataRef.docs[0].exists){
            userData = userDataRef.docs[0].data()
        }
        if(!postDataRef.empty){
            for(let post of postDataRef.docs){
                const thumbImage = await admin.storage().bucket().file(`postData/${post.id}/${post.data().thumb}`).publicUrl();
                deckData.push(JSON.parse(JSON.stringify({...post.data(), id: post.id, thumb: thumbImage})));
            }
        }
    }
    return {
      props: {user: JSON.parse(JSON.stringify(userData)), posts: [...postData]},
    }
  } catch(e) {
    return {
        props: {user: 'error', posts: []},
    }
  }
}

内容的提问来源于stack exchange,提问作者Ravin Hewage

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.30 12:36:04