mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
feat(nuxt): add experimentalNoScripts
route rule (#19805)
This commit is contained in:
parent
90d9cbbe88
commit
163913a744
@ -100,6 +100,7 @@ Nuxt 3 includes route rules and hybrid rendering support. Using route rules you
|
||||
- `swr` - Add cache headers to the server response and cache it in the server or reverse proxy for a configurable TTL. The `node-server` preset of Nitro is able to cache the full response. For Netlify and Vercel, the response is also added to the CDN layer.
|
||||
- `static` - The behavior is the same as `swr` except that there is no TTL; the response is cached until the next deployment. On Netlify and Vercel, it enables full incremental static generation.
|
||||
- `prerender` - Prerenders routes at build time and includes them in your build as static assets
|
||||
- `experimentalNoScripts` - Disables rendering of Nuxt scripts and JS resource hints for sections of your site.
|
||||
|
||||
**Examples:**
|
||||
|
||||
|
@ -276,6 +276,8 @@ export default defineRenderHandler(async (event) => {
|
||||
? await renderInlineStyles(ssrContext.modules ?? ssrContext._registeredComponents ?? [])
|
||||
: ''
|
||||
|
||||
const NO_SCRIPTS = process.env.NUXT_NO_SCRIPTS || routeOptions.experimentalNoScripts
|
||||
|
||||
// Create render context
|
||||
const htmlContext: NuxtRenderHTMLContext = {
|
||||
island: Boolean(islandContext),
|
||||
@ -285,7 +287,7 @@ export default defineRenderHandler(async (event) => {
|
||||
process.env.NUXT_JSON_PAYLOADS
|
||||
? _PAYLOAD_EXTRACTION ? `<link rel="modulepreload" href="${payloadURL}">` : null
|
||||
: _PAYLOAD_EXTRACTION ? `<link rel="preload" as="fetch" crossorigin="anonymous" href="${payloadURL}">` : null,
|
||||
_rendered.renderResourceHints(),
|
||||
NO_SCRIPTS ? null : _rendered.renderResourceHints(),
|
||||
_rendered.renderStyles(),
|
||||
inlinedStyles,
|
||||
ssrContext.styles
|
||||
@ -297,7 +299,7 @@ export default defineRenderHandler(async (event) => {
|
||||
]),
|
||||
body: [_rendered.html],
|
||||
bodyAppend: normalizeChunks([
|
||||
process.env.NUXT_NO_SCRIPTS
|
||||
NO_SCRIPTS
|
||||
? undefined
|
||||
: (_PAYLOAD_EXTRACTION
|
||||
? process.env.NUXT_JSON_PAYLOADS
|
||||
@ -307,7 +309,7 @@ export default defineRenderHandler(async (event) => {
|
||||
? renderPayloadJsonScript({ id: '__NUXT_DATA__', ssrContext, data: ssrContext.payload })
|
||||
: renderPayloadScript({ ssrContext, data: ssrContext.payload })
|
||||
),
|
||||
_rendered.renderScripts(),
|
||||
routeOptions.experimentalNoScripts ? undefined : _rendered.renderScripts(),
|
||||
// Note: bodyScripts may contain tags other than <script>
|
||||
renderedMeta.bodyScripts
|
||||
])
|
||||
|
2
packages/nuxt/types.d.ts
vendored
2
packages/nuxt/types.d.ts
vendored
@ -11,8 +11,10 @@ declare global {
|
||||
declare module 'nitropack' {
|
||||
interface NitroRouteConfig {
|
||||
ssr?: boolean
|
||||
experimentalNoScripts?: boolean
|
||||
}
|
||||
interface NitroRouteRules {
|
||||
ssr?: boolean
|
||||
experimentalNoScripts?: boolean
|
||||
}
|
||||
}
|
||||
|
@ -95,6 +95,7 @@ export default defineUntypedSchema({
|
||||
|
||||
/**
|
||||
* Turn off rendering of Nuxt scripts and JS resource hints.
|
||||
* You can also disable scripts more granularly within `routeRules`.
|
||||
*/
|
||||
noScripts: false,
|
||||
|
||||
|
@ -47,6 +47,11 @@ describe('route rules', () => {
|
||||
expect(script.serverRendered).toEqual(false)
|
||||
expect(attrs['data-ssr']).toEqual('false')
|
||||
})
|
||||
|
||||
it('test noScript routeRules', async () => {
|
||||
const page = await createPage('/no-scripts')
|
||||
expect(await page.locator('script').all()).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('modules', () => {
|
||||
|
3
test/fixtures/basic/nuxt.config.ts
vendored
3
test/fixtures/basic/nuxt.config.ts
vendored
@ -51,7 +51,8 @@ export default defineNuxtConfig({
|
||||
],
|
||||
nitro: {
|
||||
routeRules: {
|
||||
'/route-rules/spa': { ssr: false }
|
||||
'/route-rules/spa': { ssr: false },
|
||||
'/no-scripts': { experimentalNoScripts: true }
|
||||
},
|
||||
output: { dir: process.env.NITRO_OUTPUT_DIR },
|
||||
prerender: {
|
||||
|
3
test/fixtures/basic/pages/index.vue
vendored
3
test/fixtures/basic/pages/index.vue
vendored
@ -21,6 +21,9 @@
|
||||
<button @click="someValue++">
|
||||
Increment state
|
||||
</button>
|
||||
<NuxtLink to="/no-scripts">
|
||||
to no script
|
||||
</NuxtLink>
|
||||
<NestedSugarCounter :multiplier="2" />
|
||||
<CustomComponent />
|
||||
<Spin>Test</Spin>
|
||||
|
12
test/fixtures/basic/pages/no-scripts.vue
vendored
Normal file
12
test/fixtures/basic/pages/no-scripts.vue
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
<template>
|
||||
<div>
|
||||
{{ count }}
|
||||
<button @click="count++">
|
||||
++
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
const count = ref(0)
|
||||
</script>
|
Loading…
Reference in New Issue
Block a user