From 0ee41a9e5a9ee59ab692c21abcfc6ee86a33b9d8 Mon Sep 17 00:00:00 2001 From: Julien Oculi Date: Wed, 17 Jul 2024 02:23:21 +0200 Subject: [PATCH] fix(api): :bug: update glob to list all and only routes declaring an `index.tsx` --- routes/api/serviceworker/precache.tsx | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/routes/api/serviceworker/precache.tsx b/routes/api/serviceworker/precache.tsx index 5eb2927..57a8b63 100644 --- a/routes/api/serviceworker/precache.tsx +++ b/routes/api/serviceworker/precache.tsx @@ -1,8 +1,8 @@ +import { expandGlob } from '$std/fs/mod.ts' import { SessionHandlers } from ':src/session/mod.ts' import { respondApi } from ':src/utils.ts' -import { expandGlob } from '$std/fs/mod.ts' -export type PrecacheResponse = { version: string; files: string[] } +export type PrecacheResponse = { version: string; preCachedUrls: string[] } // Updated only at server start const version = crypto.randomUUID() @@ -10,31 +10,28 @@ const version = crypto.randomUUID() export const handler: SessionHandlers = { async GET() { try { - const files: string[] = ['/', '/imports/markdown_css'] + const preCachedUrls: string[] = ['/', '/imports/markdown_css'] const paths = ['/static/**', '/_fresh/static/**'] - const routes = '/routes/*' + const routes = '/routes/*/index.tsx' //Pre-cache routes for await (const route of expandGlob(routes, { root: '.' })) { - if (!route.isDirectory) continue - if (route.name === 'api') continue - if (route.name === 'imports') continue - - files.push(strip(routes, route)) + if (!route.isFile) continue + //@ts-expect-error parentPath is missing from type definition + preCachedUrls.push(strip(routes, route.parentPath)) } // Pre-cache files for (const path of paths) { for await (const entry of expandGlob(path, { root: '.' })) { - if (entry.isFile) { - files.push(strip(path, entry)) - } + if (!entry.isFile) continue + preCachedUrls.push(strip(path, entry.path)) } } return respondApi<'success', PrecacheResponse>('success', { version, - files, + preCachedUrls, }) } catch (error) { return respondApi('error', error) @@ -42,7 +39,7 @@ export const handler: SessionHandlers = { }, } -function strip(root: string, { path }: { path: string }) { +function strip(root: string, path: string) { return path // Force unix/web separator .replaceAll('\\', '/')