简体   繁体   English

Next.js 独特动态路由渲染策略

[英]Next.js rendering strategy for unique dynamic route

I want to make the best use of the server where my Next.js web app will be hosted, even if it is at the cost of the APIs where users get informations.我想充分利用托管我的 Next.js web 应用程序的服务器,即使它是以用户获取信息的 API 为代价的。

So I was wondering what was the best approach to render uniques dynamic routes, for example: /post/[postId] .所以我想知道呈现唯一动态路由的最佳方法是什么,例如: /post/[postId]

I want to avoid SSR and have static HTML files hydrated by APIs as often as possible, as I've made for /home/[page] where I've done some ISR to avoid frequent rerenders like this:我想避免 SSR,并让 static HTML 文件尽可能频繁地被 API 水化,就像我为/home/[page]所做的那样,我在其中做了一些 ISR 以避免像这样频繁重新渲染:

export async function getStaticProps(context = {}) { 
    return {
        props: {},
        revalidate: 120, //cache page for 120s
      }
}
// No prerender of paths <=> "paths: []"
export async function getStaticPaths(context = {}) { 
    return {
    paths: [],
      fallback: 'blocking'
    }
}

The problem for /post/[postId] being that postId is a unique identifier so caching the page has no real interest and prerendering is not possible. /post/[postId]的问题是 postId 是唯一标识符,因此缓存页面没有真正的意义,并且无法进行预呈现。

The thing is /post/id1 and /post/id2 have no real HTML differences because the [postId] property is only used in a useEffect to fetch data so a SSR is a complete waste of server ressources..问题是/post/id1/post/id2没有真正的 HTML 差异,因为 [postId] 属性仅在 useEffect 中用于获取数据,因此 SSR 完全是服务器资源的浪费。

So the question is what could be a way to optimise Next.js rendering uniques dynamics routes?所以问题是有什么方法可以优化 Next.js 呈现独特的动态路线? Any idea is welcomed !欢迎任何想法!

I guess one way is to use dynamic imports.我想一种方法是使用动态导入。 This will decrease your bundle size and introduce lazy loading for your JavaScript code.这将减少您的包大小并为您的 JavaScript 代码引入延迟加载。 One note with static HTML pages is that they are small in size so not a lot of optimization is needed. static HTML 页的一个注意事项是它们的大小很小,因此不需要大量优化。

const SomePage = dynamic(
  () => import('@modules/some-page/index'),
  {
    ssr: false,
  }
);

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

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