feat(ssr): useSSRContext
This commit is contained in:
parent
86464e8c04
commit
fd031490fb
19
packages/runtime-core/src/helpers/useSsrContext.ts
Normal file
19
packages/runtime-core/src/helpers/useSsrContext.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import { inject } from '../apiInject'
|
||||||
|
import { warn } from '../warning'
|
||||||
|
|
||||||
|
export const ssrContextKey = Symbol(__DEV__ ? `ssrContext` : ``)
|
||||||
|
|
||||||
|
export const useSSRContext = <T = Record<string, any>>() => {
|
||||||
|
if (!__GLOBAL__) {
|
||||||
|
const ctx = inject<T>(ssrContextKey)
|
||||||
|
if (!ctx) {
|
||||||
|
warn(
|
||||||
|
`Server rendering context not provided. Make sure to only call ` +
|
||||||
|
`useSsrContext() conditionally in the server build.`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return ctx
|
||||||
|
} else if (__DEV__) {
|
||||||
|
warn(`useSsrContext() is not supported in the global build.`)
|
||||||
|
}
|
||||||
|
}
|
@ -61,6 +61,9 @@ export {
|
|||||||
// SFC CSS Modules
|
// SFC CSS Modules
|
||||||
export { useCSSModule } from './helpers/useCssModule'
|
export { useCSSModule } from './helpers/useCssModule'
|
||||||
|
|
||||||
|
// SSR context
|
||||||
|
export { useSSRContext, ssrContextKey } from './helpers/useSsrContext'
|
||||||
|
|
||||||
// Internal API ----------------------------------------------------------------
|
// Internal API ----------------------------------------------------------------
|
||||||
|
|
||||||
// For custom renderers
|
// For custom renderers
|
||||||
|
@ -11,7 +11,8 @@ import {
|
|||||||
ssrUtils,
|
ssrUtils,
|
||||||
Slots,
|
Slots,
|
||||||
warn,
|
warn,
|
||||||
createApp
|
createApp,
|
||||||
|
ssrContextKey
|
||||||
} from 'vue'
|
} from 'vue'
|
||||||
import {
|
import {
|
||||||
ShapeFlags,
|
ShapeFlags,
|
||||||
@ -52,8 +53,6 @@ export type PushFn = (item: SSRBufferItem) => void
|
|||||||
|
|
||||||
export type Props = Record<string, unknown>
|
export type Props = Record<string, unknown>
|
||||||
|
|
||||||
const ssrContextKey = Symbol()
|
|
||||||
|
|
||||||
export type SSRContext = {
|
export type SSRContext = {
|
||||||
[key: string]: any
|
[key: string]: any
|
||||||
portals?: Record<string, string>
|
portals?: Record<string, string>
|
||||||
|
Loading…
Reference in New Issue
Block a user