From bcd8c2586ffd935b1b63f17e7a8801502e2f17f5 Mon Sep 17 00:00:00 2001 From: Joe Pea Date: Sun, 22 Jan 2023 10:54:50 -0800 Subject: [PATCH 1/2] routerMode:history for the docsify site --- 404.html | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ index.html | 30 ++++++++++++++++++++++++++++++ server.js | 24 +++++++++++++++++++++++- 3 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 404.html diff --git a/404.html b/404.html new file mode 100644 index 000000000..0c6594404 --- /dev/null +++ b/404.html @@ -0,0 +1,49 @@ + + + + + + Single Page Apps for GitHub Pages + + + +

404

+ + diff --git a/index.html b/index.html index 19e6e0fa6..17331f7dd 100644 --- a/index.html +++ b/index.html @@ -20,6 +20,34 @@ width: auto !important; } + + + + + @@ -35,6 +63,8 @@ // Docsify configuration window.$docsify = { + routerMode: 'history', + externalLinkTarget: '_self', alias: { '.*?/awesome': 'https://raw.githubusercontent.com/docsifyjs/awesome-docsify/master/README.md', diff --git a/server.js b/server.js index 2ba6c0439..0b9dabc41 100644 --- a/server.js +++ b/server.js @@ -1,3 +1,5 @@ +const fs = require('fs'); +const path = require('path'); const liveServer = require('live-server') const isSSR = !!process.env.SSR const middleware = [] @@ -46,10 +48,30 @@ if (isSSR) { }) } +// emulate default behavior of GitHub Pages and other static servers that +// serve a 404.html page when files are not found. +middleware.push( + /** + * @param {import('http').IncomingMessage} req + * @param {import('http').ServerResponse} res + * @param {(error?: Error) => void} res + */ + function (req, res, next) { + if (!fs.existsSync(path.resolve('.' + req.url))) { + console.log(res.statusCode = 404) + } + next(); + } +) + const params = { port: 3000, watch: ['lib', 'docs', 'themes'], - middleware + middleware, + + // emulate default behavior of GitHub Pages and other static servers that + // serve a 404.html page when files are not found. + file: '404.html', } liveServer.start(params) From 61b94dd4662ba72cdaccc0958733f07ecfe6ca6e Mon Sep 17 00:00:00 2001 From: Joe Pea Date: Sun, 22 Jan 2023 17:18:13 -0800 Subject: [PATCH 2/2] add vercel.json config file, force all paths to load /index.html for single-page app mode with routerMode:history --- vercel.json | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 vercel.json diff --git a/vercel.json b/vercel.json new file mode 100644 index 000000000..0f32683a9 --- /dev/null +++ b/vercel.json @@ -0,0 +1,3 @@ +{ + "rewrites": [{ "source": "/(.*)", "destination": "/index.html" }] +}