gpt4 book ai didi

Next.js slow on page transition(Next.js页面转换速度慢)

转载 作者:bug小助手 更新时间:2023-10-28 10:57:57 25 4
gpt4 key购买 nike



I am having problem with Next.js being too slow when going from "/posts" page to "/post/[postId]" and then go back to "/posts".

当我从“/post”页面转到“/post/[postID]”然后再回到“/post”时,我遇到了Next.js速度太慢的问题。


As more posts gets loaded into cache going back to "/posts" Next.js waits until every posts gets rendered then goes back (I found out this by adding console.log) in one of the components and when I press back in "/post/[postId]" page I see the console.log running and when all of the 100 (for example) posts gets console logged then the page transitions which is very slow.

随着更多的帖子被加载到缓存中,返回到“/post”,Next.js等待,直到每个帖子都被呈现,然后在其中一个组件中返回(我通过添加console.log发现了这一点),当我返回到“/post/[postID]”页面时,我看到console.log正在运行,当所有100个(例如)帖子都被控制台记录时,页面转换非常慢。


Compare this with Twitter's timeline and going back and forth for a specific tweet page it happens almost instantly, so in twitter the page immediately goes back and then tweets gets rendered (at least feels like this way).

与Twitter的时间线相比,对于特定的tweet页面,它几乎是立即发生的,所以在Twitter中,页面立即返回,然后tweet被呈现(至少感觉是这样的)。


I am fetching the posts on the client so the page is static with no getServerSide or getStaticProps or anything else.

我在客户机上获取帖子,所以页面是静态的,没有getServerSide、getStaticProps或其他任何东西。


Is it because twitter uses SPA React-Router? How to solve this issue in Next.js? I am sure other big sites have solved this issue but how?

是因为Twitter使用了SPA反应路由器吗?如何在Next.js中解决这个问题?我相信其他大型网站已经解决了这个问题,但如何解决呢?


Note that I am in production mode (not dev) and using "/pages" directory.

请注意,我处于生产模式(不是dev)并使用“/Pages”目录。


Maybe this is similar but not answering my question:
NextJs router seems very slow compare to React Router

也许这很相似,但没有回答我的问题:与Reaction路由器相比,NextJs路由器似乎非常慢


更多回答
优秀答案推荐

I solved it via Delayed component and now router.back happens instantly but you will lose the SSR for SEO. (Google says that its bots will search dynamically generated content but who knows)

我通过延迟组件和现在的路由器解决了它。返回立即发生,但你会失去搜索引擎优化的SSR。(谷歌表示,其机器人将搜索动态生成的内容,但谁知道呢)


function Delayed({
timeout = 0,
children,
}) {
const [isShown, setIsShown] = React.useState(false)

React.useEffect(() => {
if (isShown) return

const timeoutID = setTimeout(() => {
setIsShown(true)
}, timeout)

return () => {
clearTimeout(timeoutID)
}
}, [timeout])

if (!isShown) return

return children
}

and then

然后


<Delayed>
<Content />
</Delayed>

If you have another way of solving this I will be glad to hear it.

如果你有别的办法解决这个问题,我会很高兴听到的。


更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com