website/main.ts

53 lines
1.3 KiB
TypeScript

import { App, fsRoutes, staticFiles } from 'fresh'
import { type State } from './utils.ts'
import { contentType } from 'jsr:@std/media-types@1/content-type'
import { smartStylesheetPlugin } from ':plugins/SmartStylesheet.tsx'
export const app = new App<State>()
//disable this unsupprted path to prevent static file render error
app.use((ctx) =>
ctx.url.pathname === '/.well-known/appspecific/com.chrome.devtools.json'
? new Response(null, { status: 404 })
: ctx.next()
)
app.use(staticFiles())
smartStylesheetPlugin(app)
//TEMP fix before updating cssBundler middleware
app.use(async (ctx) => {
const response = await ctx.next()
if (
response.status === 404 &&
!ctx.url.pathname.match(/\/js\/[0-9a-f]+\/\S+\.js/)
) {
const ext = ctx.url.pathname.split('.').at(-1) ?? '.bin'
const mime = contentType(ext) ?? 'application/octet-stream'
try {
const file = await Deno.readFile(`./_fresh/static/${ctx.url.pathname}`)
return new Response(file, {
headers: {
'Content-Type': mime,
},
})
} catch {
//TEMP don't handle specific error for now
return response
}
}
return response
})
await fsRoutes(app, {
dir: './',
loadIsland: (path) => import(`./islands/${path}`),
loadRoute: (path) => import(`./routes/${path}`),
})
if (import.meta.main) {
await app.listen()
}