From 77a93f85074668b0f7d7167bb0712f2ab7fe5b30 Mon Sep 17 00:00:00 2001 From: Ayo Date: Sat, 7 Oct 2023 22:45:45 +0200 Subject: [PATCH] feat: nested sub pages --- pages/about.html | 11 +++++++++++ pages/about/index.html | 11 +++++++++++ pages/about/me.html | 11 +++++++++++ routes/[...index].ts | 19 +++++++++++++++++-- 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 pages/about.html create mode 100644 pages/about/index.html create mode 100644 pages/about/me.html diff --git a/pages/about.html b/pages/about.html new file mode 100644 index 0000000..1fa5293 --- /dev/null +++ b/pages/about.html @@ -0,0 +1,11 @@ + + + + + + top about + + +

Top Level About

+ + diff --git a/pages/about/index.html b/pages/about/index.html new file mode 100644 index 0000000..3c8c9e6 --- /dev/null +++ b/pages/about/index.html @@ -0,0 +1,11 @@ + + + + + + About Me + + +

About Me

+ + diff --git a/pages/about/me.html b/pages/about/me.html new file mode 100644 index 0000000..2a8fef3 --- /dev/null +++ b/pages/about/me.html @@ -0,0 +1,11 @@ + + + + + + About Meeeee + + +

About Meeeeeezzz

+ + diff --git a/routes/[...index].ts b/routes/[...index].ts index 7e3bb5b..59f006b 100644 --- a/routes/[...index].ts +++ b/routes/[...index].ts @@ -1,7 +1,22 @@ import * as fs from "fs"; export default eventHandler((event) => { const filename = event.path === "/" ? "/index.html" : `${event.path}.html`; - const path = `./src/pages${filename}`; - const html = fs.readFileSync(path, "utf8"); + const fallback = getPath(event.path + "/index.html"); + const path = getPath(filename); + let html = ""; + try { + html = fs.readFileSync(path, "utf8"); + } catch (error) { + try { + html = fs.readFileSync(fallback, "utf8"); + } catch { + html = `cannot find ${path} or ${fallback}`; + } + } + return html; }); + +function getPath(filename: string) { + return `./pages${filename}`; +}