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

fix: prevent to load script again #756

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
79 changes: 51 additions & 28 deletions packages/repack/src/modules/ScriptManager/ScriptManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export class ScriptManager extends EventEmitter {
}

protected cache: Cache = {};
protected scriptsPromises: Record<string, Promise<void> | undefined> = {};
protected cacheInitialized = false;
protected resolvers: [number, ScriptLocatorResolver][] = [];
protected storage?: StorageApi;
Expand Down Expand Up @@ -312,21 +313,30 @@ export class ScriptManager extends EventEmitter {
caller?: string,
webpackContext = getWebpackContext()
) {
let script = await this.resolveScript(scriptId, caller, webpackContext);

try {
this.emit('loading', script.toObject());
await this.nativeScriptManager.loadScript(scriptId, script.locator);
this.emit('loaded', script.toObject());
} catch (error) {
const { code } = error as Error & { code: string };
this.handleError(
error,
'[ScriptManager] Failed to load script:',
code ? `[${code}]` : '',
script.toObject()
);
const uniqueId = Script.getScriptUniqueId(scriptId, caller);
if (this.scriptsPromises[uniqueId]) {
await this.scriptsPromises[uniqueId];
}
const loadProcess = async () => {
let script = await this.resolveScript(scriptId, caller, webpackContext);

try {
this.emit('loading', script.toObject());
await this.nativeScriptManager.loadScript(scriptId, script.locator);
this.emit('loaded', script.toObject());
} catch (error) {
const { code } = error as Error & { code: string };
this.handleError(
error,
'[ScriptManager] Failed to load script:',
code ? `[${code}]` : '',
script.toObject()
);
}
};

this.scriptsPromises[uniqueId] = loadProcess();
await this.scriptsPromises[uniqueId];
}

/**
Expand All @@ -344,20 +354,30 @@ export class ScriptManager extends EventEmitter {
caller?: string,
webpackContext = getWebpackContext()
) {
let script = await this.resolveScript(scriptId, caller, webpackContext);

try {
this.emit('prefetching', script.toObject());
await this.nativeScriptManager.prefetchScript(scriptId, script.locator);
} catch (error) {
const { code } = error as Error & { code: string };
this.handleError(
error,
'[ScriptManager] Failed to prefetch script:',
code ? `[${code}]` : '',
script.toObject()
);
const uniqueId = Script.getScriptUniqueId(scriptId, caller);
if (this.scriptsPromises[uniqueId]) {
await this.scriptsPromises[uniqueId];
}
const loadProcess = async () => {
let script = await this.resolveScript(scriptId, caller, webpackContext);

try {
this.emit('prefetching', script.toObject());
await this.nativeScriptManager.prefetchScript(scriptId, script.locator);
} catch (error) {
const { code } = error as Error & { code: string };
this.handleError(
error,
'[ScriptManager] Failed to prefetch script:',
code ? `[${code}]` : '',
script.toObject()
);
}
};

this.scriptsPromises[uniqueId] = loadProcess();

await this.scriptsPromises[uniqueId];
}

/**
Expand All @@ -376,7 +396,10 @@ export class ScriptManager extends EventEmitter {
await this.initCache();

const ids = scriptIds.length ? scriptIds : Object.keys(this.cache);
ids.forEach((scriptId) => delete this.cache[scriptId]);
ids.forEach((scriptId) => {
delete this.cache[scriptId];
delete this.scriptsPromises[scriptId];
});

await this.saveCache();
await this.nativeScriptManager.invalidateScripts(scriptIds);
Expand Down