fix(nuxt): add ssrContext types on NuxtApp (#5333)

This commit is contained in:
Daniel Roe 2022-06-08 20:37:50 +01:00 committed by GitHub
parent 700b7adbc6
commit 822928b07e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 24 deletions

View File

@ -7,7 +7,7 @@ export function useRequestHeaders<K extends string = string> (include: K[]): Rec
export function useRequestHeaders (): Readonly<Record<string, string>>;
export function useRequestHeaders (include?) {
if (process.client) { return {} }
const headers: Record<string, string> = useNuxtApp().ssrContext?.event.req.headers ?? {}
const headers: Record<string, string | string[]> = useNuxtApp().ssrContext?.event.req.headers ?? {}
if (!include) { return headers }
return Object.fromEntries(include.filter(key => headers[key]).map(key => [key, headers[key]]))
}

View File

@ -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)

View File

@ -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<NuxtApp>('nuxt-app')
@ -48,11 +50,23 @@ interface _NuxtApp {
_asyncDataPromises?: Record<string, Promise<any>>
_legacyContext?: LegacyContext
ssrContext?: Record<string, any> & {
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<string, string>
renderMeta?: () => Promise<NuxtMeta> | NuxtMeta
}
payload: {
serverRendered?: true
serverRendered?: boolean
data?: Record<string, any>
state?: Record<string, any>
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
}

View File

@ -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<any>
}
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
}