Skip to content

Conversation

SgtPooki
Copy link
Member

@SgtPooki SgtPooki commented Sep 5, 2025

  • fix: hard refresh uses service-worker
  • test: remove unused FrameLocator types
  • fix: registrationTTL unregisters sw after returning response

Title

fix: refresh and ttl edgecases

Description

Fixes #822

Notes & open questions

I still think we need to work towards removing the registrationTTL, and work on implementing badbits. I threw some more thoughts down at #840.

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation if necessary (this includes comments as well)
  • I have added tests that prove my fix is effective or that my feature works

@SgtPooki SgtPooki linked an issue Sep 5, 2025 that may be closed by this pull request
Copy link
Member Author

@SgtPooki SgtPooki left a comment

Choose a reason for hiding this comment

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

self review

src/index.tsx Outdated
return
}
// user is requesting content addressed data and has the config already, render the UI
await renderUi()
Copy link
Member Author

Choose a reason for hiding this comment

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

we should likely never need to do this.. I think we can remove it. if they have the config and it's a request for contentaddresseddata (pathRequest|subdomainRequest), then we should be calling ensureSw and loadServiceWorker and reloading..? if this main page JS is running, and we are requesting content, and the service worker is registered, it should have captured the request.

we need to address this

@@ -1,10 +1,7 @@
import type { FrameLocator, Locator, Page } from '@playwright/test'
Copy link
Member Author

Choose a reason for hiding this comment

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

some cleanup

Comment on lines -204 to -208
if (!isServiceWorkerRegistrationTTLValid()) {
log.trace('Service worker registration TTL expired, unregistering service worker')
event.waitUntil(self.registration.unregister())
return false
} else if (isUnregisterRequest(event.request.url)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

moved to after the request to prevent weird network failure issues because sw unregisters in the middle of a response

Comment on lines +176 to +179
const hasActiveWorker = registration?.active != null
const hasControllingWorker = navigator.serviceWorker.controller != null

if (hasActiveWorker && !hasControllingWorker) {
Copy link
Member Author

Choose a reason for hiding this comment

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

in chrome & firefox, hasActiveWorker will be true and hasControllingWorker will be false after a hard refresh.

in playwright-firefox, this is not true. it may be an out of date browser runtime?

Copy link
Member

@lidel lidel left a comment

Choose a reason for hiding this comment

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

thanks, lets do whatever we need to fix #822 asap.

if you feel this is the way, sgtm, but i'm also ok with removing timebomb entirely if that makes things easier / less brittle

src/index.tsx Outdated
Comment on lines 41 to 42
window.location.reload()
return
Copy link
Member

Choose a reason for hiding this comment

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

💭 not sure how real risk is, but to avoid infinite reload loop we may want to do something like

// Prevent reload loops
const reloadKey = 'sw-hard-refresh-reload'
if (sessionStorage.getItem(reloadKey)) {
  sessionStorage.removeItem(reloadKey)
  // Already reloaded once, don't loop
  return
}
sessionStorage.setItem(reloadKey, '1')
  window.location.reload()
  return

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think there is a risk because one flag is whether or not the service worker is registered. isHardRefresh is only true if there is an active worker, and the controller for that worker is null.. a reload of the page means the service worker (that we already confirmed exists) will capture it

@SgtPooki SgtPooki merged commit 6a532c6 into main Sep 18, 2025
17 checks passed
@SgtPooki SgtPooki deleted the 822-indexhtml-shows-on-subdomain-on-ctrlshiftr branch September 18, 2025 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

index.html shows on subdomain on ctrl+shift+r
2 participants