fix(api): 🐛 update glob to list all and only routes declaring an index.tsx

This commit is contained in:
Julien Oculi 2024-07-17 02:23:21 +02:00
parent f2b39d0288
commit 0ee41a9e5a

View file

@ -1,8 +1,8 @@
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 { 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 // Updated only at server start
const version = crypto.randomUUID() const version = crypto.randomUUID()
@ -10,31 +10,28 @@ const version = crypto.randomUUID()
export const handler: SessionHandlers = { export const handler: SessionHandlers = {
async GET() { async GET() {
try { try {
const files: string[] = ['/', '/imports/markdown_css'] const preCachedUrls: string[] = ['/', '/imports/markdown_css']
const paths = ['/static/**', '/_fresh/static/**'] const paths = ['/static/**', '/_fresh/static/**']
const routes = '/routes/*' const routes = '/routes/*/index.tsx'
//Pre-cache routes //Pre-cache routes
for await (const route of expandGlob(routes, { root: '.' })) { for await (const route of expandGlob(routes, { root: '.' })) {
if (!route.isDirectory) continue if (!route.isFile) continue
if (route.name === 'api') continue //@ts-expect-error parentPath is missing from type definition
if (route.name === 'imports') continue preCachedUrls.push(strip(routes, route.parentPath))
files.push(strip(routes, route))
} }
// Pre-cache files // Pre-cache files
for (const path of paths) { for (const path of paths) {
for await (const entry of expandGlob(path, { root: '.' })) { for await (const entry of expandGlob(path, { root: '.' })) {
if (entry.isFile) { if (!entry.isFile) continue
files.push(strip(path, entry)) preCachedUrls.push(strip(path, entry.path))
}
} }
} }
return respondApi<'success', PrecacheResponse>('success', { return respondApi<'success', PrecacheResponse>('success', {
version, version,
files, preCachedUrls,
}) })
} catch (error) { } catch (error) {
return respondApi('error', 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 return path
// Force unix/web separator // Force unix/web separator
.replaceAll('\\', '/') .replaceAll('\\', '/')