feat: use pages directory for custom server assets
This commit is contained in:
parent
77a93f8507
commit
f6ed8ead7b
3 changed files with 12 additions and 10 deletions
|
@ -1,9 +1,9 @@
|
||||||
//https://nitro.unjs.io/config
|
//https://nitro.unjs.io/config
|
||||||
export default defineNitroConfig({
|
export default defineNitroConfig({
|
||||||
devStorage: {
|
serverAssets: [
|
||||||
db: {
|
{
|
||||||
driver: "fs",
|
baseName: "pages",
|
||||||
base: "./data/db",
|
dir: "./pages",
|
||||||
},
|
},
|
||||||
},
|
],
|
||||||
});
|
});
|
||||||
|
|
|
@ -4,7 +4,8 @@
|
||||||
"prepare": "nitropack prepare",
|
"prepare": "nitropack prepare",
|
||||||
"dev": "nitropack dev",
|
"dev": "nitropack dev",
|
||||||
"build": "nitropack build",
|
"build": "nitropack build",
|
||||||
"preview": "node .output/server/index.mjs"
|
"preview": "node .output/server/index.mjs",
|
||||||
|
"build:preview": "npm run build && npm run preview"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"fs": "^0.0.1-security",
|
"fs": "^0.0.1-security",
|
||||||
|
|
|
@ -1,14 +1,15 @@
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
export default eventHandler((event) => {
|
export default eventHandler(async (event) => {
|
||||||
const filename = event.path === "/" ? "/index.html" : `${event.path}.html`;
|
const filename = event.path === "/" ? "/index.html" : `${event.path}.html`;
|
||||||
const fallback = getPath(event.path + "/index.html");
|
const fallback = getPath(event.path + "/index.html");
|
||||||
const path = getPath(filename);
|
const path = getPath(filename);
|
||||||
let html = "";
|
let html = "";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
html = fs.readFileSync(path, "utf8");
|
html = await useStorage().getItem(path);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
try {
|
try {
|
||||||
html = fs.readFileSync(fallback, "utf8");
|
html = await useStorage().getItem(fallback);
|
||||||
} catch {
|
} catch {
|
||||||
html = `cannot find ${path} or ${fallback}`;
|
html = `cannot find ${path} or ${fallback}`;
|
||||||
}
|
}
|
||||||
|
@ -18,5 +19,5 @@ export default eventHandler((event) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
function getPath(filename: string) {
|
function getPath(filename: string) {
|
||||||
return `./pages${filename}`;
|
return `assets/pages${filename}`;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue