useSsrContext.ts 554 B

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