Skip to content

Commit

Permalink
feat(ssr): useSSRContext
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 18, 2020
1 parent 86464e8 commit fd03149
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
19 changes: 19 additions & 0 deletions packages/runtime-core/src/helpers/useSsrContext.ts
Original file line number Diff line number Diff line change
@@ -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.`)
}
}
3 changes: 3 additions & 0 deletions packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ export {
// SFC CSS Modules
export { useCSSModule } from './helpers/useCssModule'

// SSR context
export { useSSRContext, ssrContextKey } from './helpers/useSsrContext'

// Internal API ----------------------------------------------------------------

// For custom renderers
Expand Down
5 changes: 2 additions & 3 deletions packages/server-renderer/src/renderToString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
ssrUtils,
Slots,
warn,
createApp
createApp,
ssrContextKey
} from 'vue'
import {
ShapeFlags,
Expand Down Expand Up @@ -52,8 +53,6 @@ export type PushFn = (item: SSRBufferItem) => void

export type Props = Record<string, unknown>

const ssrContextKey = Symbol()

export type SSRContext = {
[key: string]: any
portals?: Record<string, string>
Expand Down

0 comments on commit fd03149

Please sign in to comment.