Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/node/serve/serve.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import fs from 'fs'
import path from 'path'
import sirv from 'sirv'
import compression from 'compression'
import polka from 'polka'
Expand Down Expand Up @@ -26,14 +28,21 @@ export async function serve(options: ServeOptions = {}) {
const site = await resolveConfig(options.root, 'serve', 'production')
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 serve = sirv(site.outDir, {
etag: true,
single: true,
maxAge: 31536000,
immutable: true,
setHeaders(res, pathname) {
if (!pathname.includes('/assets/')) {
if (notAnAsset(pathname)) {
// force server validation for non-asset files since they
// are not fingerprinted
res.setHeader('cache-control', 'no-cache')
Expand All @@ -42,14 +51,14 @@ export async function serve(options: ServeOptions = {}) {
})

if (base) {
polka()
polka({ onNoMatch })
.use(base, compress, serve)
.listen(port, (err: any) => {
if (err) throw err
console.log(`Built site served at http://localhost:${port}/${base}/\n`)
})
} else {
polka()
polka({ onNoMatch })
.use(compress, serve)
.listen(port, (err: any) => {
if (err) throw err
Expand Down
2 changes: 1 addition & 1 deletion src/shared/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const notFoundPageData: PageData = {
title: '404',
description: 'Not Found',
headers: [],
frontmatter: {},
frontmatter: { sidebar: false, layout: 'page' },
lastUpdated: 0
}

Expand Down