Skip to content

Commit

Permalink
Merge pull request #15428 from eirslett/fix/multiple-docs-context-in-…
Browse files Browse the repository at this point in the history
…vite

Addon-docs: Cache DocsContext on window to prevent duplication
  • Loading branch information
shilman authored Jul 2, 2021
2 parents a0fc009 + a1d10a5 commit 4eefcdf
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion addons/docs/src/blocks/DocsContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Context, createContext } from 'react';
import { window as globalWindow } from 'global';

export interface DocsContextProps {
id?: string;
Expand All @@ -17,4 +18,15 @@ export interface DocsContextProps {
forceRender?: () => void;
}

export const DocsContext: Context<DocsContextProps> = createContext({});
// We add DocsContext to window. The reason is that in case DocsContext.ts is
// imported multiple times (maybe once directly, and another time from a minified bundle)
// we will have multiple DocsContext definitions - leading to lost context in
// the React component tree.
// This was specifically a problem with the Vite builder.
/* eslint-disable no-underscore-dangle */
if (globalWindow.__DOCS_CONTEXT__ === undefined) {
globalWindow.__DOCS_CONTEXT__ = createContext({});
globalWindow.__DOCS_CONTEXT__.displayName = 'DocsContext';
}

export const DocsContext: Context<DocsContextProps> = globalWindow.__DOCS_CONTEXT__;

0 comments on commit 4eefcdf

Please sign in to comment.