feat(pwa): ✨ update precache version with build_id
This commit is contained in:
parent
087843c800
commit
654bc40358
|
@ -1,11 +1,17 @@
|
||||||
import { expandGlob } from '$std/fs/mod.ts'
|
import { expandGlob } from '$std/fs/mod.ts'
|
||||||
import { SessionHandlers } from ':src/session/mod.ts'
|
import { SessionHandlers } from ':src/session/mod.ts'
|
||||||
import { respondApi } from ':src/utils.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[] }
|
export type PrecacheResponse = { version: string; preCachedUrls: string[] }
|
||||||
|
|
||||||
// Updated only at server start
|
async function getVersion() {
|
||||||
const version = crypto.randomUUID()
|
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 = {
|
export const handler: SessionHandlers = {
|
||||||
async GET() {
|
async GET() {
|
||||||
|
@ -13,6 +19,7 @@ export const handler: SessionHandlers = {
|
||||||
const preCachedUrls: string[] = ['/', '/imports/markdown_css']
|
const preCachedUrls: string[] = ['/', '/imports/markdown_css']
|
||||||
const paths = ['/static/**', '/_fresh/static/**']
|
const paths = ['/static/**', '/_fresh/static/**']
|
||||||
const routes = '/routes/*/index.tsx'
|
const routes = '/routes/*/index.tsx'
|
||||||
|
const version = await getVersion()
|
||||||
|
|
||||||
//Pre-cache routes
|
//Pre-cache routes
|
||||||
for await (const route of expandGlob(routes, { root: '.' })) {
|
for await (const route of expandGlob(routes, { root: '.' })) {
|
||||||
|
|
Loading…
Reference in a new issue