From 437ffd87d81aff620cc00beca6b115641439f363 Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Thu, 3 Nov 2022 14:14:07 +0100 Subject: [PATCH 1/6] fix: handle cleanUrls with subfolders when using a trailing slash --- docs/.vitepress/config.ts | 2 +- src/client/app/utils.ts | 7 +++++++ src/node/build/buildMPAClient.ts | 12 +++++++++++- src/node/build/bundle.ts | 9 ++++++++- src/node/build/render.ts | 10 +++++++++- 5 files changed, 36 insertions(+), 4 deletions(-) diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index 53a706804f46..cbf05874806e 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -7,7 +7,7 @@ export default defineConfig({ description: 'Vite & Vue powered static site generator.', lastUpdated: true, - cleanUrls: 'without-subfolders', + cleanUrls: 'with-subfolders', head: [['meta', { name: 'theme-color', content: '#3c8772' }]], diff --git a/src/client/app/utils.ts b/src/client/app/utils.ts index 4e9687dbd849..f73f0f1de919 100644 --- a/src/client/app/utils.ts +++ b/src/client/app/utils.ts @@ -26,6 +26,13 @@ export function pathToFile(path: string): string { pagePath += 'index' } + if ( + siteDataRef.value.cleanUrls === 'with-subfolders' && + !pagePath.endsWith('index') + ) { + pagePath += '/index' + } + if (import.meta.env.DEV) { // always force re-fetch content in dev pagePath += `.md?t=${Date.now()}` diff --git a/src/node/build/buildMPAClient.ts b/src/node/build/buildMPAClient.ts index c61ee940d0aa..9a8b4e73f4e3 100644 --- a/src/node/build/buildMPAClient.ts +++ b/src/node/build/buildMPAClient.ts @@ -10,7 +10,17 @@ export async function buildMPAClient( ): Promise { const files = Object.keys(js) const themeFiles = files.filter((f) => !f.endsWith('.md')) - const pages = files.filter((f) => f.endsWith('.md')) + const pages = files + .filter((f) => f.endsWith('.md')) + .map((page) => { + if ( + config.cleanUrls === 'with-subfolders' && + !page.includes('index.md') + ) { + return page.replace('.md', '_index.md') + } + return page + }) return build({ root: config.srcDir, diff --git a/src/node/build/bundle.ts b/src/node/build/bundle.ts index 3cd865c4c42e..c0bca41178e0 100644 --- a/src/node/build/bundle.ts +++ b/src/node/build/bundle.ts @@ -33,7 +33,14 @@ export async function bundle( config.pages.forEach((file) => { // page filename conversion // foo/bar.md -> foo_bar.md - input[slash(file).replace(/\//g, '_')] = path.resolve(config.srcDir, file) + // when using cleanUrls with-subfolders + // foo/bar.md -> foo_bar_index.md + let target = slash(file).replace(/\//g, '_') + if (config.cleanUrls === 'with-subfolders' && !file.includes('index.md')) { + target = target.replace('.md', '_index.md') + } + + input[target] = path.resolve(config.srcDir, file) }) // resolve options to pass to vite diff --git a/src/node/build/render.ts b/src/node/build/render.ts index 89d94167bce1..b432938f1a2a 100644 --- a/src/node/build/render.ts +++ b/src/node/build/render.ts @@ -32,7 +32,15 @@ export async function renderPage( // render page const content = await render(routePath) - const pageName = sanitizeFileName(page.replace(/\//g, '_')) + let pageName = sanitizeFileName(page.replace(/\//g, '_')) + + if ( + config.cleanUrls === 'with-subfolders' && + !pageName.includes('index.md') + ) { + pageName = pageName.replace('.md', '_index.md') + } + // server build doesn't need hash const pageServerJsFileName = pageName + '.js' // for any initial page load, we only need the lean version of the page js From 9a7e5ce92a4364b8f060c04218943df143ad6568 Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Thu, 3 Nov 2022 14:54:17 +0100 Subject: [PATCH 2/6] refactor: fallback to index.md lookup if .md fails --- src/client/app/utils.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/client/app/utils.ts b/src/client/app/utils.ts index f73f0f1de919..028df61b659d 100644 --- a/src/client/app/utils.ts +++ b/src/client/app/utils.ts @@ -22,15 +22,10 @@ export function withBase(path: string) { export function pathToFile(path: string): string { let pagePath = path.replace(/\.html$/, '') pagePath = decodeURIComponent(pagePath) - if (pagePath.endsWith('/')) { - pagePath += 'index' - } - if ( - siteDataRef.value.cleanUrls === 'with-subfolders' && - !pagePath.endsWith('index') - ) { - pagePath += '/index' + if (pagePath.endsWith('/')) { + // remove trailing slashes from pagePath + pagePath = pagePath.slice(0, -1) } if (import.meta.env.DEV) { @@ -48,7 +43,14 @@ export function pathToFile(path: string): string { ) + '.md' // client production build needs to account for page hash, which is // injected directly in the page's html - const pageHash = __VP_HASH_MAP__[pagePath.toLowerCase()] + let pageHash = __VP_HASH_MAP__[pagePath.toLowerCase()] + + if (!pageHash) { + // try looking for an index.md hash + pagePath = pagePath.replace('.md', '_index.md') + pageHash = __VP_HASH_MAP__[pagePath.toLowerCase()] + } + pagePath = `${base}assets/${pagePath}.${pageHash}.js` } else { // ssr build uses much simpler name mapping From 40db0441868103acd98cc48db2efa3e4f47f15ed Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Thu, 3 Nov 2022 17:33:41 +0100 Subject: [PATCH 3/6] revert: fix: handle cleanUrls with subfolders when using a trailing slash --- docs/.vitepress/config.ts | 2 +- src/node/build/buildMPAClient.ts | 12 +----------- src/node/build/bundle.ts | 9 +-------- src/node/build/render.ts | 10 +--------- 4 files changed, 4 insertions(+), 29 deletions(-) diff --git a/docs/.vitepress/config.ts b/docs/.vitepress/config.ts index cbf05874806e..53a706804f46 100644 --- a/docs/.vitepress/config.ts +++ b/docs/.vitepress/config.ts @@ -7,7 +7,7 @@ export default defineConfig({ description: 'Vite & Vue powered static site generator.', lastUpdated: true, - cleanUrls: 'with-subfolders', + cleanUrls: 'without-subfolders', head: [['meta', { name: 'theme-color', content: '#3c8772' }]], diff --git a/src/node/build/buildMPAClient.ts b/src/node/build/buildMPAClient.ts index 9a8b4e73f4e3..c61ee940d0aa 100644 --- a/src/node/build/buildMPAClient.ts +++ b/src/node/build/buildMPAClient.ts @@ -10,17 +10,7 @@ export async function buildMPAClient( ): Promise { const files = Object.keys(js) const themeFiles = files.filter((f) => !f.endsWith('.md')) - const pages = files - .filter((f) => f.endsWith('.md')) - .map((page) => { - if ( - config.cleanUrls === 'with-subfolders' && - !page.includes('index.md') - ) { - return page.replace('.md', '_index.md') - } - return page - }) + const pages = files.filter((f) => f.endsWith('.md')) return build({ root: config.srcDir, diff --git a/src/node/build/bundle.ts b/src/node/build/bundle.ts index c0bca41178e0..3cd865c4c42e 100644 --- a/src/node/build/bundle.ts +++ b/src/node/build/bundle.ts @@ -33,14 +33,7 @@ export async function bundle( config.pages.forEach((file) => { // page filename conversion // foo/bar.md -> foo_bar.md - // when using cleanUrls with-subfolders - // foo/bar.md -> foo_bar_index.md - let target = slash(file).replace(/\//g, '_') - if (config.cleanUrls === 'with-subfolders' && !file.includes('index.md')) { - target = target.replace('.md', '_index.md') - } - - input[target] = path.resolve(config.srcDir, file) + input[slash(file).replace(/\//g, '_')] = path.resolve(config.srcDir, file) }) // resolve options to pass to vite diff --git a/src/node/build/render.ts b/src/node/build/render.ts index b432938f1a2a..89d94167bce1 100644 --- a/src/node/build/render.ts +++ b/src/node/build/render.ts @@ -32,15 +32,7 @@ export async function renderPage( // render page const content = await render(routePath) - let pageName = sanitizeFileName(page.replace(/\//g, '_')) - - if ( - config.cleanUrls === 'with-subfolders' && - !pageName.includes('index.md') - ) { - pageName = pageName.replace('.md', '_index.md') - } - + const pageName = sanitizeFileName(page.replace(/\//g, '_')) // server build doesn't need hash const pageServerJsFileName = pageName + '.js' // for any initial page load, we only need the lean version of the page js From 3c64778755725b6390e3b4b8fd831d7c972fc57c Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Thu, 3 Nov 2022 17:34:02 +0100 Subject: [PATCH 4/6] fix: case for home page/index page --- src/client/app/utils.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/client/app/utils.ts b/src/client/app/utils.ts index 028df61b659d..199eb970f083 100644 --- a/src/client/app/utils.ts +++ b/src/client/app/utils.ts @@ -28,6 +28,12 @@ export function pathToFile(path: string): string { pagePath = pagePath.slice(0, -1) } + // if we removed the trailing slash and have an empty page path + // we are trying to render the index/home page... + if (pagePath.length === 0) { + pagePath = '/index' + } + if (import.meta.env.DEV) { // always force re-fetch content in dev pagePath += `.md?t=${Date.now()}` From affdecaf46b08bed54e607db681b644e26ecdcfa Mon Sep 17 00:00:00 2001 From: Igor Randjelovic Date: Wed, 21 Dec 2022 19:56:24 +0100 Subject: [PATCH 5/6] fix: fallback to previous behaviour in DEV mode --- src/client/app/utils.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/client/app/utils.ts b/src/client/app/utils.ts index 199eb970f083..7346c63a4eac 100644 --- a/src/client/app/utils.ts +++ b/src/client/app/utils.ts @@ -24,8 +24,14 @@ export function pathToFile(path: string): string { pagePath = decodeURIComponent(pagePath) if (pagePath.endsWith('/')) { - // remove trailing slashes from pagePath - pagePath = pagePath.slice(0, -1) + if (import.meta.env.DEV) { + // in dev, we are importing the md files directly, and if there's a trailing slash + // it's likely an index.md file + pagePath += 'index' + } else { + // remove trailing slashes from pagePath + pagePath = pagePath.slice(0, -1) + } } // if we removed the trailing slash and have an empty page path From ff6bf3522e90d1eaa9e66d3264cbc2b7dad592c5 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Thu, 22 Dec 2022 14:33:51 +0530 Subject: [PATCH 6/6] simplify stuff --- src/client/app/utils.ts | 26 +++----------------------- 1 file changed, 3 insertions(+), 23 deletions(-) diff --git a/src/client/app/utils.ts b/src/client/app/utils.ts index 7346c63a4eac..77d1959c5bad 100644 --- a/src/client/app/utils.ts +++ b/src/client/app/utils.ts @@ -22,24 +22,7 @@ export function withBase(path: string) { export function pathToFile(path: string): string { let pagePath = path.replace(/\.html$/, '') pagePath = decodeURIComponent(pagePath) - - if (pagePath.endsWith('/')) { - if (import.meta.env.DEV) { - // in dev, we are importing the md files directly, and if there's a trailing slash - // it's likely an index.md file - pagePath += 'index' - } else { - // remove trailing slashes from pagePath - pagePath = pagePath.slice(0, -1) - } - } - - // if we removed the trailing slash and have an empty page path - // we are trying to render the index/home page... - if (pagePath.length === 0) { - pagePath = '/index' - } - + pagePath = pagePath.replace(/\/$/, '/index') // /foo/ -> /foo/index if (import.meta.env.DEV) { // always force re-fetch content in dev pagePath += `.md?t=${Date.now()}` @@ -56,13 +39,10 @@ export function pathToFile(path: string): string { // client production build needs to account for page hash, which is // injected directly in the page's html let pageHash = __VP_HASH_MAP__[pagePath.toLowerCase()] - - if (!pageHash) { - // try looking for an index.md hash - pagePath = pagePath.replace('.md', '_index.md') + if (!pageHash && pagePath.endsWith('_index.md')) { + pagePath = pagePath.slice(0, -9) + '.md' pageHash = __VP_HASH_MAP__[pagePath.toLowerCase()] } - pagePath = `${base}assets/${pagePath}.${pageHash}.js` } else { // ssr build uses much simpler name mapping