fix(build): resolve additional configs by source path

Discovered config files are keyed by their source directory, but the
lookup walked the rewrite-applied relativePath, so directories covered
by rewrites silently lost their additional config. Resolve against
pageData.filePath on both server and client, which also makes dynamic
route pages inherit configs from their route template's directory.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
pull/5014/head
Divyansh Singh 2 months ago
parent 9e2148d75e
commit 5e4a9e799f

@ -29,7 +29,11 @@ export const siteDataRef: Ref<SiteData> = shallowRef(
// per-app data // per-app data
export function initData(route: Route): VitePressData { export function initData(route: Route): VitePressData {
const site = computed(() => const site = computed(() =>
resolveSiteDataByRoute(siteDataRef.value, route.data.relativePath) resolveSiteDataByRoute(
siteDataRef.value,
route.data.relativePath,
route.data.filePath
)
) )
const appearance = site.value.appearance // fine with reactivity being lost here, config change triggers a restart const appearance = site.value.appearance // fine with reactivity being lost here, config change triggers a restart

@ -34,7 +34,6 @@ export async function renderPage(
usedIcons: Set<string> usedIcons: Set<string>
) { ) {
const routePath = `/${page.replace(/\.md$/, '')}` const routePath = `/${page.replace(/\.md$/, '')}`
const siteData = resolveSiteDataByRoute(config.site, page)
// render page // render page
const context = await render(routePath) const context = await render(routePath)
@ -70,6 +69,8 @@ export async function renderPage(
} }
} }
const siteData = resolveSiteDataByRoute(config.site, page, pageData.filePath)
const title: string = createTitle(siteData, pageData) const title: string = createTitle(siteData, pageData)
const description: string = pageData.description || siteData.description const description: string = pageData.description || siteData.description
const stylesheetLink = cssChunk const stylesheetLink = cssChunk

@ -109,14 +109,20 @@ export function getLocaleForPath(
*/ */
export function resolveSiteDataByRoute( export function resolveSiteDataByRoute(
siteData: SiteData, siteData: SiteData,
relativePath: string relativePath: string,
filePath?: string
): SiteData { ): SiteData {
const localeIndex = getLocaleForPath(siteData, relativePath) const localeIndex = getLocaleForPath(siteData, relativePath)
const { label, link, markdown, ...localeConfig } = const { label, link, markdown, ...localeConfig } =
siteData.locales[localeIndex] ?? {} siteData.locales[localeIndex] ?? {}
Object.assign(localeConfig, { localeIndex }) Object.assign(localeConfig, { localeIndex })
const additionalConfigs = resolveAdditionalConfig(siteData, relativePath) // additional configs are colocated with sources, so resolve them by the
// source path (filePath) rather than the rewritten one
const additionalConfigs = resolveAdditionalConfig(
siteData,
filePath || relativePath
)
if (inBrowser && (import.meta as any).env?.DEV) { if (inBrowser && (import.meta as any).env?.DEV) {
;(localeConfig as any)[VP_SOURCE_KEY] = `locale config (${localeIndex})` ;(localeConfig as any)[VP_SOURCE_KEY] = `locale config (${localeIndex})`

15
types/shared.d.ts vendored

@ -246,9 +246,11 @@ export interface SiteData<ThemeConfig = any> {
prefetchLinks: boolean prefetchLinks: boolean
} }
/** /**
* Config overrides applied to pages by directory: either a dict mapping a * Config overrides applied to pages by source directory: either a dict
* directory (e.g. `/guide/`) to overrides, where deeper directories take * mapping a directory (e.g. `/guide/`) to overrides, where deeper
* precedence, or a function returning the overrides to apply for a page. * directories take precedence, or a function returning the overrides to
* apply for a page. Directories are resolved against the source paths of
* pages, before rewrites.
*/ */
additionalConfig?: additionalConfig?:
AdditionalConfigDict<ThemeConfig> | AdditionalConfigLoader<ThemeConfig> AdditionalConfigDict<ThemeConfig> | AdditionalConfigLoader<ThemeConfig>
@ -523,7 +525,8 @@ export type AdditionalConfig<ThemeConfig = any> =
LocaleSpecificConfig<ThemeConfig> LocaleSpecificConfig<ThemeConfig>
/** /**
* Additional configs keyed by the directory they apply to (e.g. `/guide/`). * Additional configs keyed by the source directory they apply to
* (e.g. `/guide/`).
*/ */
export type AdditionalConfigDict<ThemeConfig = any> = Record< export type AdditionalConfigDict<ThemeConfig = any> = Record<
string, string,
@ -531,11 +534,11 @@ export type AdditionalConfigDict<ThemeConfig = any> = Record<
> >
/** /**
* Resolves the additional configs of a page from its relative path, ordered * Resolves the additional configs of a page from its source path, ordered
* from highest to lowest priority. * from highest to lowest priority.
*/ */
export type AdditionalConfigLoader<ThemeConfig = any> = ( export type AdditionalConfigLoader<ThemeConfig = any> = (
relativePath: string filePath: string
) => AdditionalConfig<ThemeConfig>[] | void ) => AdditionalConfig<ThemeConfig>[] | void
// Manually declaring all properties as rollup-plugin-dts // Manually declaring all properties as rollup-plugin-dts

Loading…
Cancel
Save