Skip to content

Commit

Permalink
CSS HMR fixes (vercel/turborepo#5948)
Browse files Browse the repository at this point in the history
### Description

handle multiple hmr events at the same time
fix a race condition when updating CSS too fast


Closes WEB-1556
  • Loading branch information
sokra committed Sep 14, 2023
1 parent c1d2c0a commit 7e46099
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7e46099

Please sign in to comment.