diff --git a/src/functions/bundler/all-pages-bundler.ts b/src/functions/bundler/all-pages-bundler.ts index 43e01bd..4782b53 100644 --- a/src/functions/bundler/all-pages-bundler.ts +++ b/src/functions/bundler/all-pages-bundler.ts @@ -81,6 +81,8 @@ export default async function allPagesBundler(params?: Params) { const elapsed = (performance.now() - buildStart).toFixed(0); log.success(`[Built] in ${elapsed}ms`); + global.RECOMPILING = false; + if (params?.exit_after_first_build) { process.exit(); } @@ -115,6 +117,6 @@ export default async function allPagesBundler(params?: Params) { if (params?.watch) { global.BUNDLER_CTX = ctx; - global.BUNDLER_CTX.watch(); + // global.BUNDLER_CTX.watch(); } } diff --git a/src/functions/bundler/grab-client-hydration-script.ts b/src/functions/bundler/grab-client-hydration-script.ts index d492717..7ff831c 100644 --- a/src/functions/bundler/grab-client-hydration-script.ts +++ b/src/functions/bundler/grab-client-hydration-script.ts @@ -54,9 +54,9 @@ export default function grabClientHydrationScript({ page_local_path }: Params) { txt += `const pageProps = window.__PAGE_PROPS__ || {};\n`; if (does_root_exist) { - txt += `const component = \n`; + txt += `const component = \n`; } else { - txt += `const component = \n`; + txt += `const component = \n`; } txt += `if (window.${ClientRootComponentWindowName}?.render) {\n`; diff --git a/src/functions/server/handle-routes.ts b/src/functions/server/handle-routes.ts index 4707e2d..8f304cb 100644 --- a/src/functions/server/handle-routes.ts +++ b/src/functions/server/handle-routes.ts @@ -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 { 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 { 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 { server, } as BunxRouteParams); + if (is_dev) { + res.headers.set("Cache-Control", "no-cache, no-store, must-revalidate"); + } + return res; } diff --git a/src/functions/server/watcher.tsx b/src/functions/server/watcher.tsx index 360f8a5..7a9b8a3 100644 --- a/src/functions/server/watcher.tsx +++ b/src/functions/server/watcher.tsx @@ -16,7 +16,17 @@ export default function watcher() { async (event, filename) => { if (!filename) return; - if (event !== "rename") return; + if (event !== "rename") { + if ( + filename.match(/\.(tsx?|jsx?|css)$/) && + global.BUNDLER_CTX + ) { + if (global.RECOMPILING) return; + global.RECOMPILING = true; + await global.BUNDLER_CTX.rebuild(); + } + return; + } if (!filename.match(/^pages\//)) return; if (filename.match(/\/(--|\()/)) return; diff --git a/src/functions/server/web-pages/grab-page-bundled-react-component.tsx b/src/functions/server/web-pages/grab-page-bundled-react-component.tsx index dffb56b..35d6517 100644 --- a/src/functions/server/web-pages/grab-page-bundled-react-component.tsx +++ b/src/functions/server/web-pages/grab-page-bundled-react-component.tsx @@ -16,9 +16,8 @@ export default async function grabPageBundledReactComponent({ try { let tsx = ``; - const server_res_json = EJSON.stringify(server_res || {})?.replace( - /"/g, - '\\"', + const server_res_json = JSON.stringify( + EJSON.stringify(server_res || {}) ?? "{}", ); if (root_file) { @@ -27,7 +26,7 @@ export default async function grabPageBundledReactComponent({ tsx += `import Page from "${file_path}"\n`; tsx += `export default function Main() {\n\n`; - tsx += `const props = JSON.parse("${server_res_json}")\n\n`; + tsx += `const props = JSON.parse(${server_res_json})\n\n`; tsx += ` return (\n`; if (root_file) { tsx += ` \n`; diff --git a/src/functions/server/web-pages/grab-page-component.tsx b/src/functions/server/web-pages/grab-page-component.tsx index 53b34e9..3e035e4 100644 --- a/src/functions/server/web-pages/grab-page-component.tsx +++ b/src/functions/server/web-pages/grab-page-component.tsx @@ -27,6 +27,7 @@ export default async function grabPageComponent({ }: Params): Promise { const url = req?.url ? new URL(req.url) : undefined; const router = global.ROUTER; + const now = Date.now(); let routeParams: BunxRouteParams | undefined = undefined; @@ -77,7 +78,7 @@ export default async function grabPageComponent({ const { root_file } = grabRootFile(); - const module: BunextPageModule = await import(file_path); + const module: BunextPageModule = await import(`${file_path}?t=${now}`); if (debug) { log.info(`module:`, module); @@ -106,7 +107,10 @@ export default async function grabPageComponent({ try { if (routeParams) { - const serverData = await module["server"]?.(routeParams); + const serverData = await module["server"]?.({ + ...routeParams, + query: { ...routeParams.query, ...match?.query }, + }); return { ...serverData, ...default_props, diff --git a/src/functions/server/web-pages/grab-web-page-hydration-script.tsx b/src/functions/server/web-pages/grab-web-page-hydration-script.tsx index 46e258e..996f960 100644 --- a/src/functions/server/web-pages/grab-web-page-hydration-script.tsx +++ b/src/functions/server/web-pages/grab-web-page-hydration-script.tsx @@ -16,7 +16,7 @@ export default async function ({ bundledMap }: Params) { script += ` console.log(\`HMR Changes Detected. Updating ...\`);\n`; script += ` try {\n`; script += ` const data = JSON.parse(event.data);\n`; - // script += ` console.log("data", data);\n`; + script += ` console.log("data", data);\n`; // script += ` const modulePath = \`/\${data.target_map.path}\`;\n\n`; // script += ` const modulePath = \`/${AppData["ClientHMRPath"]}?href=\${window.location.href}&t=\${Date.now()}\`;\n\n`; diff --git a/src/types/index.ts b/src/types/index.ts index 439c3bc..e1abcef 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -221,7 +221,7 @@ export type BunextPageModuleServerReturn< * Expiry time of the cache in seconds */ cacheExpiry?: number; - url: BunextPageModuleServerReturnURLObject; + url?: BunextPageModuleServerReturnURLObject; }; export type BunextPageModuleServerReturnURLObject = URL & {}; diff --git a/src/utils/grab-all-pages.ts b/src/utils/grab-all-pages.ts index 2b5deae..0e22abb 100644 --- a/src/utils/grab-all-pages.ts +++ b/src/utils/grab-all-pages.ts @@ -42,7 +42,7 @@ function grabPageDirRecursively({ page_dir }: { page_dir: string }) { continue; } - if (page.match(/\(|\)|--/)) { + if (page.match(/\(|\)|--|\/api\//)) { continue; }