UNPKG

sanity

Version:

Sanity is a real-time content infrastructure with a scalable, hosted backend featuring a Graph Oriented Query Language (GROQ), asset pipelines and fast edge caches

1 lines 4.11 kB
{"version":3,"file":"_createContext.mjs","sources":["../src/_createContext/createGlobalScopedContext.ts"],"sourcesContent":["import {type Context, createContext} from 'react'\n\nimport {SANITY_VERSION} from '../core/version'\n\nconst MISSING_CONTEXT_HELP_URL = 'https://www.sanity.io/help/missing-context-error'\n\n/**\n * @internal\n * @hidden\n */\nexport function createGlobalScopedContext<ContextType, const T extends ContextType = ContextType>(\n /**\n * It's important to prefix these keys as they are global\n */\n key: `sanity/_singletons/context/${string}`,\n defaultValue: T,\n): Context<ContextType> {\n const symbol = Symbol.for(key)\n\n /**\n * Prevent errors about re-renders on React SSR on Next.js App Router, as well as JSDOM-based\n * environments such as when we extract schemas etc from the studio configuration.\n */\n if (\n typeof document === 'undefined' ||\n (typeof window !== 'undefined' &&\n typeof window.navigator !== 'undefined' &&\n window.navigator.userAgent.includes('jsdom'))\n ) {\n const context = createContext<ContextType>(defaultValue)\n context.displayName = key\n return context\n }\n\n if (!globalScope[symbol]) {\n globalScope[symbol] = {context: createContext<T>(defaultValue), version: SANITY_VERSION}\n } else if (globalScope[symbol].version !== SANITY_VERSION) {\n throw new TypeError(\n `Duplicate instances of context \"${key}\" with incompatible versions detected: Expected ${SANITY_VERSION} but got ${globalScope[symbol].version}.\\n\\n` +\n `For more information, please visit ${MISSING_CONTEXT_HELP_URL}`,\n )\n } else if (!warned.has(SANITY_VERSION)) {\n console.warn(\n `Duplicate instances of context \"${key}\" detected. This is likely a mistake and may cause unexpected behavior.\\n\\n` +\n `For more information, please visit ${MISSING_CONTEXT_HELP_URL}`,\n )\n warned.add(SANITY_VERSION)\n }\n\n return globalScope[symbol].context\n}\n\nconst warned = new Set<typeof SANITY_VERSION>()\n\n/**\n * Gets the global scope instance in a given environment.\n *\n * The strategy is to return the most modern, and if not, the most common:\n * - The `globalThis` variable is the modern approach to accessing the global scope\n * - The `window` variable is the global scope in a web browser\n * - The `self` variable is the global scope in workers and others\n * - The `global` variable is the global scope in Node.js\n */\nfunction getGlobalScope() {\n if (typeof globalThis !== 'undefined') return globalThis\n if (typeof window !== 'undefined') return window\n if (typeof self !== 'undefined') return self\n if (typeof global !== 'undefined') return global\n\n throw new Error('sanity: could not locate global scope')\n}\n\nconst globalScope = getGlobalScope() as any\n"],"names":["MISSING_CONTEXT_HELP_URL","createGlobalScopedContext","key","defaultValue","symbol","Symbol","for","document","window","navigator","userAgent","includes","context","createContext","displayName","globalScope","version","SANITY_VERSION","TypeError","warned","has","console","warn","add","Set","getGlobalScope","globalThis","self","global","Error"],"mappings":";;AAIA,MAAMA,2BAA2B;AAM1B,SAASC,0BAIdC,KACAC,cACsB;AACtB,QAAMC,SAASC,OAAOC,IAAIJ,GAAG;AAM7B,MACE,OAAOK,WAAa,OACnB,OAAOC,SAAW,OACjB,OAAOA,OAAOC,YAAc,OAC5BD,OAAOC,UAAUC,UAAUC,SAAS,OAAO,GAC7C;AACA,UAAMC,UAAUC,cAA2BV,YAAY;AACvDS,WAAAA,QAAQE,cAAcZ,KACfU;AAAAA,EACT;AAEA,MAAI,CAACG,YAAYX,MAAM;AACrBW,gBAAYX,MAAM,IAAI;AAAA,MAACQ,SAASC,cAAiBV,YAAY;AAAA,MAAGa,SAASC;AAAAA,IAAAA;AAAAA,OACpE;AAAA,QAAIF,YAAYX,MAAM,EAAEY,YAAYC;AACzC,YAAM,IAAIC,UACR,mCAAmChB,GAAG,mDAAmDe,cAAc,YAAYF,YAAYX,MAAM,EAAEY,OAAO;AAAA;AAAA,qCACtGhB,wBAAwB,EAClE;AACUmB,WAAOC,IAAIH,cAAc,MACnCI,QAAQC,KACN,mCAAmCpB,GAAG;AAAA;AAAA,qCACEF,wBAAwB,EAClE,GACAmB,OAAOI,IAAIN,cAAc;AAAA,EAAA;AAG3B,SAAOF,YAAYX,MAAM,EAAEQ;AAC7B;AAEA,MAAMO,6BAAaK,IAAAA;AAWnB,SAASC,iBAAiB;AACxB,MAAI,OAAOC,aAAe,IAAa,QAAOA;AAC9C,MAAI,OAAOlB,SAAW,IAAa,QAAOA;AAC1C,MAAI,OAAOmB,OAAS,IAAa,QAAOA;AACxC,MAAI,OAAOC,SAAW,IAAa,QAAOA;AAE1C,QAAM,IAAIC,MAAM,uCAAuC;AACzD;AAEA,MAAMd,cAAcU,eAAAA;"}