feat(ssr): useSSRContext
This commit is contained in:
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.`)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user