Initial version #1
@@ -14,34 +14,26 @@ export const HttpAppDevelopment = router.pipe(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
const serveWebappDist = Effect.fnUntraced(function*(file: string) {
|
const serveWebapp = HttpMiddleware.make(() => Effect.gen(function*() {
|
||||||
const path = yield* Path.Path
|
const path = yield* Path.Path
|
||||||
const fs = yield* FileSystem.FileSystem
|
const fs = yield* FileSystem.FileSystem
|
||||||
const source = path.join(
|
const req = yield* HttpServerRequest.HttpServerRequest
|
||||||
yield* path.fromFileUrl(new URL(".", import.meta.resolve("@website/webapp"))),
|
|
||||||
"dist", file,
|
const dist = path.join(yield* path.fromFileUrl(new URL(".", import.meta.resolve("@website/webapp"))), "dist")
|
||||||
)
|
const isValid = yield* fs.stat(path.join(dist, req.url)).pipe(
|
||||||
const exists = yield* fs.stat(source).pipe(
|
|
||||||
Effect.andThen(stat => stat.type === "File"),
|
Effect.andThen(stat => stat.type === "File"),
|
||||||
Effect.catchAll(() => Effect.succeed(false)),
|
Effect.catchAll(() => Effect.succeed(false)),
|
||||||
)
|
)
|
||||||
|
|
||||||
return exists
|
return yield* HttpServerResponse.setHeader(
|
||||||
? yield* HttpServerResponse.setHeader(
|
yield* HttpServerResponse.file(path.join(dist, isValid ? req.url : "index.html")),
|
||||||
yield* HttpServerResponse.file(source),
|
"Cache-Control",
|
||||||
"Cache-Control",
|
`public, max-age=${Duration.toSeconds("365 days")}, immutable`
|
||||||
`public, max-age=${Duration.toSeconds("365 days")}, immutable`
|
)
|
||||||
)
|
}))
|
||||||
: yield* HttpServerResponse.setStatus(HttpServerResponse.empty(), 404)
|
|
||||||
})
|
|
||||||
|
|
||||||
export const HttpAppProduction = router.pipe(
|
export const HttpAppProduction = router.pipe(
|
||||||
HttpRouter.all("/assets/*", Effect.andThen(
|
serveWebapp,
|
||||||
HttpServerRequest.HttpServerRequest,
|
|
||||||
req => serveWebappDist(req.url),
|
|
||||||
)),
|
|
||||||
HttpRouter.all("*", serveWebappDist("index.html")),
|
|
||||||
|
|
||||||
HttpServer.serve(flow(
|
HttpServer.serve(flow(
|
||||||
HttpMiddleware.logger,
|
HttpMiddleware.logger,
|
||||||
HttpMiddleware.xForwardedHeaders,
|
HttpMiddleware.xForwardedHeaders,
|
||||||
|
|||||||
Reference in New Issue
Block a user