feat(pwa): update precache version with build_id

This commit is contained in:
Julien Oculi 2024-07-17 13:08:36 +02:00
parent 087843c800
commit 654bc40358

View file

@ -1,11 +1,17 @@
import { expandGlob } from '$std/fs/mod.ts'
import { SessionHandlers } from ':src/session/mod.ts'
import { respondApi } from ':src/utils.ts'
import { BUILD_ID } from '$fresh/src/server/build_id.ts'
import { encodeBase64 } from '@std/encoding'
export type PrecacheResponse = { version: string; preCachedUrls: string[] }
// Updated only at server start
const version = crypto.randomUUID()
async function getVersion() {
const versionRaw = crypto.randomUUID() + BUILD_ID
const versionBytes = new TextEncoder().encode(versionRaw)
const versionHash = await crypto.subtle.digest('SHA-256', versionBytes)
return encodeBase64(versionHash)
}
export const handler: SessionHandlers = {
async GET() {
@ -13,6 +19,7 @@ export const handler: SessionHandlers = {
const preCachedUrls: string[] = ['/', '/imports/markdown_css']
const paths = ['/static/**', '/_fresh/static/**']
const routes = '/routes/*/index.tsx'
const version = await getVersion()
//Pre-cache routes
for await (const route of expandGlob(routes, { root: '.' })) {