-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
assets.ts
54 lines (43 loc) · 1.46 KB
/
assets.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* SPDX-FileCopyrightText: 2014-present Kriasoft */
/* SPDX-License-Identifier: MIT */
import { serveStatic } from "hono/cloudflare-workers";
import { getMimeType } from "hono/utils/mime";
import assetManifest from "__STATIC_CONTENT_MANIFEST";
import { app } from "../core/app.js";
const manifest = JSON.parse(assetManifest);
// Static assets handler
// https://hono.dev/getting-started/cloudflare-workers#serve-static-files
const asset = serveStatic({ manifest });
const fallback = serveStatic({ path: "/index.html", manifest });
// Serve web application assets bundled into
// the worker script from the `../app/dist` folder
export const handler = app.use("*", async (ctx, next) => {
const url = new URL(ctx.req.url);
// Alternatively, import the list of routes from the `app` package
const isKnownRoute = [
"",
"/",
"/dashboard",
"/settings",
"/settings/account",
"/login",
"/signup",
"/privacy",
"/terms",
].includes(url.pathname);
// Serve index.html for known URL routes
if (isKnownRoute) {
return await fallback(ctx, next);
}
// Otherwise attempt to serve the static asset (file)
const res = await asset(ctx, next);
// Serve index.html for unknown URL routes with 404 status code
if (!res && !getMimeType(url.pathname)) {
const res = await fallback(ctx, next);
if (res) {
return new Response(res.body, { ...res, status: 404 });
}
}
return res;
});
export type AssetsHandler = typeof handler;