Skip to content

fix: retain currentScript reference when processing async/defer script #718

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

Open
wants to merge 1 commit 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
7 changes: 6 additions & 1 deletion dev/app-react-18/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
<html>
<head>
<meta charset="UTF-8">
<title>app react v17</title>
<title>app react v18</title>
<script
src="./scripts/readCurrentScript.js"
data-appkey="supply"
async="true"
></script>
</head>
<body>
<div id="root"></div>
Expand Down
5 changes: 5 additions & 0 deletions dev/app-react-18/public/scripts/readCurrentScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const currentScript = document.currentScript;
if (!currentScript.dataset.testName) {
throw new Error('currentScript.dataset.testName is required')
}

81 changes: 55 additions & 26 deletions packages/core/src/module/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export class App {
// Environment variables injected by garfish for linkage with child applications
private globalEnvVariables: Record<string, any>;
private deferNodeMap = new Map();
private asyncNodeMap = new Map();
private resolveAsyncProvider: () => void | undefined;
private asyncProvider?:
| interfaces.Provider
Expand Down Expand Up @@ -476,18 +477,42 @@ export class App {
let noEntry = false;
const targetUrl = jsManager.url || this.appInfo.entry;

const mockOriginScript = document.createElement('script');

if (type === 'defer') {
const node = this.deferNodeMap.get(jsManager);
if (node) {
noEntry = toBoolean(
this.entryManager.findAttributeValue(node, 'no-entry'),
);

node.attributes.forEach((attribute) => {
if (attribute.key) {
mockOriginScript.setAttribute(
attribute.key,
attribute.value || '',
);
}
});
}
// Try to read the childApp global configuration
if (!noEntry) {
noEntry = toBoolean(this.isNoEntryScript(targetUrl));
}
}

const node = this.asyncNodeMap.get(jsManager);
if (node) {
node.attributes.forEach((attribute) => {
if (attribute.key) {
mockOriginScript.setAttribute(
attribute.key,
attribute.value || '',
);
}
});
}

this.execScript(jsManager.scriptCode, {}, targetUrl, {
noEntry,
defer: type === 'defer',
Expand Down Expand Up @@ -703,36 +728,40 @@ export class App {
}

const jsManager = resources.js.find((manager) => {
return !manager.async ? manager.isSameOrigin(node) : false;
if (manager.async) {
this.asyncNodeMap.set(manager, node);
return false;
}
if (manager.defer) {
this.deferNodeMap.set(manager, node);
return false;
}
return manager.isSameOrigin(node);
});

if (jsManager) {
if (jsManager.defer) {
this.deferNodeMap.set(jsManager, node);
} else {
const { url, scriptCode } = jsManager;
const mockOriginScript = document.createElement('script');
node.attributes.forEach((attribute) => {
if (attribute.key) {
mockOriginScript.setAttribute(
attribute.key,
attribute.value || '',
);
}
});
const { url, scriptCode } = jsManager;
const mockOriginScript = document.createElement('script');
node.attributes.forEach((attribute) => {
if (attribute.key) {
mockOriginScript.setAttribute(
attribute.key,
attribute.value || '',
);
}
});

const targetUrl = url || this.appInfo.entry;
this.execScript(scriptCode, {}, targetUrl, {
isModule,
async: false,
defer: false,
isInline: jsManager.isInlineScript(),
noEntry:
toBoolean(entryManager.findAttributeValue(node, 'no-entry')) ||
toBoolean(this.isNoEntryScript(targetUrl)),
originScript: mockOriginScript,
});
}
const targetUrl = url || this.appInfo.entry;
this.execScript(scriptCode, {}, targetUrl, {
isModule,
async: false,
defer: false,
isInline: jsManager.isInlineScript(),
noEntry:
toBoolean(entryManager.findAttributeValue(node, 'no-entry')) ||
toBoolean(this.isNoEntryScript(targetUrl)),
originScript: mockOriginScript,
});
} else if (__DEV__) {
const async = entryManager.findAttributeValue(node, 'async');
if (typeof async === 'undefined' || async === 'false') {
Expand Down