@rsc-kit/core 0.42.0 → 0.42.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/vite.js CHANGED
@@ -1603,6 +1603,57 @@ const MATCH = { ignoreVary: true }
1603
1603
  // of one page, asked for at several depths, fetch its document once.
1604
1604
  const documentsInFlight = new Set()
1605
1605
 
1606
+ // Whether the site moved off this origin - www to the bare domain, an old
1607
+ // domain to a new one - asked when a request failed. The failure is what a
1608
+ // cross-origin redirect looks like from here: a payload fetch follows it to a
1609
+ // host that sends no CORS headers, and rejects exactly as it would offline.
1610
+ // So the url is asked again without following redirects, and a redirect where
1611
+ // a request just failed is the answer. Offline, the probe fails too.
1612
+ //
1613
+ // Asked rather than assumed, because this worker cannot update itself out of
1614
+ // it: the browser refuses a worker script behind a redirect, so once
1615
+ // /sw.js redirects, nothing replaces this worker. It served the old origin's
1616
+ // stored home page from its cache, the boot that followed failed, and the
1617
+ // visitor was shown the offline page on every visit - never the redirect.
1618
+ function moved(request) {
1619
+ return fetch(request.url, { redirect: 'manual', cache: 'no-store', credentials: 'omit' }).then(
1620
+ (response) => response.type === 'opaqueredirect',
1621
+ () => false,
1622
+ )
1623
+ }
1624
+
1625
+ // What the development worker does: the caches go, the worker unregisters,
1626
+ // and each page it controlled is loaded again, uncontrolled, so the
1627
+ // navigation reaches the redirect. Once, however many requests noticed.
1628
+ let leaving = null
1629
+
1630
+ function leave() {
1631
+ leaving ??= (async () => {
1632
+ const keys = await caches.keys()
1633
+ await Promise.all(keys.filter((k) => k.startsWith('rsc-kit-')).map((k) => caches.delete(k)))
1634
+ const pages = await self.clients.matchAll({ type: 'window' })
1635
+ await self.registration.unregister()
1636
+ for (const page of pages) page.navigate(page.url).catch(() => {})
1637
+ })()
1638
+
1639
+ return leaving
1640
+ }
1641
+
1642
+ /** True, and on its way out, when the failed request's url now redirects. */
1643
+ async function leftForMove(event, request) {
1644
+ if (!(await moved(request))) return false
1645
+
1646
+ const going = leave()
1647
+
1648
+ // The event may be over already - a stored page answered from the cache
1649
+ // while its refresh failed behind it - and then there is nothing to extend.
1650
+ try {
1651
+ event.waitUntil(going)
1652
+ } catch {}
1653
+
1654
+ return true
1655
+ }
1656
+
1606
1657
  self.addEventListener('fetch', (event) => {
1607
1658
  const request = event.request
1608
1659
  const url = new URL(request.url)
@@ -1648,6 +1699,7 @@ self.addEventListener('fetch', (event) => {
1648
1699
  return response
1649
1700
  })
1650
1701
  .catch(async (error) => {
1702
+ if (await leftForMove(event, request)) return Response.error()
1651
1703
  if (hit) return hit
1652
1704
 
1653
1705
  // Stored by the build but never visited from this browser, so
@@ -1718,6 +1770,10 @@ self.addEventListener('fetch', (event) => {
1718
1770
  return response
1719
1771
  })
1720
1772
  .catch(async () => {
1773
+ // Before the cache: a copy from the origin the site left is what kept
1774
+ // the visitor there.
1775
+ if (await leftForMove(event, request)) return Response.error()
1776
+
1721
1777
  const hit = await own(keyFor(request), MATCH)
1722
1778
 
1723
1779
  if (hit) return hit