fix: layout inconsistencies and remove sidebar from 404 page (#964)

pull/969/head
Divyansh Singh 3 years ago committed by GitHub
parent d91f3b1b7d
commit 0257ea88dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,3 +1,5 @@
import fs from 'fs'
import path from 'path'
import sirv from 'sirv' import sirv from 'sirv'
import compression from 'compression' import compression from 'compression'
import polka from 'polka' import polka from 'polka'
@ -26,14 +28,21 @@ export async function serve(options: ServeOptions = {}) {
const site = await resolveConfig(options.root, 'serve', 'production') const site = await resolveConfig(options.root, 'serve', 'production')
const base = trimChar(options?.base ?? site?.site?.base ?? '', '/') const base = trimChar(options?.base ?? site?.site?.base ?? '', '/')
const notAnAsset = (pathname: string) => !pathname.includes('/assets/')
const notFound = fs.readFileSync(path.resolve(site.outDir, './404.html'))
const onNoMatch: polka.Options['onNoMatch'] = (req, res) => {
res.statusCode = 404
if (notAnAsset(req.path)) res.write(notFound.toString())
res.end()
}
const compress = compression() const compress = compression()
const serve = sirv(site.outDir, { const serve = sirv(site.outDir, {
etag: true, etag: true,
single: true,
maxAge: 31536000, maxAge: 31536000,
immutable: true, immutable: true,
setHeaders(res, pathname) { setHeaders(res, pathname) {
if (!pathname.includes('/assets/')) { if (notAnAsset(pathname)) {
// force server validation for non-asset files since they // force server validation for non-asset files since they
// are not fingerprinted // are not fingerprinted
res.setHeader('cache-control', 'no-cache') res.setHeader('cache-control', 'no-cache')
@ -42,14 +51,14 @@ export async function serve(options: ServeOptions = {}) {
}) })
if (base) { if (base) {
polka() polka({ onNoMatch })
.use(base, compress, serve) .use(base, compress, serve)
.listen(port, (err: any) => { .listen(port, (err: any) => {
if (err) throw err if (err) throw err
console.log(`Built site served at http://localhost:${port}/${base}/\n`) console.log(`Built site served at http://localhost:${port}/${base}/\n`)
}) })
} else { } else {
polka() polka({ onNoMatch })
.use(compress, serve) .use(compress, serve)
.listen(port, (err: any) => { .listen(port, (err: any) => {
if (err) throw err if (err) throw err

@ -21,7 +21,7 @@ export const notFoundPageData: PageData = {
title: '404', title: '404',
description: 'Not Found', description: 'Not Found',
headers: [], headers: [],
frontmatter: {}, frontmatter: { sidebar: false, layout: 'page' },
lastUpdated: 0 lastUpdated: 0
} }

Loading…
Cancel
Save