From 9b603e1c10b7bf1dbed6d6ad176559bde6b73455 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Tue, 7 Jun 2022 20:11:23 +0530 Subject: [PATCH 1/4] feat: emit 404.html to dist --- src/client/app/router.ts | 2 +- src/node/build/build.ts | 3 +- src/node/build/render.ts | 60 ++++++++++++++++++++++++++-------------- 3 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/client/app/router.ts b/src/client/app/router.ts index 2eca5b7b41c6..b4304585206c 100644 --- a/src/client/app/router.ts +++ b/src/client/app/router.ts @@ -109,7 +109,7 @@ export function createRouter( } } } catch (err: any) { - if (!err.message.match(/fetch/)) { + if (!err.message.match(/fetch/) && !href.match(/404\.html/)) { console.error(err) } diff --git a/src/node/build/build.ts b/src/node/build/build.ts index 91c08742316e..6ccce84f6f66 100644 --- a/src/node/build/build.ts +++ b/src/node/build/build.ts @@ -49,7 +49,8 @@ export async function build( // as JS object literal. const hashMapString = JSON.stringify(JSON.stringify(pageToHashMap)) - for (const page of siteConfig.pages) { + const pages = ['404.md', ...siteConfig.pages] + for (const page of pages) { await renderPage( siteConfig, page, diff --git a/src/node/build/render.ts b/src/node/build/render.ts index 3345342a10d5..84de2e020be2 100644 --- a/src/node/build/render.ts +++ b/src/node/build/render.ts @@ -5,7 +5,7 @@ import { pathToFileURL } from 'url' import escape from 'escape-html' import { normalizePath, transformWithEsbuild } from 'vite' import { RollupOutput, OutputChunk, OutputAsset } from 'rollup' -import { HeadConfig, createTitle } from '../shared' +import { HeadConfig, createTitle, PageData } from '../shared' import { slash } from '../utils/slash' import { SiteConfig, resolveSiteDataByRoute } from '../config' @@ -52,28 +52,46 @@ export async function renderPage( const pageHash = pageToHashMap[pageName.toLowerCase()] const pageClientJsFileName = `assets/${pageName}.${pageHash}.lean.js` - // resolve page data so we can render head tags - const { __pageData } = await import( - pathToFileURL(path.join(config.tempDir, pageServerJsFileName)).toString() - ) - const pageData = JSON.parse(__pageData) + let pageData: PageData + let hasCustom404 = true + + try { + // resolve page data so we can render head tags + const { __pageData } = await import( + pathToFileURL(path.join(config.tempDir, pageServerJsFileName)).toString() + ) + pageData = JSON.parse(__pageData) + } catch (e) { + if (page === '404.md') { + hasCustom404 = false + pageData = { + relativePath: '', + title: '404', + description: 'Not Found', + headers: [], + frontmatter: {}, + lastUpdated: 0 + } + } else throw e + } - let preloadLinks = config.mpa - ? appChunk - ? [appChunk.fileName] + let preloadLinks = + config.mpa || (!hasCustom404 && page === '404.md') + ? appChunk + ? [appChunk.fileName] + : [] + : result && appChunk + ? [ + ...new Set([ + // resolve imports for index.js + page.md.js and inject script tags for + // them as well so we fetch everything as early as possible without having + // to wait for entry chunks to parse + ...resolvePageImports(config, page, result, appChunk), + pageClientJsFileName, + appChunk.fileName + ]) + ] : [] - : result && appChunk - ? [ - ...new Set([ - // resolve imports for index.js + page.md.js and inject script tags for - // them as well so we fetch everything as early as possible without having - // to wait for entry chunks to parse - ...resolvePageImports(config, page, result, appChunk), - pageClientJsFileName, - appChunk.fileName - ]) - ] - : [] let prefetchLinks: string[] = [] From 9aed4d1ea9cece07b048464cac9c50aa0d77bd85 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Tue, 7 Jun 2022 20:18:53 +0530 Subject: [PATCH 2/4] fix: make 404 regex strict --- src/client/app/router.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/app/router.ts b/src/client/app/router.ts index b4304585206c..4a6a277e8957 100644 --- a/src/client/app/router.ts +++ b/src/client/app/router.ts @@ -109,7 +109,7 @@ export function createRouter( } } } catch (err: any) { - if (!err.message.match(/fetch/) && !href.match(/404\.html/)) { + if (!err.message.match(/fetch/) && !href.match(/^[\\/]404\.html$/)) { console.error(err) } From c4e8df5cc7fd373b50d2ca204b7966fef74f435a Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Tue, 7 Jun 2022 20:26:12 +0530 Subject: [PATCH 3/4] refactor: share notFoundPageData --- src/client/app/router.ts | 11 +---------- src/node/build/render.ts | 11 ++--------- src/shared/shared.ts | 9 +++++++++ 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/src/client/app/router.ts b/src/client/app/router.ts index 4a6a277e8957..d978c92be14b 100644 --- a/src/client/app/router.ts +++ b/src/client/app/router.ts @@ -1,6 +1,6 @@ import { reactive, inject, markRaw, nextTick, readonly } from 'vue' import type { Component, InjectionKey } from 'vue' -import { PageData } from '../shared' +import { notFoundPageData, PageData } from '../shared' import { inBrowser, withBase } from './utils' import { siteDataRef } from './data' @@ -21,15 +21,6 @@ export const RouterSymbol: InjectionKey = Symbol() // matter and is only passed to support same-host hrefs. const fakeHost = `http://a.com` -const notFoundPageData: PageData = { - relativePath: '', - title: '404', - description: 'Not Found', - headers: [], - frontmatter: {}, - lastUpdated: 0 -} - const getDefaultRoute = (): Route => ({ path: '/', component: null, diff --git a/src/node/build/render.ts b/src/node/build/render.ts index 84de2e020be2..ba6cd4d290a7 100644 --- a/src/node/build/render.ts +++ b/src/node/build/render.ts @@ -5,7 +5,7 @@ import { pathToFileURL } from 'url' import escape from 'escape-html' import { normalizePath, transformWithEsbuild } from 'vite' import { RollupOutput, OutputChunk, OutputAsset } from 'rollup' -import { HeadConfig, createTitle, PageData } from '../shared' +import { HeadConfig, createTitle, PageData, notFoundPageData } from '../shared' import { slash } from '../utils/slash' import { SiteConfig, resolveSiteDataByRoute } from '../config' @@ -64,14 +64,7 @@ export async function renderPage( } catch (e) { if (page === '404.md') { hasCustom404 = false - pageData = { - relativePath: '', - title: '404', - description: 'Not Found', - headers: [], - frontmatter: {}, - lastUpdated: 0 - } + pageData = notFoundPageData } else throw e } diff --git a/src/shared/shared.ts b/src/shared/shared.ts index f296f026d185..d5cda2cbf00a 100644 --- a/src/shared/shared.ts +++ b/src/shared/shared.ts @@ -126,3 +126,12 @@ function cleanRoute(siteData: SiteData, route: string): string { return route.slice(baseWithoutSuffix.length) } + +export const notFoundPageData: PageData = { + relativePath: '', + title: '404', + description: 'Not Found', + headers: [], + frontmatter: {}, + lastUpdated: 0 +} From 9cfdc86ce828cd0856da6ee985adbfc7f3153cc0 Mon Sep 17 00:00:00 2001 From: Kia Ishii Date: Tue, 14 Jun 2022 16:59:20 +0900 Subject: [PATCH 4/4] style: format --- src/client/app/router.ts | 2 +- src/node/build/build.ts | 1 + src/node/build/render.ts | 12 +++++++----- src/shared/shared.ts | 18 +++++++++--------- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/client/app/router.ts b/src/client/app/router.ts index d978c92be14b..57398144cc72 100644 --- a/src/client/app/router.ts +++ b/src/client/app/router.ts @@ -1,6 +1,6 @@ import { reactive, inject, markRaw, nextTick, readonly } from 'vue' import type { Component, InjectionKey } from 'vue' -import { notFoundPageData, PageData } from '../shared' +import { PageData, notFoundPageData } from '../shared' import { inBrowser, withBase } from './utils' import { siteDataRef } from './data' diff --git a/src/node/build/build.ts b/src/node/build/build.ts index 6ccce84f6f66..f35ad51059ee 100644 --- a/src/node/build/build.ts +++ b/src/node/build/build.ts @@ -50,6 +50,7 @@ export async function build( const hashMapString = JSON.stringify(JSON.stringify(pageToHashMap)) const pages = ['404.md', ...siteConfig.pages] + for (const page of pages) { await renderPage( siteConfig, diff --git a/src/node/build/render.ts b/src/node/build/render.ts index ba6cd4d290a7..7be6ee2584eb 100644 --- a/src/node/build/render.ts +++ b/src/node/build/render.ts @@ -5,7 +5,7 @@ import { pathToFileURL } from 'url' import escape from 'escape-html' import { normalizePath, transformWithEsbuild } from 'vite' import { RollupOutput, OutputChunk, OutputAsset } from 'rollup' -import { HeadConfig, createTitle, PageData, notFoundPageData } from '../shared' +import { HeadConfig, PageData, createTitle, notFoundPageData } from '../shared' import { slash } from '../utils/slash' import { SiteConfig, resolveSiteDataByRoute } from '../config' @@ -65,7 +65,9 @@ export async function renderPage( if (page === '404.md') { hasCustom404 = false pageData = notFoundPageData - } else throw e + } else { + throw e + } } let preloadLinks = @@ -76,9 +78,9 @@ export async function renderPage( : result && appChunk ? [ ...new Set([ - // resolve imports for index.js + page.md.js and inject script tags for - // them as well so we fetch everything as early as possible without having - // to wait for entry chunks to parse + // resolve imports for index.js + page.md.js and inject script tags + // for them as well so we fetch everything as early as possible + // without having to wait for entry chunks to parse ...resolvePageImports(config, page, result, appChunk), pageClientJsFileName, appChunk.fileName diff --git a/src/shared/shared.ts b/src/shared/shared.ts index d5cda2cbf00a..ebcdc67b4de0 100644 --- a/src/shared/shared.ts +++ b/src/shared/shared.ts @@ -15,6 +15,15 @@ export const APPEARANCE_KEY = 'vitepress-theme-appearance' // @ts-ignore export const inBrowser = typeof window !== 'undefined' +export const notFoundPageData: PageData = { + relativePath: '', + title: '404', + description: 'Not Found', + headers: [], + frontmatter: {}, + lastUpdated: 0 +} + function findMatchRoot(route: string, roots: string[]): string | undefined { // first match to the routes with the most deep level. roots.sort((a, b) => { @@ -126,12 +135,3 @@ function cleanRoute(siteData: SiteData, route: string): string { return route.slice(baseWithoutSuffix.length) } - -export const notFoundPageData: PageData = { - relativePath: '', - title: '404', - description: 'Not Found', - headers: [], - frontmatter: {}, - lastUpdated: 0 -}