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

Move all caching to the install phase of the service worker #12

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ An in-depth comparison of all rendering methods can be found on this project's _
- [Splitting Async Vendors](#splitting-async-vendors)
- [Preloading Data](#preloading-data)
- [Precaching Async Pages](#precaching-async-pages)
- [Dynamic Source Inlining](#dynamic-source-inlining)
- [Adaptive Source Inlining](#adaptive-source-inlining)
- [Tweaking Further](#tweaking-further)
- [Transitioning Async Pages](#transitioning-async-pages)
- [Preloading Other Pages Data](#preloading-other-pages-data)
Expand Down Expand Up @@ -686,7 +686,7 @@ Before going on to the next major change, here is the backup branch containing e
<br>
https://github.com/theninthsky/client-side-rendering/tree/before-dynamic-source-inlining

## Dynamic Source Inlining
## Adaptive Source Inlining

When inspecting our 43kb `react-dom.js` file, we can see that the time it took for the request to return was 60ms while the time it took to download the file was 3ms:

Expand Down
25 changes: 16 additions & 9 deletions public/precache-service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ const CACHE_NAME = 'my-csr-app'

const allAssets = self.__WB_MANIFEST.map(({ url }) => url)

let cacheAssetsPromiseResolve
const cacheAssetsPromise = new Promise(resolve => (cacheAssetsPromiseResolve = resolve))

const getCache = () => caches.open(CACHE_NAME)

const getCachedAssets = async cache => {
Expand Down Expand Up @@ -33,8 +36,8 @@ const precacheAssets = async ({ ignoreAssets }) => {
const assetsToPrecache = allAssets.filter(asset => !cachedAssets.includes(asset) && !ignoreAssets.includes(asset))

await cache.addAll(assetsToPrecache)

fetchDocument('/')
await removeUnusedAssets()
await fetchDocument('/')
}

const removeUnusedAssets = async () => {
Expand Down Expand Up @@ -74,16 +77,20 @@ const handleFetch = async request => {
return cachedResponse || fetch(request)
}

self.addEventListener('install', () => self.skipWaiting())
self.addEventListener('install', event => {
event.waitUntil(cacheAssetsPromise)
self.skipWaiting()
})

self.addEventListener('message', event => {
self.addEventListener('message', async event => {
const { type, inlineAssets } = event.data

if (type === 'cache-assets') return cacheInlineAssets(inlineAssets)
if (type === 'precache-assets') {
precacheAssets({ ignoreAssets: inlineAssets.map(({ url }) => url) })
removeUnusedAssets()
}
if (type !== 'cache-assets') return

await cacheInlineAssets(inlineAssets)
await precacheAssets({ ignoreAssets: inlineAssets.map(({ url }) => url) })

cacheAssetsPromiseResolve()
})

self.addEventListener('fetch', async event => {
Expand Down
14 changes: 1 addition & 13 deletions src/utils/service-worker-registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,8 @@ const register = () => {

console.log('Service worker registered!')

const inlineAssets = extractInlineScripts()

if (inlineAssets.length) {
navigator.serviceWorker.ready.then(registration => {
registration.active?.postMessage({ type: 'cache-assets', inlineAssets })
})
}

registration.addEventListener('updatefound', () => {
registration.installing!.onstatechange = (event: Event) => {
const serviceWorker = event.target as ServiceWorker

if (serviceWorker.state === 'activated') serviceWorker.postMessage({ type: 'precache-assets', inlineAssets })
}
registration.installing?.postMessage({ type: 'cache-assets', inlineAssets: extractInlineScripts() })
})

setInterval(() => registration.update(), ACTIVE_REVALIDATION_INTERVAL * 1000)
Expand Down