如何从Next.js中的一个页面请求中预取代码?
使用客户端导航,我只能使用JavaScript在页面之间导航。页面的第一个请求需要一些时间才能渲染,然后几乎可以立即导航回到它。在第一个渲染期间,该页面仅加载其必要的内容。隔离每个页面使它们独立。我只想知道,如果有单个请求中的一个以上页面加载代码的方法。
例如,假设我有一个用于用户的配置文件(信息,设置,历史记录)。我想根据任何页面的一个请求加载所有这些页面的代码,因此它们之间的导航将是即时的,并且将没有等待时间。
Using client-side navigation I can navigate between pages using only JavaScript. The first request of a page takes some time to render and then the navigation back to it is nearly instant. During that first render that page only loads what’s necessary for it. That isolates each page making them independent. I just want to know, if there is a way to load the code for more than one page on a single request.
As an example, let's say I have multiple profile pages for a user (info, settings, history). I want to load the code for all of those pages on one request for any of those pages, so the navigation between them will be instant, and there will be no waiting time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只要链接(使用
next/link
)到这些页面在视口内,在生产模式下,默认情况下将预摘要(尽管预取在Dev模式下不起作用)。
如果您不使用
next/link
来导航到这些页面,则可以使用router.prefetch
强制预取。As long as the links (using
next/link
) to those pages are within the viewport the pages will be prefetched by default when in production mode (prefetch doesn't work in dev mode, though).If you're not using
next/link
to navigate to those pages, then you can userouter.prefetch
to force the prefetch.这让我感到惊讶,因为我手动在链接中手动添加了预取,现在立即打开页面。之前,加载页面花了几毫秒。
我不明白为什么,因为我发现生产中的真正默认值,但对我来说有所不同。
This is surprising to me because I manually added prefetch in the link, and now the pages open instantly. Before, it took a few milliseconds to load the pages.
I don't understand why, as I found the true default in production, but it works differently for me.