Refactor HMR server-side watcher. Move to fs.watch instead of the inbuild esbuild.watch
This commit is contained in:
@@ -3,6 +3,7 @@ import type { BunextServerRouteConfig, BunxRouteParams } from "../../types";
|
||||
import grabRouteParams from "../../utils/grab-route-params";
|
||||
import grabConstants from "../../utils/grab-constants";
|
||||
import grabRouter from "../../utils/grab-router";
|
||||
import isDevelopment from "../../utils/is-development";
|
||||
|
||||
type Params = {
|
||||
req: Request;
|
||||
@@ -11,6 +12,7 @@ type Params = {
|
||||
|
||||
export default async function ({ req, server }: Params): Promise<Response> {
|
||||
const url = new URL(req.url);
|
||||
const is_dev = isDevelopment();
|
||||
|
||||
const { MBInBytes, ServerDefaultRequestBodyLimitBytes } = grabConstants();
|
||||
|
||||
@@ -37,7 +39,10 @@ export default async function ({ req, server }: Params): Promise<Response> {
|
||||
|
||||
const routeParams: BunxRouteParams = await grabRouteParams({ req });
|
||||
|
||||
const module = await import(match.filePath);
|
||||
const now = Date.now();
|
||||
const import_path = is_dev ? `${match.filePath}?t=${now}` : match.filePath;
|
||||
|
||||
const module = await import(import_path);
|
||||
const config = module.config as BunextServerRouteConfig | undefined;
|
||||
|
||||
const contentLength = req.headers.get("content-length");
|
||||
@@ -70,5 +75,9 @@ export default async function ({ req, server }: Params): Promise<Response> {
|
||||
server,
|
||||
} as BunxRouteParams);
|
||||
|
||||
if (is_dev) {
|
||||
res.headers.set("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user