Skip to content

Commit

Permalink
feat: add ssr routeRule (#593)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Oct 25, 2022
1 parent a725a7a commit de9e5c2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
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

0 comments on commit de9e5c2

Please sign in to comment.