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

CSS HMR fixes #5948

Merged
merged 3 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,16 @@ export function connect({
handleSocketConnected(sendMessage);
break;
default:
handleSocketMessage(msg.data as ServerMessage);
if (Array.isArray(msg.data)) {
for (let i = 0; i < msg.data.length; i++) {
handleSocketMessage(
msg.data[i] as ServerMessage,
i !== msg.data.length - 1
);
}
} else {
handleSocketMessage(msg.data as ServerMessage);
}
break;
}
});
Expand Down Expand Up @@ -499,13 +508,15 @@ export function setHooks(newHooks: typeof hooks) {
Object.assign(hooks, newHooks);
}

function handleSocketMessage(msg: ServerMessage) {
function handleSocketMessage(msg: ServerMessage, aggregate: boolean = false) {
sortIssues(msg.issues);

const hasCriticalIssues = handleIssues(msg);

// TODO(WEB-582) Disable update aggregation for now.
const aggregate = /* hasCriticalIssues */ false;
// if(hasCriticalIssues) {
// aggregate = true;
// }
const aggregatedMsg = aggregateUpdates(msg, aggregate);

if (aggregate) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ async function loadWebAssemblyModule(

const chunkUrl = `/${getChunkRelativeUrl(encodedChunkPath)}`;

const previousLink = document.querySelector(
const previousLinks = document.querySelectorAll(
`link[rel=stylesheet][href^="${chunkUrl}"]`
);

if (previousLink == null) {
if (previousLinks.length == 0) {
reject(new Error(`No link element found for chunk ${chunkPath}`));
return;
}
Expand All @@ -142,10 +142,11 @@ async function loadWebAssemblyModule(
reject();
};
link.onload = () => {
// First load the new CSS, then remove the old one. This prevents visible
// First load the new CSS, then remove the old ones. This prevents visible
// flickering that would happen in-between removing the previous CSS and
// loading the new one.
previousLink.remove();
for (const previousLink of Array.from(previousLinks))
previousLink.remove();

// CSS chunks do not register themselves, and as such must be marked as
// loaded instantly.
Expand All @@ -154,9 +155,9 @@ async function loadWebAssemblyModule(

// Make sure to insert the new CSS right after the previous one, so that
// its precedence is higher.
previousLink.parentElement!.insertBefore(
previousLinks[0].parentElement!.insertBefore(
link,
previousLink.nextSibling
previousLinks[0].nextSibling
);
});
},
Expand Down
Loading