Skip to content

Commit

Permalink
Fix workerd HMR (#2019)
Browse files Browse the repository at this point in the history
* Handle HMR updates synchronously in the worker

* Changesets
  • Loading branch information
frandiox committed Apr 19, 2024
1 parent 59ff672 commit a335afc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/three-windows-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/mini-oxygen': patch
---

Fix an SSR HMR issue related to hanging promises during development.
24 changes: 17 additions & 7 deletions packages/mini-oxygen/src/vite/worker-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,24 @@ function fetchEntryModule(publicUrl: URL, env: ViteEnv) {
hmrReady = !!hmrWs;
hmrWs?.addEventListener('message', (message) => {
if (onHmrRecieve) {
let data: HMRPayload = JSON.parse(message.data?.toString());

if (data?.type === 'update') {
// TODO: handle partial updates
data = {type: 'full-reload', path: '*'};
if (!message.data) return;
const data: HMRPayload = JSON.parse(message.data.toString());

if (!data) return;

if (data.type === 'update') {
// Invalidate cache synchronously without revalidating the
// module to avoid hanging promises in workerd
for (const update of data.updates) {
runtime.moduleCache.invalidateDepTree([update.path]);
}
} else if (data.type !== 'custom') {
// Custom events are only used in browser HMR, so ignore them.
// This type is wrong in ViteRuntime:
(onHmrRecieve(data) as unknown as Promise<unknown>)?.catch(
(error) => console.error('During SSR HMR:', error),
);
}

onHmrRecieve(data);
}
});
})
Expand Down

0 comments on commit a335afc

Please sign in to comment.