-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhono.ts
More file actions
133 lines (117 loc) · 2.74 KB
/
Copy pathhono.ts
File metadata and controls
133 lines (117 loc) · 2.74 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// 此模块仅用于 deno deploy
import { slash } from "https://deno.land/x/easy_std@v0.8.0/src/path.ts";
import { walk } from "https://deno.land/std@0.217.0/fs/walk.ts";
import { relative } from "https://deno.land/std@0.217.0/path/relative.ts";
import { Hono } from "https://deno.land/x/hono@v4.0.7/hono.ts";
import {
etag,
serveStatic,
} from "https://deno.land/x/hono@v4.0.7/middleware.ts";
import cache from "./middlewares/cache.ts";
import { MiddlewareHandler } from "https://deno.land/x/hono@v4.0.7/types.ts";
export interface Options {
/**
* @default 'ssg'
*/
mode?: "ssg" | "spa" | "fallback" | "nuxt-ssg";
/**
* @default './'
*/
root?: string;
/**
* @default false
*/
forceCache?: boolean;
port?: number;
beforeMiddlewares?: MiddlewareHandler[];
afterMiddlewares?: MiddlewareHandler[];
}
export const defaultOptions: Options = {
mode: "ssg",
root: "./",
forceCache: false,
};
export async function useStaticServer(options: Options = {}) {
const {
port,
mode = defaultOptions.mode,
root = defaultOptions.root,
forceCache = defaultOptions.forceCache,
} = options;
const app = new Hono();
options.beforeMiddlewares?.forEach((middleware) => {
use(middleware);
});
if (forceCache) {
// 强制缓存
use(cache());
}
// 协商缓存
use(etag());
if (mode === "fallback") {
useServeStatic();
}
if (mode === "ssg") {
useServeStatic((path) => {
if (path.includes(".")) {
return path;
}
if (path.endsWith("/")) {
return path + "index.html";
}
return path + ".html";
});
}
if (mode === "spa") {
useServeStatic((path) => {
if (path.includes(".")) {
return path;
}
return "index.html";
});
}
if (mode === "nuxt-ssg") {
const routes: string[] = [];
for await (
const entry of walk(root!, {
includeFiles: false,
skip: [/_nuxt/],
})
) {
routes.push("/" + slash(relative(root!, entry.path)));
}
useServeStatic((path) => {
if (path.includes(".")) {
return path;
}
if (path.endsWith("/")) {
return path + "index.html";
}
if (routes.includes(path)) {
return path + "/" + "index.html";
}
return "404.html";
});
}
options.afterMiddlewares?.forEach((middleware) => {
use(middleware);
});
Deno.serve({ port }, app.fetch);
function use(middleware: MiddlewareHandler) {
app.use("*", middleware);
}
function useServeStatic(rewriteRequestPath?: (path: string) => string) {
use(
serveStatic({
root,
rewriteRequestPath,
}),
);
}
}
if (import.meta.main) {
// 开启强制缓存
useStaticServer({
forceCache: true,
});
}