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 { 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('\\', '/')