]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat(ssr): useSSRContext
authorEvan You <yyx990803@gmail.com>
Tue, 18 Feb 2020 18:26:15 +0000 (13:26 -0500)
committerEvan You <yyx990803@gmail.com>
Tue, 18 Feb 2020 18:26:15 +0000 (13:26 -0500)
packages/runtime-core/src/helpers/useSsrContext.ts [new file with mode: 0644]
packages/runtime-core/src/index.ts
packages/server-renderer/src/renderToString.ts

diff --git a/packages/runtime-core/src/helpers/useSsrContext.ts b/packages/runtime-core/src/helpers/useSsrContext.ts
new file mode 100644 (file)
index 0000000..a83f4c1
--- /dev/null
@@ -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.`)
+  }
+}
index 7a4be08e42385a8341270a9a577dda309a4d1eba..b26c9a8f98732f7f1e781117edfbac81ddb34976 100644 (file)
@@ -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
index 3e6d8056bd34e397e4921dbc01d8135eb530f3ac..2753c3455384c15e110fd89d3aa62d22947932db 100644 (file)
@@ -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<string, unknown>
 
-const ssrContextKey = Symbol()
-
 export type SSRContext = {
   [key: string]: any
   portals?: Record<string, string>