如何从Next.js中的一个页面请求中预取代码?

发布于 2025-02-04 17:23:28 字数 223 浏览 3 评论 0原文

使用客户端导航,我只能使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两人的回忆 2025-02-11 17:23:28

只要链接(使用 next/link)到这些页面在视口内,在生产模式下,默认情况下将预摘要(尽管预取在Dev模式下不起作用)。

预摘要 - 在后台预摘要页面。默认为true
任何&lt; link /&gt; < /code>(最初或通过滚动)
将预装。可以通过通过
prefetch = {false}。当预摘要设置为false时,预摘要将
仍然发生在悬停。使用静态生成
带有数据的文件,以进行更快的页面过渡。 预摘要仅在生产中启用


如果您不使用next/link来导航到这些页面,则可以使用 router.prefetch 强制预取。

router.prefetch(url,AS)

  • url - 预取的URL,包括显式路由(例如
    /dashboard)和动态路由(例如/product/[id]
  • AS -
    url的可选装饰器。 Next.js 9.5.3之前
    预取动态路线,检查我们的
    文档

    要查看它的工作方式

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).

prefetch - Prefetch the page in the background. Defaults to true.
Any <Link /> that is in the viewport (initially or through scroll)
will be preloaded. Prefetch can be disabled by passing
prefetch={false}. When prefetch is set to false, prefetching will
still occur on hover. Pages using Static Generation will preload JSON
files with the data for faster page transitions. Prefetching is only enabled in production.

If you're not using next/link to navigate to those pages, then you can use router.prefetch to force the prefetch.

router.prefetch(url, as)

  • url - The URL to prefetch, including explicit routes (e.g.
    /dashboard) and dynamic routes (e.g. /product/[id])
  • as -
    Optional decorator for url. Before Next.js 9.5.3 this was used to
    prefetch dynamic routes, check our previous
    docs

    to see how it worked
最美的太阳 2025-02-11 17:23:28

这让我感到惊讶,因为我手动在链接中手动添加了预取,现在立即打开页面。之前,加载页面花了几毫秒。

我不明白为什么,因为我发现生产中的真正默认值,但对我来说有所不同。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文