Skip to content

Commit fd03149

Browse files
committed
feat(ssr): useSSRContext
1 parent 86464e8 commit fd03149

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { inject } from '../apiInject'
2+
import { warn } from '../warning'
3+
4+
export const ssrContextKey = Symbol(__DEV__ ? `ssrContext` : ``)
5+
6+
export const useSSRContext = <T = Record<string, any>>() => {
7+
if (!__GLOBAL__) {
8+
const ctx = inject<T>(ssrContextKey)
9+
if (!ctx) {
10+
warn(
11+
`Server rendering context not provided. Make sure to only call ` +
12+
`useSsrContext() conditionally in the server build.`
13+
)
14+
}
15+
return ctx
16+
} else if (__DEV__) {
17+
warn(`useSsrContext() is not supported in the global build.`)
18+
}
19+
}

packages/runtime-core/src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ export {
6161
// SFC CSS Modules
6262
export { useCSSModule } from './helpers/useCssModule'
6363

64+
// SSR context
65+
export { useSSRContext, ssrContextKey } from './helpers/useSsrContext'
66+
6467
// Internal API ----------------------------------------------------------------
6568

6669
// For custom renderers

packages/server-renderer/src/renderToString.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
ssrUtils,
1212
Slots,
1313
warn,
14-
createApp
14+
createApp,
15+
ssrContextKey
1516
} from 'vue'
1617
import {
1718
ShapeFlags,
@@ -52,8 +53,6 @@ export type PushFn = (item: SSRBufferItem) => void
5253

5354
export type Props = Record<string, unknown>
5455

55-
const ssrContextKey = Symbol()
56-
5756
export type SSRContext = {
5857
[key: string]: any
5958
portals?: Record<string, string>

0 commit comments

Comments
 (0)