From 822928b07e41b4579d7079927988cd6cd90c48ce Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 8 Jun 2022 20:37:50 +0100 Subject: [PATCH] fix(nuxt): add `ssrContext` types on `NuxtApp` (#5333) --- packages/nuxt/src/app/composables/ssr.ts | 2 +- packages/nuxt/src/app/entry.ts | 2 +- packages/nuxt/src/app/nuxt.ts | 20 +++++++++++++--- .../nuxt/src/core/runtime/nitro/renderer.ts | 24 ++++--------------- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/packages/nuxt/src/app/composables/ssr.ts b/packages/nuxt/src/app/composables/ssr.ts index 3829af692f..39be8cdc37 100644 --- a/packages/nuxt/src/app/composables/ssr.ts +++ b/packages/nuxt/src/app/composables/ssr.ts @@ -7,7 +7,7 @@ export function useRequestHeaders (include: K[]): Rec export function useRequestHeaders (): Readonly>; export function useRequestHeaders (include?) { if (process.client) { return {} } - const headers: Record = useNuxtApp().ssrContext?.event.req.headers ?? {} + const headers: Record = useNuxtApp().ssrContext?.event.req.headers ?? {} if (!include) { return headers } return Object.fromEntries(include.filter(key => headers[key]).map(key => [key, headers[key]])) } diff --git a/packages/nuxt/src/app/entry.ts b/packages/nuxt/src/app/entry.ts index 412b30adf1..36388b7826 100644 --- a/packages/nuxt/src/app/entry.ts +++ b/packages/nuxt/src/app/entry.ts @@ -24,7 +24,7 @@ let entry: Function const plugins = normalizePlugins(_plugins) if (process.server) { - entry = async function createNuxtAppServer (ssrContext: CreateOptions['ssrContext'] = {}) { + entry = async function createNuxtAppServer (ssrContext: CreateOptions['ssrContext']) { const vueApp = createApp(RootComponent) vueApp.component('App', AppComponent) diff --git a/packages/nuxt/src/app/nuxt.ts b/packages/nuxt/src/app/nuxt.ts index 65d50a560b..cb49190445 100644 --- a/packages/nuxt/src/app/nuxt.ts +++ b/packages/nuxt/src/app/nuxt.ts @@ -4,6 +4,8 @@ import type { App, onErrorCaptured, VNode } from 'vue' import { createHooks, Hookable } from 'hookable' import type { RuntimeConfig } from '@nuxt/schema' import { getContext } from 'unctx' +import type { SSRContext } from 'vue-bundle-renderer' +import type { CompatibilityEvent } from 'h3' import { legacyPlugin, LegacyContext } from './compat/legacy-app' const nuxtAppCtx = getContext('nuxt-app') @@ -48,11 +50,23 @@ interface _NuxtApp { _asyncDataPromises?: Record> _legacyContext?: LegacyContext - ssrContext?: Record & { + ssrContext?: SSRContext & { + url: string + event: CompatibilityEvent + /** @deprecated Use `event` instead. */ + req?: CompatibilityEvent['req'] + /** @deprecated Use `event` instead. */ + res?: CompatibilityEvent['res'] + runtimeConfig: RuntimeConfig + noSSR: boolean + error?: any + nuxt: _NuxtApp + payload: _NuxtApp['payload'] + teleports?: Record renderMeta?: () => Promise | NuxtMeta } payload: { - serverRendered?: true + serverRendered?: boolean data?: Record state?: Record rendered?: Function @@ -115,7 +129,7 @@ export function createNuxtApp (options: CreateOptions) { if (process.server) { // Expose to server renderer to create window.__NUXT__ - nuxtApp.ssrContext = nuxtApp.ssrContext || {} + nuxtApp.ssrContext = nuxtApp.ssrContext || {} as any nuxtApp.ssrContext.payload = nuxtApp.payload } diff --git a/packages/nuxt/src/core/runtime/nitro/renderer.ts b/packages/nuxt/src/core/runtime/nitro/renderer.ts index 4bf3ab9ecb..98cfba67fc 100644 --- a/packages/nuxt/src/core/runtime/nitro/renderer.ts +++ b/packages/nuxt/src/core/runtime/nitro/renderer.ts @@ -1,10 +1,10 @@ import { createRenderer } from 'vue-bundle-renderer' -import type { SSRContext } from 'vue-bundle-renderer' -import { CompatibilityEvent, eventHandler, useQuery } from 'h3' +import { eventHandler, useQuery } from 'h3' import devalue from '@nuxt/devalue' -import { RuntimeConfig } from '@nuxt/schema' import { renderToString as _renderToString } from 'vue/server-renderer' +import type { NuxtApp } from '#app' + // @ts-ignore import { useRuntimeConfig } from '#internal/nitro' // @ts-ignore @@ -12,20 +12,7 @@ import { buildAssetsURL } from '#paths' // @ts-ignore import htmlTemplate from '#build/views/document.template.mjs' -interface NuxtSSRContext extends SSRContext { - url: string - noSSR: boolean - redirected: boolean - event: CompatibilityEvent - req: CompatibilityEvent['req'] - res: CompatibilityEvent['res'] - runtimeConfig: RuntimeConfig - error?: any - nuxt?: any - payload?: any - teleports?: { body?: string } - renderMeta?: () => Promise -} +type NuxtSSRContext = NuxtApp['ssrContext'] interface RenderResult { html: any @@ -126,7 +113,6 @@ export default eventHandler(async (event) => { runtimeConfig: useRuntimeConfig(), noSSR: !!event.req.headers['x-nuxt-no-ssr'], error: ssrError, - redirected: undefined, nuxt: undefined, /* NuxtApp */ payload: undefined } @@ -140,7 +126,7 @@ export default eventHandler(async (event) => { // If we error on rendering error page, we bail out and directly return to the error handler if (!rendered) { return } - if (ssrContext.redirected || event.res.writableEnded) { + if (event.res.writableEnded) { return }