-
Notifications
You must be signed in to change notification settings - Fork 754
Expand file tree
/
Copy pathdev_server.ts
More file actions
82 lines (72 loc) · 2.43 KB
/
Copy pathdev_server.ts
File metadata and controls
82 lines (72 loc) · 2.43 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
import type { Plugin } from "vite";
import { nodeToRequest, responseToNode } from "../request.ts";
import * as path from "@std/path";
import { ASSET_CACHE_BUST_KEY } from "fresh/internal";
export function devServer(): Plugin[] {
return [
{
name: "fresh:dev_server",
configureServer(server) {
const IGNORE_URLS = /^\/(@(vite|fs|id)|\.vite)\//;
server.middlewares.use(async (nodeReq, nodeRes, next) => {
const serverCfg = server.config.server;
const protocol = serverCfg.https ? "https" : "http";
const host = serverCfg.host ? serverCfg.host : "localhost";
const port = serverCfg.port;
const url = new URL(
`${protocol}://${host}:${port}${nodeReq.url ?? "/"}`,
);
// Don't cache in dev
url.searchParams.delete(ASSET_CACHE_BUST_KEY);
// Check if it's a vite url
if (
IGNORE_URLS.test(url.pathname) ||
server.environments.client.moduleGraph.urlToModuleMap.has(
url.pathname,
) ||
url.pathname === "/.well-known/appspecific/com.chrome.devtools.json"
) {
return next();
}
// Check if it's a static file first
if (url.pathname !== "/") {
const ext = path.extname(url.pathname);
if (ext !== "") {
return next();
}
}
const mod = await server.ssrLoadModule("fresh:server_entry");
try {
const req = nodeToRequest(nodeReq, url);
const res = await mod.default.fetch(req);
await responseToNode(res, nodeRes);
return nodeRes;
} catch (err) {
return next(err);
}
});
},
},
{
name: "fresh:server_hmr",
applyToEnvironment(env) {
return env.name === "ssr";
},
hotUpdate(options) {
const clientMod = options.server.environments.client.moduleGraph
.getModulesByFile(options.file);
if (clientMod !== undefined) {
// Vite can do HMR here
return;
}
const ssrMod = options.server.environments.ssr.moduleGraph
.getModulesByFile(options.file);
if (ssrMod !== undefined) {
// SSR-only module. Might still be a route.
// TODO: Implement proper ssr hmr
options.server.hot.send("fresh:reload");
}
},
},
];
}