mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-26 07:32:01 +00:00
fix(nuxt): add ssrContext
types on NuxtApp
(#5333)
This commit is contained in:
parent
700b7adbc6
commit
822928b07e
@ -7,7 +7,7 @@ export function useRequestHeaders<K extends string = string> (include: K[]): Rec
|
|||||||
export function useRequestHeaders (): Readonly<Record<string, string>>;
|
export function useRequestHeaders (): Readonly<Record<string, string>>;
|
||||||
export function useRequestHeaders (include?) {
|
export function useRequestHeaders (include?) {
|
||||||
if (process.client) { return {} }
|
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 }
|
if (!include) { return headers }
|
||||||
return Object.fromEntries(include.filter(key => headers[key]).map(key => [key, headers[key]]))
|
return Object.fromEntries(include.filter(key => headers[key]).map(key => [key, headers[key]]))
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ let entry: Function
|
|||||||
const plugins = normalizePlugins(_plugins)
|
const plugins = normalizePlugins(_plugins)
|
||||||
|
|
||||||
if (process.server) {
|
if (process.server) {
|
||||||
entry = async function createNuxtAppServer (ssrContext: CreateOptions['ssrContext'] = {}) {
|
entry = async function createNuxtAppServer (ssrContext: CreateOptions['ssrContext']) {
|
||||||
const vueApp = createApp(RootComponent)
|
const vueApp = createApp(RootComponent)
|
||||||
vueApp.component('App', AppComponent)
|
vueApp.component('App', AppComponent)
|
||||||
|
|
||||||
|
@ -4,6 +4,8 @@ import type { App, onErrorCaptured, VNode } from 'vue'
|
|||||||
import { createHooks, Hookable } from 'hookable'
|
import { createHooks, Hookable } from 'hookable'
|
||||||
import type { RuntimeConfig } from '@nuxt/schema'
|
import type { RuntimeConfig } from '@nuxt/schema'
|
||||||
import { getContext } from 'unctx'
|
import { getContext } from 'unctx'
|
||||||
|
import type { SSRContext } from 'vue-bundle-renderer'
|
||||||
|
import type { CompatibilityEvent } from 'h3'
|
||||||
import { legacyPlugin, LegacyContext } from './compat/legacy-app'
|
import { legacyPlugin, LegacyContext } from './compat/legacy-app'
|
||||||
|
|
||||||
const nuxtAppCtx = getContext<NuxtApp>('nuxt-app')
|
const nuxtAppCtx = getContext<NuxtApp>('nuxt-app')
|
||||||
@ -48,11 +50,23 @@ interface _NuxtApp {
|
|||||||
_asyncDataPromises?: Record<string, Promise<any>>
|
_asyncDataPromises?: Record<string, Promise<any>>
|
||||||
_legacyContext?: LegacyContext
|
_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
|
renderMeta?: () => Promise<NuxtMeta> | NuxtMeta
|
||||||
}
|
}
|
||||||
payload: {
|
payload: {
|
||||||
serverRendered?: true
|
serverRendered?: boolean
|
||||||
data?: Record<string, any>
|
data?: Record<string, any>
|
||||||
state?: Record<string, any>
|
state?: Record<string, any>
|
||||||
rendered?: Function
|
rendered?: Function
|
||||||
@ -115,7 +129,7 @@ export function createNuxtApp (options: CreateOptions) {
|
|||||||
|
|
||||||
if (process.server) {
|
if (process.server) {
|
||||||
// Expose to server renderer to create window.__NUXT__
|
// Expose to server renderer to create window.__NUXT__
|
||||||
nuxtApp.ssrContext = nuxtApp.ssrContext || {}
|
nuxtApp.ssrContext = nuxtApp.ssrContext || {} as any
|
||||||
nuxtApp.ssrContext.payload = nuxtApp.payload
|
nuxtApp.ssrContext.payload = nuxtApp.payload
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
import { createRenderer } from 'vue-bundle-renderer'
|
import { createRenderer } from 'vue-bundle-renderer'
|
||||||
import type { SSRContext } from 'vue-bundle-renderer'
|
import { eventHandler, useQuery } from 'h3'
|
||||||
import { CompatibilityEvent, eventHandler, useQuery } from 'h3'
|
|
||||||
import devalue from '@nuxt/devalue'
|
import devalue from '@nuxt/devalue'
|
||||||
import { RuntimeConfig } from '@nuxt/schema'
|
|
||||||
import { renderToString as _renderToString } from 'vue/server-renderer'
|
import { renderToString as _renderToString } from 'vue/server-renderer'
|
||||||
|
|
||||||
|
import type { NuxtApp } from '#app'
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import { useRuntimeConfig } from '#internal/nitro'
|
import { useRuntimeConfig } from '#internal/nitro'
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
@ -12,20 +12,7 @@ import { buildAssetsURL } from '#paths'
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import htmlTemplate from '#build/views/document.template.mjs'
|
import htmlTemplate from '#build/views/document.template.mjs'
|
||||||
|
|
||||||
interface NuxtSSRContext extends SSRContext {
|
type NuxtSSRContext = NuxtApp['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>
|
|
||||||
}
|
|
||||||
|
|
||||||
interface RenderResult {
|
interface RenderResult {
|
||||||
html: any
|
html: any
|
||||||
@ -126,7 +113,6 @@ export default eventHandler(async (event) => {
|
|||||||
runtimeConfig: useRuntimeConfig(),
|
runtimeConfig: useRuntimeConfig(),
|
||||||
noSSR: !!event.req.headers['x-nuxt-no-ssr'],
|
noSSR: !!event.req.headers['x-nuxt-no-ssr'],
|
||||||
error: ssrError,
|
error: ssrError,
|
||||||
redirected: undefined,
|
|
||||||
nuxt: undefined, /* NuxtApp */
|
nuxt: undefined, /* NuxtApp */
|
||||||
payload: undefined
|
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 we error on rendering error page, we bail out and directly return to the error handler
|
||||||
if (!rendered) { return }
|
if (!rendered) { return }
|
||||||
|
|
||||||
if (ssrContext.redirected || event.res.writableEnded) {
|
if (event.res.writableEnded) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user