Skip to content
Merged
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
34 changes: 32 additions & 2 deletions packages/vinext/src/shims/script.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,41 @@ function buildBeforeInteractiveScriptProps(options: {
return scriptProps;
}

function setBooleanScriptAttribute(el: HTMLScriptElement, attr: string, value: unknown): boolean {
const enabled = value !== false && value !== "false" && Boolean(value);

switch (attr) {
case "async":
el.async = enabled;
break;
case "defer":
el.defer = enabled;
break;
case "noModule":
case "nomodule":
el.noModule = enabled;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Matching "nomodule" (lowercase) in addition to "noModule" is more permissive than Next.js, which only recognizes noModule through DOMAttributeNames. This is fine — arguably better — but worth noting in case strict parity matters for some future test.

Separately, when the removal branch fires with attr = "nomodule" (lowercase), the el.setAttribute("nomodule", "") call will set the lowercase attribute name. In real browsers setAttribute is case-insensitive for HTML elements, so this works correctly, but it's a subtle difference from Next.js which always uses the camelCase "noModule" for the attribute name.

break;
default:
return false;
}

if (!enabled) {
// Dynamic script elements start in the browser's force-async state.
// Setting and removing the attribute mirrors Next.js and clears that state.
el.setAttribute(attr, "");
el.removeAttribute(attr);
}

return true;
}

function setScriptAttributes(el: HTMLScriptElement, rest: Record<string, unknown>): void {
for (const [attr, value] of Object.entries(rest)) {
if (attr === "dangerouslySetInnerHTML") continue;
if (attr === "className") {
el.setAttribute("class", String(value));
if (value === undefined) continue;
if (setBooleanScriptAttribute(el, attr, value)) continue;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-existing gap (not introduced by this PR): numeric attribute values like data-count={42} are silently dropped because the value falls through both the typeof value === "string" and typeof value === "boolean" checks. Next.js handles this with el.setAttribute(attr, String(value)) for all non-boolean attrs.

Not a blocker for this PR since it's pre-existing, but worth a follow-up issue if you want full parity.

if (attr === "className" && typeof value === "string") {
el.setAttribute("class", value);
} else if (typeof value === "string") {
el.setAttribute(attr, value);
} else if (typeof value === "boolean" && value) {
Expand Down
54 changes: 54 additions & 0 deletions tests/script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,58 @@ describe("Script SSR rendering", () => {
expect(appendedScripts).toHaveLength(1);
expect(appendedScripts[0]!.attrs.nonce).toBe("property-nonce");
});

it("clears forced async execution when async is explicitly false", () => {
type MockScript = {
async: boolean;
attrs: Record<string, string>;
src: string;
setAttribute(name: string, value: string): void;
removeAttribute(name: string): void;
getAttribute(name: string): string | null;
addEventListener(): void;
};

const appendedScripts: MockScript[] = [];
class MockHTMLElement {}

const createdScript: MockScript = {
async: true,
attrs: {},
src: "",
setAttribute(name: string, value: string) {
this.attrs[name] = value;
},
removeAttribute(name: string) {
Reflect.deleteProperty(this.attrs, name);
},
getAttribute(name: string): string | null {
return this.attrs[name] ?? null;
},
addEventListener() {},
};

setGlobalValue("HTMLElement", MockHTMLElement);
setGlobalValue("window", {});
setGlobalValue("document", {
querySelector() {
return null;
},
createElement(tagName: string) {
expect(tagName).toBe("script");
return createdScript;
},
body: {
appendChild(element: unknown) {
appendedScripts.push(element as typeof createdScript);
},
},
});

handleClientScriptLoad({ src: "/ordered-script.js", async: false });

expect(appendedScripts).toHaveLength(1);
expect(appendedScripts[0]!.async).toBe(false);
expect(appendedScripts[0]!.attrs).not.toHaveProperty("async");
});
});
Loading