diff --git a/src/functions/server/web-pages/grab-page-component.tsx b/src/functions/server/web-pages/grab-page-component.tsx index 6bdc0f6..d36fe60 100644 --- a/src/functions/server/web-pages/grab-page-component.tsx +++ b/src/functions/server/web-pages/grab-page-component.tsx @@ -11,6 +11,8 @@ import path from "path"; import AppNames from "../../../utils/grab-app-names"; import { existsSync } from "fs"; +class NotFoundError extends Error {} + type Params = { req?: Request; file_path?: string; @@ -23,7 +25,7 @@ export default async function grabPageComponent({ const url = req?.url ? new URL(req.url) : undefined; const router = grabRouter(); - const { BUNX_ROOT_500_PRESET_COMPONENT, PAGES_DIR } = grabDirNames(); + const { BUNX_ROOT_500_PRESET_COMPONENT, BUNX_ROOT_404_PRESET_COMPONENT, PAGES_DIR } = grabDirNames(); const routeParams = req ? await grabRouteParams({ req }) : undefined; @@ -31,9 +33,7 @@ export default async function grabPageComponent({ const match = url ? router.match(url.pathname) : undefined; if (!match?.filePath && url?.pathname) { - const errMsg = `Page ${url.pathname} not found`; - // console.error(errMsg); - throw new Error(errMsg); + throw new NotFoundError(`Page ${url.pathname} not found`); } const file_path = match?.filePath || passed_file_path; @@ -126,9 +126,15 @@ export default async function grabPageComponent({ meta, }; } catch (error: any) { + const is404 = error instanceof NotFoundError; + const errorRoute = is404 ? "/404" : "/500"; + const presetComponent = is404 + ? BUNX_ROOT_404_PRESET_COMPONENT + : BUNX_ROOT_500_PRESET_COMPONENT; + try { - const match = router.match("/500"); - const filePath = match?.filePath || BUNX_ROOT_500_PRESET_COMPONENT; + const match = router.match(errorRoute); + const filePath = match?.filePath || presetComponent; const bundledMap = match?.filePath ? (global.BUNDLER_CTX_MAP?.find( @@ -147,7 +153,7 @@ export default async function grabPageComponent({ bundledMap, }; } catch { - const DefaultServerError: FC = () => ( + const DefaultNotFound: FC = () => (
- 500 Internal Server Error + {is404 ? "404 Not Found" : "500 Internal Server Error"}
); return { - component: , + component: , routeParams, - module: { - default: DefaultServerError, - }, + module: { default: DefaultNotFound }, + bundledMap: {} as BundlerCTXMap, }; } } diff --git a/src/functions/server/web-pages/grab-web-meta-html.ts b/src/functions/server/web-pages/grab-web-meta-html.ts index a307388..ebb1176 100644 --- a/src/functions/server/web-pages/grab-web-meta-html.ts +++ b/src/functions/server/web-pages/grab-web-meta-html.ts @@ -4,7 +4,7 @@ type Params = { meta: BunextPageModuleMeta; }; -export default async function grabWebMetaHTML({ meta }: Params) { +export default function grabWebMetaHTML({ meta }: Params) { let html = ``; if (meta.title) { @@ -40,23 +40,36 @@ export default async function grabWebMetaHTML({ meta }: Params) { if (meta.og) { const { og } = meta; - if (og.title) html += ` \n`; - if (og.description) html += ` \n`; - if (og.image) html += ` \n`; - if (og.url) html += ` \n`; - if (og.type) html += ` \n`; - if (og.siteName) html += ` \n`; - if (og.locale) html += ` \n`; + if (og.title) + html += ` \n`; + if (og.description) + html += ` \n`; + if (og.image) + html += ` \n`; + if (og.url) + html += ` \n`; + if (og.type) + html += ` \n`; + if (og.siteName) + html += ` \n`; + if (og.locale) + html += ` \n`; } if (meta.twitter) { const { twitter } = meta; - if (twitter.card) html += ` \n`; - if (twitter.title) html += ` \n`; - if (twitter.description) html += ` \n`; - if (twitter.image) html += ` \n`; - if (twitter.site) html += ` \n`; - if (twitter.creator) html += ` \n`; + if (twitter.card) + html += ` \n`; + if (twitter.title) + html += ` \n`; + if (twitter.description) + html += ` \n`; + if (twitter.image) + html += ` \n`; + if (twitter.site) + html += ` \n`; + if (twitter.creator) + html += ` \n`; } return html; diff --git a/src/presets/not-found.tsx b/src/presets/not-found.tsx new file mode 100644 index 0000000..2119b4a --- /dev/null +++ b/src/presets/not-found.tsx @@ -0,0 +1,16 @@ +export default function DefaultNotFoundPage() { + return ( +
+ 404 Not Found +
+ ); +} diff --git a/src/utils/grab-dir-names.ts b/src/utils/grab-dir-names.ts index 4fd9b9c..a51da8e 100644 --- a/src/utils/grab-dir-names.ts +++ b/src/utils/grab-dir-names.ts @@ -30,6 +30,12 @@ export default function grabDirNames() { `${BUNX_ROOT_500_FILE_NAME}.tsx`, ); + const BUNX_ROOT_404_FILE_NAME = `not-found`; + const BUNX_ROOT_404_PRESET_COMPONENT = path.join( + BUNX_ROOT_PRESETS_DIR, + `${BUNX_ROOT_404_FILE_NAME}.tsx`, + ); + return { ROOT_DIR, SRC_DIR, @@ -45,6 +51,8 @@ export default function grabDirNames() { BUNX_ROOT_PRESETS_DIR, BUNX_ROOT_500_PRESET_COMPONENT, BUNX_ROOT_500_FILE_NAME, + BUNX_ROOT_404_PRESET_COMPONENT, + BUNX_ROOT_404_FILE_NAME, HYDRATION_DST_DIR_MAP_JSON_FILE, }; }