Appearance
What a service worker must pass through so sign-in still works when a session expires.
A site behind BehindGate sign-in can register a service worker like any other site. There is one rule to follow, and getting it wrong is invisible until a visitor's session expires.
The rule
Pass the original request through. Do not rebuild it.
js
self.addEventListener("fetch", (event) => {
event.respondWith(fetch(event.request));
});These two look equivalent and are not:
js
// Broken: a new request, with none of the original's context.
event.respondWith(fetch(event.request.url));
// Broken: copies the headers, but not what the request is for.
event.respondWith(fetch(new Request(event.request)));Why it matters
The browser tags every request with what it is for, a page the visitor is navigating to or an image the page needs, and with what the response may be. BehindGate reads those tags to decide what to do when a session has expired: send a page load to the sign-in form, and refuse anything else outright.
Rebuilding the request throws the tags away, and script cannot put the navigation tag back. Every page load then looks like a stray background request, and gets refused as one.
The symptom
The site works normally for as long as the session lasts. When it expires, every page answers 403 at once, including the site root, and there is no way to reach the sign-in form. It looks like a sign-in outage. It is the worker.
To confirm it, open the site in a private window, or unregister the worker under your browser's application tools and reload. If the sign-in page appears, the worker is the cause.
Why the two rebuilds differ
Both break the sign-in path, but not in the same way, and the difference is worth knowing when you are reading a bug report.
new Request(event.request) keeps the original's redirect handling, so the browser still resolves a redirect itself.
fetch(event.request.url) does not. It follows redirects on your behalf and hands back a response the browser marks as redirected. A page load may not be answered with one of those, so the browser shows a network error instead of the page. BehindGate avoids handing that worker a redirect at all, but a redirect from anywhere else in your own stack will still hit it.
Caching
Cache static assets. Let navigations go to the network.
A cached page served to a visitor whose session has ended shows them content they are no longer entitled to, and hides the sign-in redirect that should have replaced it. Keep gated pages out of the cache, or check the network first and fall back to the cache only when offline.
Calling an API from the page
An app that fetches its own data should say what it parses:
js
const res = await fetch("/api/thing", {
credentials: "same-origin",
headers: { Accept: "application/json" },
});With the header, an expired session comes back as a clean failure your code can handle. Without it, the request is treated as a page load and follows the redirect to the sign-in form, so res.ok is true and the body is HTML.