feat: added not-found and error pages

This commit is contained in:
2023-01-10 01:22:49 +01:00
parent 4e417266c4
commit 478d986a7d
7 changed files with 95 additions and 44 deletions
+12 -19
View File
@@ -14,7 +14,7 @@ import { RenderFunction } from './shared/ssr'
// Load ".env"
dotenv.config()
const HTML_ROUTES = ['/', '/login', '/problem/:id', '/u/:uid', '/admin', '/profile', '/scores']
// const HTML_ROUTES = ['/', '/login', '/problem/:id', '/u/:uid', '/admin', '/profile', '/scores']
const config = {
isDevelopment: process.env.MODE === 'development',
@@ -26,12 +26,12 @@ if (config.isDevelopment) {
console.log(`[Config] PORT = ${config.port}`)
}
function mountIndexHtmlRoutes(r: Router, serveIndexHtml: Handler) {
for (const route of HTML_ROUTES) {
console.log(`[Server] Mounted index html for "${route}"`)
r.get(route, serveIndexHtml)
}
}
// function mountIndexHtmlRoutes(r: Router, serveIndexHtml: Handler) {
// for (const route of HTML_ROUTES) {
// console.log(`[Server] Mounted index html for "${route}"`)
// r.get(route, serveIndexHtml)
// }
// }
async function createDevRouter() {
const r = express.Router()
@@ -42,7 +42,8 @@ async function createDevRouter() {
})
r.use(vite.middlewares)
mountIndexHtmlRoutes(r, async (req, res, next) => {
r.use('*', async (req, res, next) => {
try {
const indexHtml = await fs.readFile(path.resolve('./index.html'), 'utf-8')
const transformedTemplate = await vite.transformIndexHtml(req.originalUrl, indexHtml)
@@ -66,10 +67,6 @@ async function createDevRouter() {
}
})
r.use('*', (_req, res) => {
res.redirect('/')
})
return r
}
@@ -81,7 +78,9 @@ async function createProductionRouter() {
const r = express.Router()
mountIndexHtmlRoutes(r, async (req, res) => {
r.use('/', express.static('dist/entry-client'))
r.use('*', async (req, res) => {
const transformedTemplate = await fs.readFile(path.resolve('./dist/entry-client/index.html'), 'utf-8')
const { html, metadata } = render(req.originalUrl)
@@ -96,12 +95,6 @@ async function createProductionRouter() {
res.send(transformedTemplate.replace('<!-- INJECT META TAGS -->', metaTagsHtml).replace('<!-- SSR OUTLET -->', html))
})
r.use('/', express.static('dist/entry-client'))
r.use('*', (_req, res) => {
res.redirect('/')
})
return r
}