fix(build): better align server and client side filename sanitization (#1488)

pull/1448/head
Divyansh Singh 2 years ago committed by GitHub
parent 0b616657ee
commit 1de0069a53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,5 +1,5 @@
import { siteDataRef } from './data.js' import { siteDataRef } from './data.js'
import { inBrowser, EXTERNAL_URL_RE } from '../shared.js' import { inBrowser, EXTERNAL_URL_RE, sanitizeFileName } from '../shared.js'
export { inBrowser } export { inBrowser }
@ -36,14 +36,18 @@ export function pathToFile(path: string): string {
if (inBrowser) { if (inBrowser) {
const base = import.meta.env.BASE_URL const base = import.meta.env.BASE_URL
pagePath = pagePath =
(pagePath.slice(base.length).replace(/\//g, '_') || 'index') + '.md' sanitizeFileName(
pagePath.slice(base.length).replace(/\//g, '_') || 'index'
) + '.md'
// client production build needs to account for page hash, which is // client production build needs to account for page hash, which is
// injected directly in the page's html // injected directly in the page's html
const pageHash = __VP_HASH_MAP__[pagePath.toLowerCase()] const pageHash = __VP_HASH_MAP__[pagePath.toLowerCase()]
pagePath = `${base}assets/${pagePath}.${pageHash}.js` pagePath = `${base}assets/${pagePath}.${pageHash}.js`
} else { } else {
// ssr build uses much simpler name mapping // ssr build uses much simpler name mapping
pagePath = `./${pagePath.slice(1).replace(/\//g, '_')}.md.js` pagePath = `./${sanitizeFileName(
pagePath.slice(1).replace(/\//g, '_')
)}.md.js`
} }
} }

@ -7,6 +7,7 @@ import { slash } from '../utils/slash'
import { SiteConfig } from '../config' import { SiteConfig } from '../config'
import { APP_PATH } from '../alias' import { APP_PATH } from '../alias'
import { createVitePressPlugin } from '../plugin' import { createVitePressPlugin } from '../plugin'
import { sanitizeFileName } from '../shared'
import { buildMPAClient } from './buildMPAClient' import { buildMPAClient } from './buildMPAClient'
export const okMark = '\x1b[32m✓\x1b[0m' export const okMark = '\x1b[32m✓\x1b[0m'
@ -68,6 +69,7 @@ export async function bundle(
// other // other
preserveEntrySignatures: 'allow-extension', preserveEntrySignatures: 'allow-extension',
output: { output: {
sanitizeFileName,
...rollupOptions?.output, ...rollupOptions?.output,
...(ssr ...(ssr
? { ? {

@ -10,7 +10,8 @@ import {
createTitle, createTitle,
notFoundPageData, notFoundPageData,
mergeHead, mergeHead,
EXTERNAL_URL_RE EXTERNAL_URL_RE,
sanitizeFileName
} from '../shared' } from '../shared'
import { slash } from '../utils/slash' import { slash } from '../utils/slash'
import { SiteConfig, resolveSiteDataByRoute } from '../config' import { SiteConfig, resolveSiteDataByRoute } from '../config'
@ -31,7 +32,7 @@ export async function renderPage(
// render page // render page
const content = await render(routePath) const content = await render(routePath)
const pageName = page.replace(/\//g, '_') const pageName = sanitizeFileName(page.replace(/\//g, '_'))
// server build doesn't need hash // server build doesn't need hash
const pageServerJsFileName = pageName + '.js' const pageServerJsFileName = pageName + '.js'
// for any initial page load, we only need the lean version of the page js // for any initial page load, we only need the lean version of the page js

@ -161,3 +161,21 @@ function hasTag(head: HeadConfig[], tag: HeadConfig) {
export function mergeHead(prev: HeadConfig[], curr: HeadConfig[]) { export function mergeHead(prev: HeadConfig[], curr: HeadConfig[]) {
return [...prev.filter((tagAttrs) => !hasTag(curr, tagAttrs)), ...curr] return [...prev.filter((tagAttrs) => !hasTag(curr, tagAttrs)), ...curr]
} }
// https://github.com/rollup/rollup/blob/fec513270c6ac350072425cc045db367656c623b/src/utils/sanitizeFileName.ts
const INVALID_CHAR_REGEX = /[\u0000-\u001F"#$&*+,:;<=>?[\]^`{|}\u007F]/g
const DRIVE_LETTER_REGEX = /^[a-z]:/i
export function sanitizeFileName(name: string): string {
const match = DRIVE_LETTER_REGEX.exec(name)
const driveLetter = match ? match[0] : ''
return (
driveLetter +
name
.slice(driveLetter.length)
.replace(INVALID_CHAR_REGEX, '_')
.replace(/(^|\/)_+(?=[^/]*$)/, '$1')
)
}

Loading…
Cancel
Save