-
Notifications
You must be signed in to change notification settings - Fork 417
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: detect duplicate context errors (#7160)
* fix: detect duplicate context errors * feat: add missing context help URL --------- Co-authored-by: Rico Kahler <ricokahler@gmail.com>
- Loading branch information
1 parent
f0d5f82
commit 7f3a881
Showing
31 changed files
with
190 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# legacy package exports | ||
_singletons.js | ||
_createContext.js | ||
desk.js | ||
form.js | ||
presentation.js | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
packages/sanity/src/_createContext/createGlobalScopedContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import {type Context, createContext} from 'react' | ||
|
||
import {SANITY_VERSION} from '../core/version' | ||
|
||
const MISSING_CONTEXT_HELP_URL = 'https://www.sanity.io/help/missing-context-error' | ||
|
||
/** | ||
* @internal | ||
* @hidden | ||
*/ | ||
export function createGlobalScopedContext<ContextType, const T extends ContextType = ContextType>( | ||
/** | ||
* It's important to prefix these keys as they are global | ||
*/ | ||
key: `sanity/_singletons/context/${string}`, | ||
defaultValue: T, | ||
): Context<ContextType> { | ||
const symbol = Symbol.for(key) | ||
|
||
/** | ||
* Prevent errors about re-renders on React SSR on Next.js App Router | ||
*/ | ||
if (typeof document === 'undefined') { | ||
return createContext<ContextType>(defaultValue) | ||
} | ||
|
||
if (!globalScope[symbol]) { | ||
globalScope[symbol] = {context: createContext<T>(defaultValue), version: SANITY_VERSION} | ||
} else if (globalScope[symbol].version !== SANITY_VERSION) { | ||
throw new TypeError( | ||
`Duplicate instances of context "${key}" with incompatible versions detected: Expected ${SANITY_VERSION} but got ${globalScope[symbol].version}.\n\n` + | ||
`For more information, please visit ${MISSING_CONTEXT_HELP_URL}`, | ||
) | ||
} else if (!warned.has(SANITY_VERSION)) { | ||
console.warn( | ||
`Duplicate instances of context "${key}" detected. This is likely a mistake and may cause unexpected behavior.\n\n` + | ||
`For more information, please visit ${MISSING_CONTEXT_HELP_URL}`, | ||
) | ||
warned.add(SANITY_VERSION) | ||
} | ||
|
||
return globalScope[symbol].context | ||
} | ||
|
||
const warned = new Set<typeof SANITY_VERSION>() | ||
|
||
/** | ||
* Gets the global scope instance in a given environment. | ||
* | ||
* The strategy is to return the most modern, and if not, the most common: | ||
* - The `globalThis` variable is the modern approach to accessing the global scope | ||
* - The `window` variable is the global scope in a web browser | ||
* - The `self` variable is the global scope in workers and others | ||
* - The `global` variable is the global scope in Node.js | ||
*/ | ||
function getGlobalScope() { | ||
if (typeof globalThis !== 'undefined') return globalThis | ||
if (typeof window !== 'undefined') return window | ||
if (typeof self !== 'undefined') return self | ||
if (typeof global !== 'undefined') return global | ||
|
||
throw new Error('sanity: could not locate global scope') | ||
} | ||
|
||
const globalScope = getGlobalScope() as any |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export {createGlobalScopedContext as createContext} from './createGlobalScopedContext' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from '../_createContext' |
7 changes: 5 additions & 2 deletions
7
packages/sanity/src/_singletons/core/form/FormValueContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import {createContext} from 'react' | ||
import {createContext} from 'sanity/_createContext' | ||
|
||
import type {FormValueContextValue} from '../../../core/form/contexts/FormValue' | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const FormValueContext = createContext<FormValueContextValue | null>(null) | ||
export const FormValueContext = createContext<FormValueContextValue | null>( | ||
'sanity/_singletons/context/form-value', | ||
null, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 5 additions & 2 deletions
7
packages/sanity/src/_singletons/core/store/_legacy/ResourceCacheContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import {createContext} from 'react' | ||
import {createContext} from 'sanity/_createContext' | ||
|
||
import type {ResourceCache} from '../../../../core/store/_legacy/ResourceCacheProvider' | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const ResourceCacheContext = createContext<ResourceCache | null>(null) | ||
export const ResourceCacheContext = createContext<ResourceCache | null>( | ||
'sanity/_singletons/context/resource-cache', | ||
null, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import {createContext} from 'react' | ||
import {createContext} from 'sanity/_createContext' | ||
|
||
import type {Source} from '../../../core/config/types' | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const SourceContext = createContext<Source | null>(null) | ||
export const SourceContext = createContext<Source | null>('sanity/_singletons/context/source', null) |
7 changes: 5 additions & 2 deletions
7
packages/sanity/src/_singletons/core/studio/WorkspaceContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import {createContext} from 'react' | ||
import {createContext} from 'sanity/_createContext' | ||
|
||
import type {Workspace} from '../../../core/config/types' | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const WorkspaceContext = createContext<Workspace | null>(null) | ||
export const WorkspaceContext = createContext<Workspace | null>( | ||
'sanity/_singletons/context/workspace', | ||
null, | ||
) |
7 changes: 5 additions & 2 deletions
7
...anity/src/_singletons/core/studio/activeWorkspaceMatcher/ActiveWorkspaceMatcherContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import {createContext} from 'react' | ||
import {createContext} from 'sanity/_createContext' | ||
|
||
import type {ActiveWorkspaceMatcherContextValue} from '../../../../core/studio/activeWorkspaceMatcher/ActiveWorkspaceMatcherContext' | ||
|
||
/** @internal */ | ||
export const ActiveWorkspaceMatcherContext = | ||
createContext<ActiveWorkspaceMatcherContextValue | null>(null) | ||
createContext<ActiveWorkspaceMatcherContextValue | null>( | ||
'sanity/_singletons/context/active-workspace-matcher', | ||
null, | ||
) |
7 changes: 5 additions & 2 deletions
7
packages/sanity/src/_singletons/core/studio/workspaces/WorkspacesContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
import {createContext} from 'react' | ||
import {createContext} from 'sanity/_createContext' | ||
|
||
import type {WorkspacesContextValue} from '../../../../core/studio/workspaces/WorkspacesContext' | ||
|
||
/** @internal */ | ||
export const WorkspacesContext = createContext<WorkspacesContextValue | null>(null) | ||
export const WorkspacesContext = createContext<WorkspacesContextValue | null>( | ||
'sanity/_singletons/context/workspaces', | ||
null, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import {createContext} from 'react' | ||
import {createContext} from 'sanity/_createContext' | ||
|
||
import type {RouterContextValue} from '../../router/types' | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const RouterContext = createContext<RouterContextValue | null>(null) | ||
export const RouterContext = createContext<RouterContextValue | null>( | ||
'sanity/_singletons/context/router', | ||
null, | ||
) |
7 changes: 5 additions & 2 deletions
7
packages/sanity/src/_singletons/structure/StructureToolContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import {createContext} from 'react' | ||
import {createContext} from 'sanity/_createContext' | ||
|
||
import type {StructureToolContextValue} from '../../structure/types' | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const StructureToolContext = createContext<StructureToolContextValue | null>(null) | ||
export const StructureToolContext = createContext<StructureToolContextValue | null>( | ||
'sanity/_singletons/context/structure-tool', | ||
null, | ||
) |
3 changes: 2 additions & 1 deletion
3
packages/sanity/src/_singletons/structure/comments/intent/CommentsIntentContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import {createContext} from 'react' | ||
import {createContext} from 'sanity/_createContext' | ||
|
||
import type {CommentsIntentContextValue} from '../../../../core/comments/context/intent/types' | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const CommentsIntentContext = createContext<CommentsIntentContextValue | undefined>( | ||
'sanity/_singletons/context/comments-intent', | ||
undefined, | ||
) |
7 changes: 5 additions & 2 deletions
7
packages/sanity/src/_singletons/structure/components/pane/PaneContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import {createContext} from 'react' | ||
import {createContext} from 'sanity/_createContext' | ||
|
||
import type {PaneContextValue} from '../../../../structure/components/pane/types' | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const PaneContext = createContext<PaneContextValue | null>(null) | ||
export const PaneContext = createContext<PaneContextValue | null>( | ||
'sanity/_singletons/context/pane', | ||
null, | ||
) |
7 changes: 5 additions & 2 deletions
7
packages/sanity/src/_singletons/structure/components/pane/PaneLayoutContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import {createContext} from 'react' | ||
import {createContext} from 'sanity/_createContext' | ||
|
||
import type {PaneLayoutContextValue} from '../../../../structure/components/pane/types' | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const PaneLayoutContext = createContext<PaneLayoutContextValue | null>(null) | ||
export const PaneLayoutContext = createContext<PaneLayoutContextValue | null>( | ||
'sanity/_singletons/context/pane-layout', | ||
null, | ||
) |
Oops, something went wrong.