Fix api route caching issue
This commit is contained in:
@@ -0,0 +1 @@
|
||||
export default function clearRequireCache(modulePath: string): void;
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
export default function clearRequireCache(modulePath) {
|
||||
const resolved = require.resolve(modulePath);
|
||||
const mod = require.cache[resolved];
|
||||
if (mod) {
|
||||
mod.children?.forEach((child) => {
|
||||
clearRequireCache(child.id);
|
||||
});
|
||||
delete require.cache[resolved];
|
||||
}
|
||||
}
|
||||
+14
-2
@@ -3,6 +3,9 @@ import grabConstants from "../../utils/grab-constants";
|
||||
import grabRouter from "../../utils/grab-router";
|
||||
import isDevelopment from "../../utils/is-development";
|
||||
import _ from "lodash";
|
||||
import path from "path";
|
||||
import grabDirNames from "../../utils/grab-dir-names";
|
||||
const { ROOT_DIR } = grabDirNames();
|
||||
export default async function ({ req }) {
|
||||
const url = new URL(req.url);
|
||||
const is_dev = isDevelopment();
|
||||
@@ -22,9 +25,18 @@ export default async function ({ req }) {
|
||||
});
|
||||
}
|
||||
const routeParams = await grabRouteParams({ req });
|
||||
let module;
|
||||
const now = Date.now();
|
||||
const import_path = is_dev ? `${match.filePath}?t=${now}` : match.filePath;
|
||||
const module = await import(import_path);
|
||||
if (global.SSR_BUNDLER_CTX_MAP?.[match.filePath]?.path) {
|
||||
const target_import = path.join(ROOT_DIR, global.SSR_BUNDLER_CTX_MAP[match.filePath].path);
|
||||
module = await import(`${target_import}?t=${now}`);
|
||||
}
|
||||
else {
|
||||
const import_path = is_dev
|
||||
? `${match.filePath}?t=${now}`
|
||||
: match.filePath;
|
||||
module = await import(import_path);
|
||||
}
|
||||
const config = module.config;
|
||||
const contentLength = req.headers.get("content-length");
|
||||
if (contentLength) {
|
||||
|
||||
-5
@@ -1,5 +0,0 @@
|
||||
type Params = {
|
||||
target_file_paths?: string[];
|
||||
};
|
||||
export default function rebuildBundler(params?: Params): Promise<void>;
|
||||
export {};
|
||||
-19
@@ -1,19 +0,0 @@
|
||||
import serverPostBuildFn from "./server-post-build-fn";
|
||||
import { log } from "../../utils/log";
|
||||
import allPagesBunBundler from "../bundler/all-pages-bun-bundler";
|
||||
import cleanupArtifacts from "./cleanup-artifacts";
|
||||
export default async function rebuildBundler(params) {
|
||||
try {
|
||||
global.ROUTER.reload();
|
||||
const new_artifacts = await allPagesBunBundler({
|
||||
page_file_paths: params?.target_file_paths,
|
||||
});
|
||||
await serverPostBuildFn();
|
||||
if (new_artifacts?.[0]) {
|
||||
cleanupArtifacts({ new_artifacts });
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
log.error(error);
|
||||
}
|
||||
}
|
||||
+2
-3
@@ -39,10 +39,9 @@ export default async function watcherEsbuildCTX() {
|
||||
return;
|
||||
}
|
||||
const target_files_match = /\.(tsx?|jsx?|css)$/;
|
||||
const rebuild_skip_paths = /\/pages\/api\//;
|
||||
// const rebuild_skip_paths = /\/pages\/api\//;
|
||||
if (event !== "rename") {
|
||||
if (filename.match(target_files_match) &&
|
||||
!filename.match(rebuild_skip_paths)) {
|
||||
if (filename.match(target_files_match)) {
|
||||
if (global.RECOMPILING)
|
||||
return;
|
||||
global.RECOMPILING = true;
|
||||
|
||||
Vendored
-1
@@ -1 +0,0 @@
|
||||
export default function watcher(): Promise<void>;
|
||||
Vendored
-81
@@ -1,81 +0,0 @@
|
||||
import { watch, existsSync } from "fs";
|
||||
import path from "path";
|
||||
import grabDirNames from "../../utils/grab-dir-names";
|
||||
import rebuildBundler from "./rebuild-bundler";
|
||||
import { log } from "../../utils/log";
|
||||
const { ROOT_DIR } = grabDirNames();
|
||||
export default async function watcher() {
|
||||
const pages_src_watcher = watch(ROOT_DIR, {
|
||||
recursive: true,
|
||||
persistent: true,
|
||||
}, async (event, filename) => {
|
||||
if (!filename)
|
||||
return;
|
||||
const full_file_path = path.join(ROOT_DIR, filename);
|
||||
if (full_file_path.match(/\/styles$/)) {
|
||||
global.RECOMPILING = true;
|
||||
await Bun.sleep(1000);
|
||||
await fullRebuild({
|
||||
msg: `Detected new \`styles\` directory. Rebuilding ...`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
const excluded_match = /node_modules\/|^public\/|^\.bunext\/|^\.git\/|^dist\/|bun\.lockb$/;
|
||||
if (filename.match(excluded_match))
|
||||
return;
|
||||
if (filename.match(/bunext.config\.ts/)) {
|
||||
await fullRebuild({
|
||||
msg: `bunext.config.ts file changed. Rebuilding server ...`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
const target_files_match = /\.(tsx?|jsx?|css)$/;
|
||||
if (event !== "rename") {
|
||||
if (filename.match(target_files_match)) {
|
||||
if (global.RECOMPILING)
|
||||
return;
|
||||
global.RECOMPILING = true;
|
||||
await fullRebuild();
|
||||
}
|
||||
return;
|
||||
}
|
||||
const is_file_of_interest = Boolean(filename.match(target_files_match));
|
||||
if (!is_file_of_interest) {
|
||||
return;
|
||||
}
|
||||
if (!filename.match(/^src\/pages\/|\.css$/))
|
||||
return;
|
||||
if (filename.match(/\/(--|\(| )/))
|
||||
return;
|
||||
if (global.RECOMPILING)
|
||||
return;
|
||||
const action = existsSync(full_file_path) ? "created" : "deleted";
|
||||
const type = filename.match(/\.css$/) ? "Sylesheet" : "Page";
|
||||
await fullRebuild({
|
||||
msg: `${type} ${action}: ${filename}. Rebuilding ...`,
|
||||
});
|
||||
});
|
||||
global.PAGES_SRC_WATCHER = pages_src_watcher;
|
||||
}
|
||||
async function fullRebuild(params) {
|
||||
try {
|
||||
const { msg } = params || {};
|
||||
global.RECOMPILING = true;
|
||||
const target_file_paths = global.HMR_CONTROLLERS.map((hmr) => hmr.target_map?.local_path).filter((f) => typeof f == "string");
|
||||
if (msg) {
|
||||
log.watch(msg);
|
||||
}
|
||||
await rebuildBundler({ target_file_paths });
|
||||
}
|
||||
catch (error) {
|
||||
log.error(error);
|
||||
}
|
||||
finally {
|
||||
global.RECOMPILING = false;
|
||||
global.IS_SERVER_COMPONENT = false;
|
||||
}
|
||||
if (global.PAGES_SRC_WATCHER) {
|
||||
global.PAGES_SRC_WATCHER.close();
|
||||
watcher();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user