diff --git a/dist/data/app-data.d.ts b/dist/data/app-data.d.ts index 19b0109..9efb307 100644 --- a/dist/data/app-data.d.ts +++ b/dist/data/app-data.d.ts @@ -5,4 +5,5 @@ export declare const AppData: { readonly ClientHMRPath: "__bunext_client_hmr__"; readonly BunextClientHydrationScriptID: "bunext-client-hydration-script"; readonly BunextTmpFileExt: ".bunext_tmp.tsx"; + readonly BunextHMRRetryRoute: "/.bunext/hmr-retry"; }; diff --git a/dist/data/app-data.js b/dist/data/app-data.js index 727cbb0..5a6c1e0 100644 --- a/dist/data/app-data.js +++ b/dist/data/app-data.js @@ -5,4 +5,5 @@ export const AppData = { ClientHMRPath: "__bunext_client_hmr__", BunextClientHydrationScriptID: "bunext-client-hydration-script", BunextTmpFileExt: ".bunext_tmp.tsx", + BunextHMRRetryRoute: "/.bunext/hmr-retry", }; diff --git a/dist/functions/bunext-init.d.ts b/dist/functions/bunext-init.d.ts index bd429d0..37039f1 100644 --- a/dist/functions/bunext-init.d.ts +++ b/dist/functions/bunext-init.d.ts @@ -3,6 +3,7 @@ import type { FileSystemRouter, Server } from "bun"; import grabDirNames from "../utils/grab-dir-names"; import { type FSWatcher } from "fs"; import type { BuildContext } from "esbuild"; +import grabConstants from "../utils/grab-constants"; /** * # Declare Global Variables */ @@ -46,5 +47,6 @@ declare global { var BUNDLER_CTX_DISPOSED: boolean | undefined; var REBUILD_RETRIES: number; var IS_404_PAGE: boolean; + var CONSTANTS: ReturnType; } export default function bunextInit(): Promise; diff --git a/dist/functions/bunext-init.js b/dist/functions/bunext-init.js index f0863af..cbeca15 100644 --- a/dist/functions/bunext-init.js +++ b/dist/functions/bunext-init.js @@ -8,6 +8,7 @@ import watcherEsbuildCTX from "./server/watcher-esbuild-ctx"; import allPagesESBuildContextBundler from "./bundler/all-pages-esbuild-context-bundler"; import serverPostBuildFn from "./server/server-post-build-fn"; import reactModulesBundler from "./bundler/react-modules-bundler"; +import grabConstants from "../utils/grab-constants"; const dirNames = grabDirNames(); const { PAGES_DIR } = dirNames; export default async function bunextInit() { @@ -24,6 +25,7 @@ export default async function bunextInit() { global.REACT_DOM_MODULE_CACHE = new Map(); log.banner(); await init(); + global.CONSTANTS = grabConstants(); await reactModulesBundler(); const router = new Bun.FileSystemRouter({ style: "nextjs", @@ -36,17 +38,11 @@ export default async function bunextInit() { await allPagesESBuildContextBundler({ post_build_fn: serverPostBuildFn, }); - // initPages({ - // log_time: true, - // }); watcherEsbuildCTX(); } else { log.build(`Building Modules ...`); await allPagesESBuildContextBundler(); - // initPages({ - // log_time: true, - // }); cron(); } } diff --git a/dist/functions/cache/trim-cache-key.js b/dist/functions/cache/trim-cache-key.js index f6129b3..72eb3d1 100644 --- a/dist/functions/cache/trim-cache-key.js +++ b/dist/functions/cache/trim-cache-key.js @@ -10,7 +10,7 @@ export default async function trimCacheKey({ key, }) { key, }); const config = global.CONFIG; - const default_expiry_time_seconds = config.defaultCacheExpiry || + const default_expiry_time_seconds = config.default_cache_expiry || AppData["DefaultCacheExpiryTimeSeconds"]; const default_expiry_time_milliseconds = default_expiry_time_seconds * 1000; const cache_content_path = path.join(BUNEXT_CACHE_DIR, cache_name); diff --git a/dist/functions/server/bunext-req-handler.js b/dist/functions/server/bunext-req-handler.js index ad5fd51..f77e61c 100644 --- a/dist/functions/server/bunext-req-handler.js +++ b/dist/functions/server/bunext-req-handler.js @@ -1,20 +1,24 @@ import handleWebPages from "./web-pages/handle-web-pages"; import handleRoutes from "./handle-routes"; import isDevelopment from "../../utils/is-development"; -import grabConstants from "../../utils/grab-constants"; import handleHmr from "./handle-hmr"; import handlePublic from "./handle-public"; import handleFiles from "./handle-files"; import handleBunextPublicAssets from "./handle-bunext-public-assets"; +import checkExcludedPatterns from "../../utils/check-excluded-patterns"; +import { AppData } from "../../data/app-data"; +import fullRebuild from "./full-rebuild"; export default async function bunextRequestHandler({ req: initial_req, server, }) { const is_dev = isDevelopment(); let req = initial_req.clone(); try { const url = new URL(req.url); - const { config } = grabConstants(); + if (checkExcludedPatterns({ path: url.pathname })) { + return Response.json({ success: false, msg: `Invalid Path` }); + } let response = undefined; - if (config?.middleware) { - const middleware_res = await config.middleware({ + if (global.CONSTANTS.config?.middleware) { + const middleware_res = await global.CONSTANTS.config.middleware({ req: initial_req, url, }); @@ -25,10 +29,10 @@ export default async function bunextRequestHandler({ req: initial_req, server, } req = middleware_res; } } - // const server_upgrade = server.upgrade(req); - // if (server_upgrade) { - // return undefined; - // } + if (is_dev && url.pathname == AppData["BunextHMRRetryRoute"]) { + await fullRebuild({ msg: `HMR Retry Rebuild ...` }); + return new Response("Modules Rebuilt"); + } if (url.pathname === "/__hmr" && is_dev) { response = await handleHmr({ req }); } diff --git a/dist/functions/server/server-params-gen.js b/dist/functions/server/server-params-gen.js index 156419b..19eebba 100644 --- a/dist/functions/server/server-params-gen.js +++ b/dist/functions/server/server-params-gen.js @@ -15,6 +15,6 @@ export default async function () { idleTimeout: development ? 0 : undefined, development, websocket: config?.websocket, - ..._.omit(config?.serverOptions || {}, ["fetch"]), + ..._.omit(config?.server_options || {}, ["fetch"]), }; } diff --git a/dist/functions/server/watcher-esbuild-ctx.js b/dist/functions/server/watcher-esbuild-ctx.js index 8c50ad8..030cec9 100644 --- a/dist/functions/server/watcher-esbuild-ctx.js +++ b/dist/functions/server/watcher-esbuild-ctx.js @@ -1,11 +1,9 @@ import { watch, existsSync, statSync } from "fs"; import path from "path"; import grabDirNames from "../../utils/grab-dir-names"; -import { log } from "../../utils/log"; -import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler"; -import serverPostBuildFn from "./server-post-build-fn"; import fullRebuild from "./full-rebuild"; import { AppData } from "../../data/app-data"; +import checkExcludedPatterns from "../../utils/check-excluded-patterns"; const { ROOT_DIR } = grabDirNames(); export default async function watcherEsbuildCTX() { const pages_src_watcher = watch(ROOT_DIR, { @@ -80,7 +78,7 @@ export default async function watcherEsbuildCTX() { } if (!filename.match(/^src\/pages\/|\.css$/)) return reloadWatcher(); - if (filename.match(/\/(--|\()/)) + if (checkExcludedPatterns({ path: filename })) return reloadWatcher(); if (filename.match(/ /)) return reloadWatcher(); diff --git a/dist/functions/server/web-pages/grab-web-page-hydration-script.js b/dist/functions/server/web-pages/grab-web-page-hydration-script.js index 7c03494..d4104e9 100644 --- a/dist/functions/server/web-pages/grab-web-page-hydration-script.js +++ b/dist/functions/server/web-pages/grab-web-page-hydration-script.js @@ -12,6 +12,9 @@ export default async function (params) { const supress_condition = errors_to_supress .map((e) => `args[0].includes("${e}")`) .join(" || "); + script += `let retries = 0;\n`; + script += `let retries_exhausted = false;\n`; + script += `const MAX_RETRIES = 1;\n`; script += `const _ce = console.error.bind(console);\n`; script += `console.error = (...args) => {\n`; script += ` if (typeof args[0] === "string" && (${supress_condition})) return;\n`; @@ -23,8 +26,15 @@ export default async function (params) { script += ` const overlay = document.createElement("div");\n`; script += ` overlay.id = "__bunext_error_overlay";\n`; script += ` overlay.style.cssText = "position:fixed;inset:0;z-index:99999;background:#1a1a1a;color:#ff6b6b;font-family:monospace;font-size:14px;padding:24px;overflow:auto;";\n`; - script += ` overlay.innerHTML = \`
Runtime Error
\${message}
\${source ? \`
\${source}
\` : ""}\${stack ? \`
\${stack}
\` : ""}
\`;\n`; + script += ` overlay.innerHTML = \`
Runtime Error
\${message}
\${source ? \`
\${source}
\` : ""}\${stack ? \`
\${stack}
\` : ""}
\`;\n`; script += ` document.body.appendChild(overlay);\n`; + script += ` if (retries < MAX_RETRIES) {\n`; + script += ` retries++\n`; + script += ` console.log(\`Retrying \${retries} ...\`)\n`; + script += ` fetch("${AppData["BunextHMRRetryRoute"]}")\n`; + script += ` } else {\n`; + script += ` retries_exhausted = true\n`; + script += ` }\n`; script += `}\n\n`; script += `function __bunext_should_suppress_runtime_error(message) {\n`; script += ` return false;\n`; @@ -53,7 +63,14 @@ export default async function (params) { script += `hmr.addEventListener("update", async (event) => {\n`; script += ` if (event?.data) {\n`; script += ` try {\n`; - script += ` document.getElementById("__bunext_error_overlay")?.remove();\n`; + script += ` if (retries_exhausted) {\n`; + script += ` document.getElementById("__bunext_error_overlay")?.remove();\n`; + script += ` retries = 0;\n`; + script += ` retries_exhausted = false;\n`; + script += ` }\n`; + // script += ` if (retries >= MAX_RETRIES && document.getElementById("__bunext_error_overlay")) {\n`; + // script += ` retries = 0;\n`; + // script += ` }\n`; script += ` const data = JSON.parse(event.data);\n`; // script += ` console.log("data", data);\n`; script += ` if (data.reload) {\n`; @@ -98,7 +115,9 @@ export default async function (params) { // script += ` window.location.reload();\n`; // script += ` }\n`; // script += ` console.log("newScript", newScript);\n`; + // script += ` document.getElementById("__bunext_error_overlay")?.remove();\n`; script += ` document.head.appendChild(newScript);\n\n`; + // script += ` retries = 0;\n\n`; script += ` } catch (err) {\n`; script += ` console.error("HMR update failed, falling back to reload:", err.message);\n`; script += ` window.location.reload();\n`; diff --git a/dist/types/index.d.ts b/dist/types/index.d.ts index 09f84f9..cc818df 100644 --- a/dist/types/index.d.ts +++ b/dist/types/index.d.ts @@ -36,18 +36,23 @@ export type PageModule = { staticParams: StaticParams; }; export type BunextConfig = { - distDir?: string; - assetsPrefix?: string; + dist_dir?: string; + assets_prefix?: string; origin?: string; - globalVars?: { + global_vars?: { [k: string]: any; }; port?: number; development?: boolean; middleware?: (params: BunextConfigMiddlewareParams) => Promise | Response | Request | undefined; - defaultCacheExpiry?: number; + default_cache_expiry?: number; websocket?: WebSocketHandler; - serverOptions?: Omit, "fetch" | "routes">; + server_options?: Omit, "fetch" | "routes">; + /** + * These are patterns in the pages to exclude + * from the router + */ + pages_exclude_patterns?: RegExp[]; }; export type BunextConfigMiddlewareParams = { req: Request; diff --git a/dist/utils/check-excluded-patterns.d.ts b/dist/utils/check-excluded-patterns.d.ts new file mode 100644 index 0000000..0a344f8 --- /dev/null +++ b/dist/utils/check-excluded-patterns.d.ts @@ -0,0 +1,5 @@ +type Params = { + path: string; +}; +export default function ({ path }: Params): boolean; +export {}; diff --git a/dist/utils/check-excluded-patterns.js b/dist/utils/check-excluded-patterns.js new file mode 100644 index 0000000..8cc97df --- /dev/null +++ b/dist/utils/check-excluded-patterns.js @@ -0,0 +1,8 @@ +export default function ({ path }) { + for (let i = 0; i < global.CONSTANTS.RouteIgnorePatterns.length; i++) { + const regex = global.CONSTANTS.RouteIgnorePatterns[i]; + if (path.match(regex)) + return true; + } + return false; +} diff --git a/dist/utils/grab-all-pages.js b/dist/utils/grab-all-pages.js index fffef17..49e8f2c 100644 --- a/dist/utils/grab-all-pages.js +++ b/dist/utils/grab-all-pages.js @@ -3,6 +3,7 @@ import grabDirNames from "./grab-dir-names"; import path from "path"; import AppNames from "./grab-app-names"; import pagePathTransform from "./page-path-transform"; +import checkExcludedPatterns from "./check-excluded-patterns"; export default function grabAllPages(params) { const { PAGES_DIR } = grabDirNames(); const pages = grabPageDirRecursively({ page_dir: PAGES_DIR }); @@ -31,7 +32,10 @@ function grabPageDirRecursively({ page_dir }) { if (page.match(new RegExp(`${AppNames["RootPagesComponentName"]}`))) { continue; } - if (page.match(/\(|\)|--|\/api\//)) { + if (checkExcludedPatterns({ path: page })) { + continue; + } + if (page.match(/\/api\//)) { continue; } if (page_name.split(".").length > 2) { @@ -39,7 +43,7 @@ function grabPageDirRecursively({ page_dir }) { } const page_stat = statSync(full_page_path); if (page_stat.isDirectory()) { - if (page.match(/\(|\)/)) + if (checkExcludedPatterns({ path: page })) continue; const new_page_files = grabPageDirRecursively({ page_dir: full_page_path, diff --git a/dist/utils/grab-assets-prefix.js b/dist/utils/grab-assets-prefix.js index 1802e51..661c7bd 100644 --- a/dist/utils/grab-assets-prefix.js +++ b/dist/utils/grab-assets-prefix.js @@ -1,7 +1,7 @@ import AppNames from "./grab-app-names"; export default function grabAssetsPrefix() { - if (global.CONFIG.assetsPrefix) { - return global.CONFIG.assetsPrefix; + if (global.CONFIG.assets_prefix) { + return global.CONFIG.assets_prefix; } const { defaultAssetPrefix } = AppNames; return defaultAssetPrefix; diff --git a/dist/utils/grab-constants.d.ts b/dist/utils/grab-constants.d.ts index 5c81cd6..2865c06 100644 --- a/dist/utils/grab-constants.d.ts +++ b/dist/utils/grab-constants.d.ts @@ -6,4 +6,5 @@ export default function grabConstants(): { readonly ClientRootComponentWindowName: "BUNEXT_ROOT"; readonly MaxBundlerRebuilds: 5; readonly config: import("../types").BunextConfig; + readonly RouteIgnorePatterns: RegExp[]; }; diff --git a/dist/utils/grab-constants.js b/dist/utils/grab-constants.js index 626fdef..221f8ea 100644 --- a/dist/utils/grab-constants.js +++ b/dist/utils/grab-constants.js @@ -6,6 +6,10 @@ export default function grabConstants() { const ClientRootComponentWindowName = "BUNEXT_ROOT"; const ServerDefaultRequestBodyLimitBytes = MB_IN_BYTES * 10; const MaxBundlerRebuilds = 5; + const RouteIgnorePatterns = [/\/\(/]; + if (config.pages_exclude_patterns) { + RouteIgnorePatterns.push(...config.pages_exclude_patterns); + } return { ClientRootElementIDName, ClientWindowPagePropsName, @@ -14,5 +18,6 @@ export default function grabConstants() { ClientRootComponentWindowName, MaxBundlerRebuilds, config, + RouteIgnorePatterns, }; } diff --git a/package.json b/package.json index 3a1751d..5205281 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/bunext", - "version": "1.0.74", + "version": "1.0.75", "main": "dist/index.js", "module": "index.ts", "dependencies": { diff --git a/src/data/app-data.ts b/src/data/app-data.ts index f4fdbe8..424374a 100644 --- a/src/data/app-data.ts +++ b/src/data/app-data.ts @@ -5,4 +5,5 @@ export const AppData = { ClientHMRPath: "__bunext_client_hmr__", BunextClientHydrationScriptID: "bunext-client-hydration-script", BunextTmpFileExt: ".bunext_tmp.tsx", + BunextHMRRetryRoute: "/.bunext/hmr-retry", } as const; diff --git a/src/functions/bunext-init.ts b/src/functions/bunext-init.ts index 309a635..16c9941 100644 --- a/src/functions/bunext-init.ts +++ b/src/functions/bunext-init.ts @@ -16,7 +16,7 @@ import watcherEsbuildCTX from "./server/watcher-esbuild-ctx"; import allPagesESBuildContextBundler from "./bundler/all-pages-esbuild-context-bundler"; import serverPostBuildFn from "./server/server-post-build-fn"; import reactModulesBundler from "./bundler/react-modules-bundler"; -// import initPages from "./bundler/init-pages"; +import grabConstants from "../utils/grab-constants"; /** * # Declare Global Variables @@ -50,6 +50,7 @@ declare global { var BUNDLER_CTX_DISPOSED: boolean | undefined; var REBUILD_RETRIES: number; var IS_404_PAGE: boolean; + var CONSTANTS: ReturnType; } const dirNames = grabDirNames(); @@ -71,6 +72,9 @@ export default async function bunextInit() { log.banner(); await init(); + + global.CONSTANTS = grabConstants(); + await reactModulesBundler(); const router = new Bun.FileSystemRouter({ @@ -87,16 +91,10 @@ export default async function bunextInit() { await allPagesESBuildContextBundler({ post_build_fn: serverPostBuildFn, }); - // initPages({ - // log_time: true, - // }); watcherEsbuildCTX(); } else { log.build(`Building Modules ...`); await allPagesESBuildContextBundler(); - // initPages({ - // log_time: true, - // }); cron(); } } diff --git a/src/functions/cache/trim-cache-key.ts b/src/functions/cache/trim-cache-key.ts index f99b128..2b6bb09 100644 --- a/src/functions/cache/trim-cache-key.ts +++ b/src/functions/cache/trim-cache-key.ts @@ -21,7 +21,7 @@ export default async function trimCacheKey({ const config = global.CONFIG; const default_expiry_time_seconds = - config.defaultCacheExpiry || + config.default_cache_expiry || AppData["DefaultCacheExpiryTimeSeconds"]; const default_expiry_time_milliseconds = diff --git a/src/functions/server/bunext-req-handler.ts b/src/functions/server/bunext-req-handler.ts index b231251..80389a4 100644 --- a/src/functions/server/bunext-req-handler.ts +++ b/src/functions/server/bunext-req-handler.ts @@ -1,11 +1,13 @@ import handleWebPages from "./web-pages/handle-web-pages"; import handleRoutes from "./handle-routes"; import isDevelopment from "../../utils/is-development"; -import grabConstants from "../../utils/grab-constants"; import handleHmr from "./handle-hmr"; import handlePublic from "./handle-public"; import handleFiles from "./handle-files"; import handleBunextPublicAssets from "./handle-bunext-public-assets"; +import checkExcludedPatterns from "../../utils/check-excluded-patterns"; +import { AppData } from "../../data/app-data"; +import fullRebuild from "./full-rebuild"; type Params = { req: Request; server: Bun.Server; @@ -21,12 +23,14 @@ export default async function bunextRequestHandler({ try { const url = new URL(req.url); - const { config } = grabConstants(); + if (checkExcludedPatterns({ path: url.pathname })) { + return Response.json({ success: false, msg: `Invalid Path` }); + } let response: Response | undefined = undefined; - if (config?.middleware) { - const middleware_res = await config.middleware({ + if (global.CONSTANTS.config?.middleware) { + const middleware_res = await global.CONSTANTS.config.middleware({ req: initial_req, url, }); @@ -40,11 +44,10 @@ export default async function bunextRequestHandler({ } } - // const server_upgrade = server.upgrade(req); - - // if (server_upgrade) { - // return undefined; - // } + if (is_dev && url.pathname == AppData["BunextHMRRetryRoute"]) { + await fullRebuild({ msg: `HMR Retry Rebuild ...` }); + return new Response("Modules Rebuilt"); + } if (url.pathname === "/__hmr" && is_dev) { response = await handleHmr({ req }); diff --git a/src/functions/server/server-params-gen.ts b/src/functions/server/server-params-gen.ts index 4176410..43d7b29 100644 --- a/src/functions/server/server-params-gen.ts +++ b/src/functions/server/server-params-gen.ts @@ -18,6 +18,6 @@ export default async function (): Promise> { idleTimeout: development ? 0 : undefined, development, websocket: config?.websocket, - ..._.omit(config?.serverOptions || {}, ["fetch"]), + ..._.omit(config?.server_options || {}, ["fetch"]), } as Bun.Serve.Options; } diff --git a/src/functions/server/watcher-esbuild-ctx.ts b/src/functions/server/watcher-esbuild-ctx.ts index b1a2a33..158621a 100644 --- a/src/functions/server/watcher-esbuild-ctx.ts +++ b/src/functions/server/watcher-esbuild-ctx.ts @@ -1,11 +1,9 @@ import { watch, existsSync, statSync } from "fs"; import path from "path"; import grabDirNames from "../../utils/grab-dir-names"; -import { log } from "../../utils/log"; -import allPagesESBuildContextBundler from "../bundler/all-pages-esbuild-context-bundler"; -import serverPostBuildFn from "./server-post-build-fn"; import fullRebuild from "./full-rebuild"; import { AppData } from "../../data/app-data"; +import checkExcludedPatterns from "../../utils/check-excluded-patterns"; const { ROOT_DIR } = grabDirNames(); @@ -102,7 +100,8 @@ export default async function watcherEsbuildCTX() { } if (!filename.match(/^src\/pages\/|\.css$/)) return reloadWatcher(); - if (filename.match(/\/(--|\()/)) return reloadWatcher(); + if (checkExcludedPatterns({ path: filename })) + return reloadWatcher(); if (filename.match(/ /)) return reloadWatcher(); if (global.RECOMPILING) return; 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 2a3e4af..b26c027 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 @@ -23,6 +23,10 @@ export default async function (params?: Params) { .map((e) => `args[0].includes("${e}")`) .join(" || "); + script += `let retries = 0;\n`; + script += `let retries_exhausted = false;\n`; + script += `const MAX_RETRIES = 1;\n`; + script += `const _ce = console.error.bind(console);\n`; script += `console.error = (...args) => {\n`; script += ` if (typeof args[0] === "string" && (${supress_condition})) return;\n`; @@ -35,8 +39,16 @@ export default async function (params?: Params) { script += ` const overlay = document.createElement("div");\n`; script += ` overlay.id = "__bunext_error_overlay";\n`; script += ` overlay.style.cssText = "position:fixed;inset:0;z-index:99999;background:#1a1a1a;color:#ff6b6b;font-family:monospace;font-size:14px;padding:24px;overflow:auto;";\n`; - script += ` overlay.innerHTML = \`
Runtime Error
\${message}
\${source ? \`
\${source}
\` : ""}\${stack ? \`
\${stack}
\` : ""}
\`;\n`; + script += ` overlay.innerHTML = \`
Runtime Error
\${message}
\${source ? \`
\${source}
\` : ""}\${stack ? \`
\${stack}
\` : ""}
\`;\n`; script += ` document.body.appendChild(overlay);\n`; + + script += ` if (retries < MAX_RETRIES) {\n`; + script += ` retries++\n`; + script += ` console.log(\`Retrying \${retries} ...\`)\n`; + script += ` fetch("${AppData["BunextHMRRetryRoute"]}")\n`; + script += ` } else {\n`; + script += ` retries_exhausted = true\n`; + script += ` }\n`; script += `}\n\n`; script += `function __bunext_should_suppress_runtime_error(message) {\n`; script += ` return false;\n`; @@ -66,7 +78,16 @@ export default async function (params?: Params) { script += `hmr.addEventListener("update", async (event) => {\n`; script += ` if (event?.data) {\n`; script += ` try {\n`; - script += ` document.getElementById("__bunext_error_overlay")?.remove();\n`; + script += ` if (retries_exhausted) {\n`; + script += ` document.getElementById("__bunext_error_overlay")?.remove();\n`; + script += ` retries = 0;\n`; + script += ` retries_exhausted = false;\n`; + script += ` }\n`; + + // script += ` if (retries >= MAX_RETRIES && document.getElementById("__bunext_error_overlay")) {\n`; + // script += ` retries = 0;\n`; + // script += ` }\n`; + script += ` const data = JSON.parse(event.data);\n`; // script += ` console.log("data", data);\n`; @@ -118,7 +139,9 @@ export default async function (params?: Params) { // script += ` window.location.reload();\n`; // script += ` }\n`; // script += ` console.log("newScript", newScript);\n`; + // script += ` document.getElementById("__bunext_error_overlay")?.remove();\n`; script += ` document.head.appendChild(newScript);\n\n`; + // script += ` retries = 0;\n\n`; script += ` } catch (err) {\n`; script += ` console.error("HMR update failed, falling back to reload:", err.message);\n`; diff --git a/src/types/index.ts b/src/types/index.ts index 45ded88..8205416 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -49,10 +49,10 @@ export type PageModule = { }; export type BunextConfig = { - distDir?: string; - assetsPrefix?: string; + dist_dir?: string; + assets_prefix?: string; origin?: string; - globalVars?: { [k: string]: any }; + global_vars?: { [k: string]: any }; port?: number; development?: boolean; middleware?: ( @@ -62,9 +62,14 @@ export type BunextConfig = { | Response | Request | undefined; - defaultCacheExpiry?: number; + default_cache_expiry?: number; websocket?: WebSocketHandler; - serverOptions?: Omit, "fetch" | "routes">; + server_options?: Omit, "fetch" | "routes">; + /** + * These are patterns in the pages to exclude + * from the router + */ + pages_exclude_patterns?: RegExp[]; }; export type BunextConfigMiddlewareParams = { diff --git a/src/utils/check-excluded-patterns.ts b/src/utils/check-excluded-patterns.ts new file mode 100644 index 0000000..049e4e3 --- /dev/null +++ b/src/utils/check-excluded-patterns.ts @@ -0,0 +1,14 @@ +import type { APIResponseObject } from "../types"; + +type Params = { + path: string; +}; + +export default function ({ path }: Params): boolean { + for (let i = 0; i < global.CONSTANTS.RouteIgnorePatterns.length; i++) { + const regex = global.CONSTANTS.RouteIgnorePatterns[i]; + if (path.match(regex)) return true; + } + + return false; +} diff --git a/src/utils/grab-all-pages.ts b/src/utils/grab-all-pages.ts index 44dd8a3..6c45e68 100644 --- a/src/utils/grab-all-pages.ts +++ b/src/utils/grab-all-pages.ts @@ -4,6 +4,7 @@ import path from "path"; import type { PageFiles } from "../types"; import AppNames from "./grab-app-names"; import pagePathTransform from "./page-path-transform"; +import checkExcludedPatterns from "./check-excluded-patterns"; type Params = { exclude_api?: boolean; @@ -49,7 +50,11 @@ function grabPageDirRecursively({ page_dir }: { page_dir: string }) { continue; } - if (page.match(/\(|\)|--|\/api\//)) { + if (checkExcludedPatterns({ path: page })) { + continue; + } + + if (page.match(/\/api\//)) { continue; } @@ -60,7 +65,7 @@ function grabPageDirRecursively({ page_dir }: { page_dir: string }) { const page_stat = statSync(full_page_path); if (page_stat.isDirectory()) { - if (page.match(/\(|\)/)) continue; + if (checkExcludedPatterns({ path: page })) continue; const new_page_files = grabPageDirRecursively({ page_dir: full_page_path, }); diff --git a/src/utils/grab-assets-prefix.ts b/src/utils/grab-assets-prefix.ts index 89ddc06..5eb2923 100644 --- a/src/utils/grab-assets-prefix.ts +++ b/src/utils/grab-assets-prefix.ts @@ -1,8 +1,8 @@ import AppNames from "./grab-app-names"; export default function grabAssetsPrefix() { - if (global.CONFIG.assetsPrefix) { - return global.CONFIG.assetsPrefix; + if (global.CONFIG.assets_prefix) { + return global.CONFIG.assets_prefix; } const { defaultAssetPrefix } = AppNames; diff --git a/src/utils/grab-constants.ts b/src/utils/grab-constants.ts index f61886e..c0d9051 100644 --- a/src/utils/grab-constants.ts +++ b/src/utils/grab-constants.ts @@ -9,6 +9,11 @@ export default function grabConstants() { const ServerDefaultRequestBodyLimitBytes = MB_IN_BYTES * 10; const MaxBundlerRebuilds = 5; + const RouteIgnorePatterns = [/\/\(/]; + + if (config.pages_exclude_patterns) { + RouteIgnorePatterns.push(...config.pages_exclude_patterns); + } return { ClientRootElementIDName, @@ -18,5 +23,6 @@ export default function grabConstants() { ClientRootComponentWindowName, MaxBundlerRebuilds, config, + RouteIgnorePatterns, } as const; }