From 4e8f2d3cb355d2d2ecc64e85d5a3779418658a5d Mon Sep 17 00:00:00 2001 From: Alejandro Carrasco Espinoza Date: Fri, 6 Sep 2024 00:04:18 -0400 Subject: [PATCH 1/2] feat: move initial-chunks store variable to window --- packages/component/.size-snapshot.json | 14 +++++++------- packages/component/src/createLoadable.js | 4 ++-- packages/component/src/loadableReady.js | 5 +++-- packages/component/src/shared.js | 10 ++++++++-- 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/packages/component/.size-snapshot.json b/packages/component/.size-snapshot.json index 2f50e41a1..9796464f0 100644 --- a/packages/component/.size-snapshot.json +++ b/packages/component/.size-snapshot.json @@ -1,20 +1,20 @@ { "dist/cjs/loadable.cjs.js": { - "bundled": 16900, - "minified": 7226, - "gzipped": 2553 + "bundled": 17103, + "minified": 7356, + "gzipped": 2583 }, "dist/esm/loadable.esm.mjs": { - "bundled": 16517, - "minified": 6917, - "gzipped": 2490, + "bundled": 16720, + "minified": 7047, + "gzipped": 2520, "treeshaked": { "rollup": { "code": 259, "import_statements": 259 }, "webpack": { - "code": 5764 + "code": 5858 } } } diff --git a/packages/component/src/createLoadable.js b/packages/component/src/createLoadable.js index 5a3da8315..f51d2ae0b 100644 --- a/packages/component/src/createLoadable.js +++ b/packages/component/src/createLoadable.js @@ -3,7 +3,7 @@ import React from 'react' import hoistNonReactStatics from 'hoist-non-react-statics' import { invariant } from './util' import Context from './Context' -import { LOADABLE_SHARED } from './shared' +import { getInitialChunks } from './shared' const STATUS_PENDING = 'PENDING' const STATUS_RESOLVED = 'RESOLVED' @@ -173,7 +173,7 @@ function createLoadable({ ((ctor.isReady && ctor.isReady(props)) || // is ready - was loaded during SSR process (ctor.chunkName && - LOADABLE_SHARED.initialChunks[ctor.chunkName(props)])) + getInitialChunks()[ctor.chunkName(props)])) ) { this.loadSync() } diff --git a/packages/component/src/loadableReady.js b/packages/component/src/loadableReady.js index 11e8dedf4..071254823 100644 --- a/packages/component/src/loadableReady.js +++ b/packages/component/src/loadableReady.js @@ -2,7 +2,7 @@ /* eslint-env browser */ import { warn } from './util' import { getRequiredChunkKey } from './sharedInternals' -import { LOADABLE_SHARED } from './shared' +import { getInitialChunks } from './shared' const BROWSER = typeof window !== 'undefined' @@ -26,8 +26,9 @@ export default function loadableReady( const extElement = document.getElementById(`${id}_ext`) if (extElement) { const { namedChunks } = JSON.parse(extElement.textContent) + const initialChunks = getInitialChunks() namedChunks.forEach(chunkName => { - LOADABLE_SHARED.initialChunks[chunkName] = true + initialChunks[chunkName] = true }) } else { // version mismatch diff --git a/packages/component/src/shared.js b/packages/component/src/shared.js index 0aeda3404..93409605f 100644 --- a/packages/component/src/shared.js +++ b/packages/component/src/shared.js @@ -1,3 +1,9 @@ -export const LOADABLE_SHARED = { - initialChunks: {}, +/* eslint-env browser */ +const INITIAL_CHUNKS_KEY = '__LOADABLE_INITIAL_CHUNKS__' + +export function getInitialChunks() { + if (!window[INITIAL_CHUNKS_KEY]) { + window[INITIAL_CHUNKS_KEY] = {} + } + return window[INITIAL_CHUNKS_KEY] } From 789f338dd008d88302a55f845c14209e9ace8f62 Mon Sep 17 00:00:00 2001 From: Alejandro Carrasco Espinoza Date: Fri, 6 Sep 2024 20:52:08 -0400 Subject: [PATCH 2/2] chore: add server validation --- packages/component/.size-snapshot.json | 14 +++++++------- packages/component/src/loadableReady.js | 4 +--- packages/component/src/shared.js | 7 +++++++ 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/packages/component/.size-snapshot.json b/packages/component/.size-snapshot.json index 9796464f0..920f303ed 100644 --- a/packages/component/.size-snapshot.json +++ b/packages/component/.size-snapshot.json @@ -1,20 +1,20 @@ { "dist/cjs/loadable.cjs.js": { - "bundled": 17103, - "minified": 7356, - "gzipped": 2583 + "bundled": 17192, + "minified": 7413, + "gzipped": 2600 }, "dist/esm/loadable.esm.mjs": { - "bundled": 16720, - "minified": 7047, - "gzipped": 2520, + "bundled": 16809, + "minified": 7104, + "gzipped": 2537, "treeshaked": { "rollup": { "code": 259, "import_statements": 259 }, "webpack": { - "code": 5858 + "code": 5902 } } } diff --git a/packages/component/src/loadableReady.js b/packages/component/src/loadableReady.js index 071254823..d4cd0d9e5 100644 --- a/packages/component/src/loadableReady.js +++ b/packages/component/src/loadableReady.js @@ -2,9 +2,7 @@ /* eslint-env browser */ import { warn } from './util' import { getRequiredChunkKey } from './sharedInternals' -import { getInitialChunks } from './shared' - -const BROWSER = typeof window !== 'undefined' +import { BROWSER, getInitialChunks } from './shared' export default function loadableReady( done = () => {}, diff --git a/packages/component/src/shared.js b/packages/component/src/shared.js index 93409605f..3d5dbb635 100644 --- a/packages/component/src/shared.js +++ b/packages/component/src/shared.js @@ -1,7 +1,14 @@ /* eslint-env browser */ const INITIAL_CHUNKS_KEY = '__LOADABLE_INITIAL_CHUNKS__' +const SERVER_INITIAL_CHUNKS = {} + +export const BROWSER = typeof window !== 'undefined' + export function getInitialChunks() { + if (!BROWSER) { + return SERVER_INITIAL_CHUNKS + } if (!window[INITIAL_CHUNKS_KEY]) { window[INITIAL_CHUNKS_KEY] = {} }