Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ssr routeRule #593

Merged
merged 1 commit into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export default defineNuxtConfig({
buildDir: process.env.NITRO_BUILD_DIR,
plugins: ['~/plugins/setup.js'],
nitro: {
routeRules: {
'/route-rules/spa': { ssr: false }
},
output: { dir: process.env.NITRO_OUTPUT_DIR }
},
bridge: {
Expand Down
5 changes: 5 additions & 0 deletions playground/pages/route-rules/spa.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<div>
should not be rendered on ssr
</div>
</template>
7 changes: 5 additions & 2 deletions src/runtime/nitro/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import devalue from '@nuxt/devalue'
import type { RuntimeConfig } from '@nuxt/schema'
import type { RenderResponse } from 'nitropack'
// @ts-ignore
import { useRuntimeConfig, useNitroApp, defineRenderHandler } from '#internal/nitro'
import { useRuntimeConfig, useNitroApp, defineRenderHandler, getRouteRules } from '#internal/nitro'
// @ts-ignore
import { buildAssetsURL } from '#paths'
// @ts-ignore
Expand Down Expand Up @@ -131,6 +131,9 @@ export default defineRenderHandler(async (event) => {
url = url.slice(STATIC_ASSETS_BASE.length, url.length - PAYLOAD_JS.length) || '/'
}

// Get route options (currently to apply `ssr: false`)
const routeOptions = getRouteRules(event)

// Initialize ssr context
const config = useRuntimeConfig()
const ssrContext: NuxtSSRContext = {
Expand All @@ -139,7 +142,7 @@ export default defineRenderHandler(async (event) => {
req: event.req,
res: event.res,
runtimeConfig: { private: config, public: { public: config.public, app: config.app } },
noSSR: !!event.req.headers['x-nuxt-no-ssr'],
noSSR: !!event.req.headers['x-nuxt-no-ssr'] || routeOptions.ssr === false,
error: ssrError,
redirected: undefined,
nuxt: undefined as undefined | Record<string, any>, /* Nuxt 2 payload */
Expand Down
12 changes: 11 additions & 1 deletion test/bridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ describe('errors', () => {
})
})

describe('route rules', () => {
it('should enable spa mode', async () => {
expect(await $fetch('/route-rules/spa')).toContain('serverRendered:false')
})
})

describe('dynamic paths', () => {
if (process.env.NUXT_TEST_DEV) {
// TODO:
Expand Down Expand Up @@ -143,7 +149,11 @@ describe('dynamic paths', () => {
\\"buildAssetsDir\\": \\"/_nuxt/\\"
},
\\"nitro\\": {
\\"routeRules\\": {},
\\"routeRules\\": {
\\"/route-rules/spa\\": {
\\"ssr\\": false
}
},
\\"envPrefix\\": \\"NUXT_\\"
},
\\"public\\": {
Expand Down
9 changes: 9 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,13 @@ declare module '@nuxt/schema' {
}
}

declare module 'nitropack' {
interface NitroRouteConfig {
ssr?: boolean
}
interface NitroRouteOptions {
ssr?: boolean
}
}

export declare function defineNuxtConfig (config: NuxtConfig): NuxtConfig