From 6198a1151485882e4e27bdd31d112c69fff230ff Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 28 Aug 2023 17:46:49 +0100 Subject: [PATCH] fix(nuxt): render server components when `ssr: false` (#22869) --- packages/nuxt/src/app/components/nuxt-island.ts | 2 +- packages/nuxt/src/core/nuxt.ts | 7 +++++++ packages/nuxt/src/core/runtime/nitro/renderer.ts | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/nuxt/src/app/components/nuxt-island.ts b/packages/nuxt/src/app/components/nuxt-island.ts index 3955b8d8ef..0c4fb8c67b 100644 --- a/packages/nuxt/src/app/components/nuxt-island.ts +++ b/packages/nuxt/src/app/components/nuxt-island.ts @@ -171,7 +171,7 @@ export default defineComponent({ if (import.meta.client && !nuxtApp.isHydrating && props.lazy) { fetchComponent() - } else if (import.meta.server || !nuxtApp.isHydrating) { + } else if (import.meta.server || !nuxtApp.isHydrating || !nuxtApp.payload.serverRendered) { await fetchComponent() } diff --git a/packages/nuxt/src/core/nuxt.ts b/packages/nuxt/src/core/nuxt.ts index 36ccb593be..03b4f0a398 100644 --- a/packages/nuxt/src/core/nuxt.ts +++ b/packages/nuxt/src/core/nuxt.ts @@ -8,6 +8,7 @@ import escapeRE from 'escape-string-regexp' import fse from 'fs-extra' import { withoutLeadingSlash } from 'ufo' /* eslint-disable import/no-restricted-paths */ +import defu from 'defu' import pagesModule from '../pages/module' import metaModule from '../head/module' import componentsModule from '../components/module' @@ -283,6 +284,12 @@ async function initNuxt (nuxt: Nuxt) { priority: 10, // built-in that we do not expect the user to override filePath: resolve(nuxt.options.appDir, 'components/nuxt-island') }) + + if (!nuxt.options.ssr) { + nuxt.options.ssr = true + nuxt.options.nitro.routeRules ||= {} + nuxt.options.nitro.routeRules['/**'] = defu(nuxt.options.nitro.routeRules['/**'], { ssr: false }) + } } // Add experimental cross-origin prefetch support using Speculation Rules API diff --git a/packages/nuxt/src/core/runtime/nitro/renderer.ts b/packages/nuxt/src/core/runtime/nitro/renderer.ts index bc2b75cb9a..13c053a3ca 100644 --- a/packages/nuxt/src/core/runtime/nitro/renderer.ts +++ b/packages/nuxt/src/core/runtime/nitro/renderer.ts @@ -259,7 +259,7 @@ export default defineRenderHandler(async (event): Promise