From: Evan You Date: Tue, 18 Feb 2020 18:26:15 +0000 (-0500) Subject: feat(ssr): useSSRContext X-Git-Tag: v3.0.0-alpha.5~7 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=fd031490fb89b7c0d1d478b586151a24324101a3;p=thirdparty%2Fvuejs%2Fcore.git feat(ssr): useSSRContext --- diff --git a/packages/runtime-core/src/helpers/useSsrContext.ts b/packages/runtime-core/src/helpers/useSsrContext.ts new file mode 100644 index 0000000000..a83f4c10ec --- /dev/null +++ b/packages/runtime-core/src/helpers/useSsrContext.ts @@ -0,0 +1,19 @@ +import { inject } from '../apiInject' +import { warn } from '../warning' + +export const ssrContextKey = Symbol(__DEV__ ? `ssrContext` : ``) + +export const useSSRContext = >() => { + if (!__GLOBAL__) { + const ctx = inject(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.`) + } +} diff --git a/packages/runtime-core/src/index.ts b/packages/runtime-core/src/index.ts index 7a4be08e42..b26c9a8f98 100644 --- a/packages/runtime-core/src/index.ts +++ b/packages/runtime-core/src/index.ts @@ -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 diff --git a/packages/server-renderer/src/renderToString.ts b/packages/server-renderer/src/renderToString.ts index 3e6d8056bd..2753c34553 100644 --- a/packages/server-renderer/src/renderToString.ts +++ b/packages/server-renderer/src/renderToString.ts @@ -11,7 +11,8 @@ import { ssrUtils, Slots, warn, - createApp + createApp, + ssrContextKey } from 'vue' import { ShapeFlags, @@ -52,8 +53,6 @@ export type PushFn = (item: SSRBufferItem) => void export type Props = Record -const ssrContextKey = Symbol() - export type SSRContext = { [key: string]: any portals?: Record