Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

feat(nuxt): add ssr route rule to enable SPA mode #7938

Merged
merged 5 commits into from
Oct 11, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/nuxt/src/core/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,9 @@ async function resolveHandlers (nuxt: Nuxt) {
devHandlers
}
}

declare module 'nitropack' {
interface NitroRouteOption {
ssr?: boolean
}
}
8 changes: 8 additions & 0 deletions packages/nuxt/src/core/runtime/nitro/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { RenderResponse } from 'nitropack'
import type { Manifest } from 'vite'
import { appendHeader, getQuery } from 'h3'
import devalue from '@nuxt/devalue'
import { createRouter as createMatcher } from 'radix3'
import { joinURL } from 'ufo'
import { renderToString as _renderToString } from 'vue/server-renderer'
import { useRuntimeConfig, useNitroApp, defineRenderHandler } from '#internal/nitro'
Expand Down Expand Up @@ -106,6 +107,9 @@ const getSPARenderer = lazyCachedFunction(async () => {
}
})

// Set up route rule matcher
const routerOptions = createMatcher({ routes: useRuntimeConfig().nitro.routes })

const PAYLOAD_CACHE = (process.env.NUXT_PAYLOAD_EXTRACTION && process.env.prerender) ? new Map() : null // TODO: Use LRU cache
const PAYLOAD_URL_RE = /\/_payload(\.[a-zA-Z0-9]+)?.js(\?.*)?$/

Expand All @@ -128,6 +132,9 @@ export default defineRenderHandler(async (event) => {
}
}

// TODO: share across endpoints on event context
const routeOptions = event.context.routeOptions || routerOptions.lookup(url) || {}

// Initialize ssr context
const ssrContext: NuxtSSRContext = {
url,
Expand All @@ -138,6 +145,7 @@ export default defineRenderHandler(async (event) => {
noSSR:
!!(process.env.NUXT_NO_SSR) ||
!!(event.req.headers['x-nuxt-no-ssr']) ||
routeOptions.ssr === false ||
(process.env.prerender ? PRERENDER_NO_SSR_ROUTES.has(url) : false),
error: !!ssrError,
nuxt: undefined!, /* NuxtApp */
Expand Down
6 changes: 6 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ describe('server api', () => {
})
})

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

describe('pages', () => {
it('render index', async () => {
const html = await $fetch('/')
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/basic/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export default defineNuxtConfig({
'./extends/node_modules/foo'
],
nitro: {
routes: {
'/route-rules/spa': { ssr: false }
},
output: { dir: process.env.NITRO_OUTPUT_DIR },
prerender: {
routes: [
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/basic/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>