[Nuxt] Rendering mode

news/2025/2/4 14:43:19/文章来源:https://www.cnblogs.com/Answer1215/p/18698204

A Nuxt app can be rendered in various modes.

The default standard mode is simply called SSR. Specifically, it means Server-Side Rendering on request. “on request” means whenever the user clicks on an external link or enters a URL in the address bar to access a page.

The default mode is great for a highly-dynamic website with ever-changing content where timing is critical, such as news sites, forums, and e-commerce platforms.

However, there are other flavors of Server-Side Rendering that could be more suitable for your unique use cases:

  • Static-Site Generation (SSG) is rendering each page only once at build time. This is great for websites that are content driven but they don’t get updates often, maybe only once a week, such as a documentation site. This is faster than the default SSG because the page doesn’t have to be generated on request. Additionally, you don’t need an app server for hosting an SSG site.
  • Incremental Static Regeneration (ISR) is rendering that takes place in the background, and doing so from time to time to keep the content up-to-date. “In the background” means that it will serve the old version of the page while the new version is being generated in the background. And the new version will be served for future requests. This is great for content-driven websites that get updated often but the timing of the update is not critical as long as the sites do get updated within an hour or so, such as a blog. You can think of this as a mixture of the SSG and the default SSR. But it does require a hosting platform that supports CDN caching, such as Netlify and Vercel.
  • SWR (Stale-While-Revalidate) is another mode that is similar to ISR, but it doesn’t require CDN cache, it can be cached with a regular app server. We’ll talk more about the difference between ISR and SWR in a bit.

While all of these modes are considered server-side rendering, they will also render the components on the client side. This behavior doesn’t change no matter what mode you’re using because Nuxt.js is fundamentally a Vue.js framework, and Vue.js is a Single-Page App framework. Regardless of what’s happening on the server, Single-Page App components have to render on the client side.

That’s also why Pure SPA Rendering is also an available mode in Nuxt.js. This will behave exactly the same as a standard Vue.js app without any Server-Side Rendering, but it will still be driven by the Nuxt.js file-based router. This is great for websites that don’t require SEO because you will not need an app server for hosting a pure SPA.

Aside from SEO, Server-Side Rendering in general is about gaining a performance edge on the first load of your site. So if both SEO and performance are not what you care about, it’s fine to just use Pure SPA Rendering.


Choose the right mode

To choose the right rendering mode for your project, you can use this set of questions as a reference:

criteria-01.png

So, for example, if SEO is important, if updates occur weekly or less often, if you want static hosting, and if first-load performance is important:

choice-01.png

Your ideal choice would be SSG.

But there can be other project-specific factors that come into play, so you might still need a deeper analysis for your project before arriving to a decision.

Now that we know how to choose a rendering mode, let’s talk about how to configure it in the project.


Route Rules Configuration

By default, a Nuxt app will be rendered using the standard SSR, which generates the page on request. If we want something else, we can configure the rendering mode in the nuxt.config.ts file using the routeRules property.

For instance, if you want SSG, use prerender: true:

/nuxt.config.ts

export default defineNuxtConfig({routeRules: {'**': { prerender: true } // SSG},...
})

Double asterisk means all pages. We have to specify the pages because Nuxt.js allows us to render an app with different rendering modes for different pages. But for now, let’s render all the pages the same way.

And for ISR, we can set the isr property with a number:

/nuxt.config.ts

export default defineNuxtConfig({routeRules: {'**': { isr: 300 }},...
})

This will now regenerate the pages on the first request 300 seconds after the previous regeneration. The regeneration process will occur in the background.

If you just want to host this on a regular app server, you can use swr instead:

export default defineNuxtConfig({routeRules: {'**': { swr: 300 }},...
})

This will also regenerate 300 seconds after the previous regeneration.

And if you want to provide it with a first generation at build time, you can mix swr and prerender:

export default defineNuxtConfig({routeRules: {'**': { swr: 300, prerender: true }},...
})

Now the pages will be generated at build time, and then they will be regenerated in the background as the requests are hitting the server. And of course, you can also use isr instead of swr.

Next, let’s talk more about the differences between SWR and ISR.


SWR vs ISR

With a supported hosting platform, isr will be cached in the CDN layer. So it will be faster than using swr, which serves all the way from the original app server.

Furthermore, both isr and swr can be configured with a boolean instead of a number:

/nuxt.config.ts

export default defineNuxtConfig({routeRules: {'**': { swr: true }},...
})

This is where they become very different.

isr: true will essentially function like an on-demand SSG, where the page will be rendered on demand, but it will not update again until you redeploy your website.

On the other hand, swr: true will still regenerate the page with a new version in the background. The new version will replace the old version in the cache if they’re different.


Finally, if you do not want any type of Server-Side Rendering and prefer a pure Single Page Application (SPA), set ssr: false.

export default defineNuxtConfig({routeRules: {'**': { ssr: false } // Pure SPA},...
})

Hybrid Rendering

Now let’s talk about Hybrid Rendering, that is, generating different pages with different modes.

You can specify the paths of the pages:

export default defineNuxtConfig({routeRules: {'/': { ssr: true }, // Home Page'/categories': { prerender: true }, // Categories Page'/categories/*': { swr: 300 }, // Category Pages'/posts/*': { swr: 60 } // Post Details Page},...
})
  • The Home Page is generated on each request. Since SSR is the default behavior, this is optional.
  • The Categories Page will be generated once at build time.
  • The Category Pages will be also be regenerated in the background, so we use swr: 300. The asterisk means all pages within a directory.
  • The Post Details Pages will be regenerated incrementally.

Notice that we don’t refer to dynamic paths by their file names with their dynamic segments, such as /categories/[category] or /posts/[post]. We’re using the asterisk in place of the dynamic segment on the path.

Hybrid rendering is useful because not all pages share the same update frequency. For instance, the Home Page would need to be updated whenever one of the articles is updated. In contrast, the Categories Page would only need to be updated when a new article is added to the site.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/878549.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ASP.NET Core 中,认证(Authentication)和授权(Authorization)

在 ASP.NET Core 中,认证(Authentication)和授权(Authorization)是两个非常重要的概念。它们确保用户能够安全地访问应用程序,并且在访问过程中能按其权限被正确地控制。接下来,我将详细解释这两个概念,并且如何在 ASP.NET Core 中实现它们。 1. 认证(Authentication)…

ASP.NET Core 中,Cookie 认证在集群环境下的应用

在 ASP.NET Core 中,Cookie 认证在集群环境下的应用通常会遇到一些挑战。主要的问题是 Cookie 存储在客户端的浏览器中,而认证信息(比如 Session 或身份令牌)通常是保存在 Cookie 中,多个应用实例需要共享这些 Cookie 信息,以便用户在集群中各个实例间无缝切换。 1. 集群…

ASP.NET Core 中使用 Cookie 身份验证

在 ASP.NET Core 中使用 Cookie 身份验证,通常是为了实现用户的登录和授权。以下是配置 Cookie 身份验证的步骤。 1. 安装必要的 NuGet 包 首先,确保项目中包含 Microsoft.AspNetCore.Authentication.Cookies 包。你可以通过 NuGet 包管理器或命令行安装它: dotnet add pack…

GDB调试(一)

GDB调试 GDB简介 GDB的功能 GDB(GNU Debugger)是用于调试 C、C++ 等语言的强大工具。它允许开发者执行以下操作:启动程序并按照预期条件暂停(如断点处)。 检查程序中的变量和内存状态。 单步执行代码,观察每一步的变化。 修改运行中的变量值以测试不同的假设。 调试程序崩…

Nginx 粘性会话配置与实现详解

**Nginx 粘性会话(Sticky Session)**是指将同一个客户端的请求始终路由到相同的后端服务器,确保该客户端的多个请求在同一个会话期间都由同一台服务器处理。粘性会话通常在负载均衡环境中使用,特别是当应用程序依赖于在同一会话中保持用户状态(例如购物车、登录会话等)时…

ASP.NET Core 中,操作过滤器(Action Filters)

在ASP.NET Core 中,操作过滤器(Action Filters)用于在控制器的操作方法执行之前或之后执行自定义逻辑。操作过滤器主要用于在请求到达控制器方法之前进行处理(例如:验证请求参数、设置数据)、在操作执行后处理响应(例如:记录日志、修改响应结果)等。 操作过滤器的工作…

ASP.NET Core 中间件(Middleware)

在ASP.NET Core 中,中间件是用来处理 HTTP 请求和响应管道的组件。中间件的核心思想是通过一个链式的管道处理请求和响应,每个中间件既可以处理请求或响应,也可以将其传递给下一个中间件。以下是详细解释:1. 中间件是什么? 中间件是一个软件组件,用于在 HTTP 请求到达应用…

elf2部署官方yolov5模型

ELF2开发板(飞凌嵌入式)搭建深度学习环境部署(RKNN环境部署)本人主要介绍用于elf2的rk3588开发板的深度学习环境的搭建,和官方的方法不同,对于新手比较友好。零基础即可搭建,本人使用的是WSL2系统,当然使用虚拟机也是可以的,本人主要教学搭建yolov5模型转换为rknn的环…

ASP.NET Core 中授权过滤器(Authorization Filters)

在ASP.NET Core 中,授权过滤器(Authorization Filters)是用于在请求到达控制器操作方法之前,验证用户是否具有执行该操作的权限的一种机制。授权过滤器的主要作用是确保用户在访问控制器或操作方法时,已通过身份验证并且有足够的权限。 授权过滤器的工作原理 授权过滤器在…

Roo Code插件搭配DeepSeek快速创建项目示例

一、环境准备 1. 安装VSCode 访问 Visual Studio Code官网 下载并安装最新版本 2. 安装IDEA(运行Java项目) 访问IDEA官网下载并安装最新Community社区版二、Roo Code插件配置 1. 安装插件打开VSCode扩展市场(Ctrl+Shift+X) 搜索 "Roo Code" 安装官方插件(确认发…

电子书查找阅读教程

免责声明本文仅作学习交流,对于喜欢的作者,建议支持正版。软件下载Github发布地址:https://github.com/gedoor/legado/releases 完整教学:https://www.yuque.com/legado/wiki/xz直接浏览器打开选择最新版本下载即可书源导入喵工资订阅源:https://dy.miaogongzi.cc/直接浏览…