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

fix(sandpack-sw): retrieve current SW & invalidate relay port on reload #8522

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix(sandpack-sw): retrieve current SW
  • Loading branch information
danilowoz committed Jul 24, 2024
commit 2faa317d8a96ffeee40d5a2c05472802f379782b
24 changes: 12 additions & 12 deletions packages/app/src/sandbox/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,18 @@ export async function startServiceWorker() {
);
preventStaleTermination(worker);

if (process.env.NODE_ENV === 'development') {
window.addEventListener('beforeunload', async () => {
const registrations = await navigator.serviceWorker.getRegistrations();

for (const registration of registrations) {
// eslint-disable-next-line no-await-in-loop
await registration.unregister();
}

debug('[relay] Unregister all SW');
});
}
// if (process.env.NODE_ENV === 'development') {
// window.addEventListener('beforeunload', async () => {
// const registrations = await navigator.serviceWorker.getRegistrations();

// for (const registration of registrations) {
// // eslint-disable-next-line no-await-in-loop
// await registration.unregister();
// }

// debug('[relay] Unregister all SW');
// });
// }

workerReadyPromise.resolve(worker);

Expand Down
1 change: 1 addition & 0 deletions packages/app/src/sandbox/worker/sw.no-webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ self.addEventListener('message', async event => {
const message = event.data;
switch (message.$type) {
case 'worker/init': {
console.log('worker/init');
const nextRelayPort = event.ports[0];

invariant(
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/sandbox/worker/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function getServiceWorker(): Promise<ServiceWorker | null> {
const worker = getWorkerInstance(registration);

// Unregister any worker that shouldn't be there.
if (worker && worker.scriptURL !== workerUrl) {
if (worker && new URL(worker.scriptURL).pathname !== workerUrl) {
debug(
'[sw:register] found irrelevant worker registration, unregistering...',
worker,
Expand Down Expand Up @@ -67,7 +67,7 @@ export async function getServiceWorker(): Promise<ServiceWorker | null> {

// If the controller has the same script as the expected worker,
// this means the correct worker is already handling the page.
if (controller.scriptURL === workerUrl) {
if (new URL(controller.scriptURL).pathname === workerUrl) {
debug(
'[sw:register] relay is controlled by the correct worker',
controller.scriptURL
Expand Down
Loading