简体   繁体   English

Next.js 正在执行两个完整的 F5 刷新操作,以在 static 页面上显示新内容,并带有“重新验证:1”? 我期待一个

[英]Next.js is taking two full F5 refresh actions to show new content on a static page with `revalidate: 1`? I was expecting a single one

I have a blog post edit page on my Next.js project.我的 Next.js 项目上有一个博客文章编辑页面。

My post pages are using the Incremental Static Regeneration feature with a revalidate: 1 second (currently for testing).我的帖子页面使用增量 Static 再生功能, revalidate: 1秒(目前用于测试)。 I'll later be using something like revalidate: 300 (5 minutes).稍后我将使用类似revalidate: 300 (5 分钟)的方法。

To update a post, this is what I do:要更新帖子,这就是我所做的:

As an admin, I visit /admin/post/edit/my-post-slug , change whatever I need and save.作为管理员,我访问/admin/post/edit/my-post-slug ,更改我需要的任何内容并保存。

After saving, I'm doing a route.push to the post page, to see the new version in /post/my-post-slug .保存后,我正在执行route.push到帖子页面,以在/post/my-post-slug中查看新版本。

Here is the code:这是代码:

const savePost = async (post: BlogPost, router: NextRouter) => {
  await savePostAPI(post);
  cons POST_ROUTE = "/post/my-post-slug";
  router.push(POST_ROUTE);
};

It's all working as intended, with one little caveat.这一切都按预期工作,但有一点需要注意。

This is the flow I'm getting:这是我得到的流程:

- I'M ON ADMIN POST EDIT PAGE AND I SAVE
- I GO TO POST PAGE VIA router.push() AND I SEE THE OLD CONTENT (THIS IS EXPECTED)
- WAIT A LITLE
- I HIT REFRESH (F5) ONCE AND I RELOAD THE PAGE AND STILL SEE OLD CONTENT (NOT EXPECTED)
- WAIT A LITLE
- I HIT REFRESH (F5) AGAIN AND SEE THE NEW CONTENT

I'm okay with seeing the old content after router.push() .我可以在router.push()之后看到旧内容。 Next.js is supposed to always serve cached content, even a stale one, before regenerating the new version. Next.js 应该在重新生成新版本之前始终提供缓存的内容,即使是陈旧的内容。 But why don't I see the new content right after pressing F5 for the first time?但是为什么我第一次按F5后看不到新内容呢? Why do I need to refresh it twice to see the new content?为什么我需要刷新两次才能看到新内容? Doesn't router.push trigger a new server request and let the server know that it should regenerate the page (given the fact that is obviously stale, since revalidate:1 will make it stale after 1 second)? router.push不会触发一个新的服务器请求并让服务器知道它应该重新生成页面(鉴于这显然是陈旧的事实,因为revalidate:1会在 1 秒后使其陈旧)? Why is that happening?为什么会这样?

Instead of using router.push() should I just use window.location.href = "https://www.example.com/post/my-post-slug" to make sure I'll send that request that will trigger the regeneration of the page?而不是使用router.push()我应该只使用window.location.href = "https://www.example.com/post/my-post-slug"以确保我将发送将触发的请求页面的再生?

After further investigation, I can confirm.经过进一步调查,我可以确认。

router.push() implements a client-side transition, and that does not trigger a new getStaticProps call. router.push()实现了客户端转换,并且不会触发新的getStaticProps调用。 It only fetches a JSON object with previously generated page props .它仅使用先前生成的页面props获取JSON object 。

Ie: This means that the page will not be revalidated no matter how my client-side transitions you perform.即:这意味着无论您如何执行我的客户端转换,页面都不会被重新验证。

Either get the fresh data using client-side code, or do a full page reload from time to time.要么使用client-side代码获取新数据,要么不时重新加载整个页面。 At least 2 will be necessary.至少需要 2 个。 The first one will return stale data and will trigger the revalidation process.第一个将返回stale数据并触发重新验证过程。 The 2nd one will get the fresh data.第二个将获得新数据。

Yep two page reloads seems required per Next.js docs (which is annoying and not intuitive from a user perspective):是的,每个 Next.js 文档似乎都需要重新加载两个页面(从用户的角度来看,这很烦人且不直观):

After the 10-second window, the next request will still show the cached (stale) page Next.js triggers a regeneration of the page in the background.在 10 秒 window 之后,下一个请求仍将显示缓存(陈旧)页面 Next.js 在后台触发页面的重新生成。

My solution to this is to use ISR together with SWR which will revalidate the data on mount.我对此的解决方案是将ISRSWR一起使用,这将重新验证挂载数据。 This enables the data to stay fresh even on normal routing without the need of full page reloads.这使数据即使在正常路由上也能保持新鲜,而无需重新加载整页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM