From 4ed14d13b0c12565d8879024e52b4da6a6a20c47 Mon Sep 17 00:00:00 2001 From: Alex Liu Date: Wed, 26 Feb 2025 17:35:37 +0800 Subject: [PATCH] refactor(nuxt): update to `noScripts` route rule (#31083) --- docs/1.getting-started/8.server.md | 2 +- docs/2.guide/1.concepts/3.rendering.md | 2 +- packages/nuxt/src/core/nitro.ts | 12 ++++++++++++ packages/nuxt/src/core/runtime/nitro/renderer.ts | 4 ++-- packages/nuxt/src/core/templates.ts | 8 ++++++++ packages/nuxt/types.d.mts | 8 ++++++++ packages/nuxt/types.d.ts | 8 ++++++++ test/fixtures/basic/nuxt.config.ts | 2 +- 8 files changed, 41 insertions(+), 5 deletions(-) diff --git a/docs/1.getting-started/8.server.md b/docs/1.getting-started/8.server.md index cf9c43218a..00896b372d 100644 --- a/docs/1.getting-started/8.server.md +++ b/docs/1.getting-started/8.server.md @@ -87,7 +87,7 @@ export default defineNuxtConfig({ Learn about all available route rules are available to customize the rendering mode of your routes. :: -In addition, there are some route rules (for example, `ssr`, `appMiddleware`, and `experimentalNoScripts`) that are Nuxt specific to change the behavior when rendering your pages to HTML. +In addition, there are some route rules (for example, `ssr`, `appMiddleware`, and `noScripts`) that are Nuxt specific to change the behavior when rendering your pages to HTML. Some route rules (`appMiddleware`, `redirect` and `prerender`) also affect client-side behavior. diff --git a/docs/2.guide/1.concepts/3.rendering.md b/docs/2.guide/1.concepts/3.rendering.md index b0b2117ef9..25cca93e8b 100644 --- a/docs/2.guide/1.concepts/3.rendering.md +++ b/docs/2.guide/1.concepts/3.rendering.md @@ -189,7 +189,7 @@ The different properties you can use are the following: - `swr: number | boolean`{lang=ts} - Add cache headers to the server response and cache it on the server or reverse proxy for a configurable TTL (time to live). The `node-server` preset of Nitro is able to cache the full response. When the TTL expired, the cached response will be sent while the page will be regenerated in the background. If true is used, a `stale-while-revalidate` header is added without a MaxAge. - `isr: number | boolean`{lang=ts} - The behavior is the same as `swr` except that we are able to add the response to the CDN cache on platforms that support this (currently Netlify or Vercel). If `true` is used, the content persists until the next deploy inside the CDN. - `prerender: boolean`{lang=ts} - Prerenders routes at build time and includes them in your build as static assets -- `experimentalNoScripts: boolean`{lang=ts} - Disables rendering of Nuxt scripts and JS resource hints for sections of your site. +- `noScripts: boolean`{lang=ts} - Disables rendering of Nuxt scripts and JS resource hints for sections of your site. - `appMiddleware: string | string[] | Record`{lang=ts} - Allows you to define middleware that should or should not run for page paths within the Vue app part of your application (that is, not your Nitro routes) Whenever possible, route rules will be automatically applied to the deployment platform's native rules for optimal performances (Netlify and Vercel are currently supported). diff --git a/packages/nuxt/src/core/nitro.ts b/packages/nuxt/src/core/nitro.ts index 088adff5a1..cc794a0526 100644 --- a/packages/nuxt/src/core/nitro.ts +++ b/packages/nuxt/src/core/nitro.ts @@ -291,6 +291,18 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) { }) } + if (nuxt.options.future.compatibilityVersion !== 4) { + // TODO: remove in Nuxt v4 + nuxt.hook('nitro:config', (config) => { + for (const value of Object.values(config.routeRules || {})) { + if ('experimentalNoScripts' in value) { + value.noScripts = value.experimentalNoScripts + delete value.experimentalNoScripts + } + } + }) + } + nuxt.hook('nitro:config', (config) => { config.alias ||= {} config.alias['#app-manifest'] = join(tempDir, `meta/${buildId}.json`) diff --git a/packages/nuxt/src/core/runtime/nitro/renderer.ts b/packages/nuxt/src/core/runtime/nitro/renderer.ts index bb546dcf17..ac4dc40400 100644 --- a/packages/nuxt/src/core/runtime/nitro/renderer.ts +++ b/packages/nuxt/src/core/runtime/nitro/renderer.ts @@ -385,7 +385,7 @@ export default defineRenderHandler(async (event): Promise (