Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move initialChunks store variable to window global variable #1013

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions packages/component/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"dist/cjs/loadable.cjs.js": {
"bundled": 16900,
"minified": 7226,
"gzipped": 2553
"bundled": 17192,
"minified": 7413,
"gzipped": 2600
},
"dist/esm/loadable.esm.mjs": {
"bundled": 16517,
"minified": 6917,
"gzipped": 2490,
"bundled": 16809,
"minified": 7104,
"gzipped": 2537,
"treeshaked": {
"rollup": {
"code": 259,
"import_statements": 259
},
"webpack": {
"code": 5764
"code": 5902
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/component/src/createLoadable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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()
}
Expand Down
7 changes: 3 additions & 4 deletions packages/component/src/loadableReady.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
/* eslint-env browser */
import { warn } from './util'
import { getRequiredChunkKey } from './sharedInternals'
import { LOADABLE_SHARED } from './shared'

const BROWSER = typeof window !== 'undefined'
import { BROWSER, getInitialChunks } from './shared'

export default function loadableReady(
done = () => {},
Expand All @@ -26,8 +24,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
Expand Down
17 changes: 15 additions & 2 deletions packages/component/src/shared.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
export const LOADABLE_SHARED = {
initialChunks: {},
/* 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] = {}
}
return window[INITIAL_CHUNKS_KEY]
}