diff --git a/plugins/plugin-search/src/node/prepareSearchIndex.ts b/plugins/plugin-search/src/node/prepareSearchIndex.ts index 3042e3bc..571a82a3 100644 --- a/plugins/plugin-search/src/node/prepareSearchIndex.ts +++ b/plugins/plugin-search/src/node/prepareSearchIndex.ts @@ -22,9 +22,9 @@ const indexByLocales = new Map>() const indexCache = new Map() function getIndexByLocale(locale: string, lang: string, options: SearchIndexOptions['searchOptions']) { - const segmenter = new Intl.Segmenter(lang, { granularity: 'word' }) let index = indexByLocales.get(locale) if (!index) { + const segmenter = new Intl.Segmenter(lang, { granularity: 'word' }) index = new MiniSearch({ fields: ['title', 'titles', 'text'], storeFields: ['title', 'titles'], @@ -62,6 +62,11 @@ export async function prepareSearchIndex({ }) await writeTemp(app) + if (app.env.isBuild) { + indexByLocales.clear() + indexCache.clear() + } + if (app.env.isDebug) { logger.info( `\n[${colors.green('@vuepress-plume/plugin-search')}] prepare search time spent: ${(performance.now() - start).toFixed(2)}ms`, diff --git a/theme/src/client/components/VPDoc.vue b/theme/src/client/components/VPDoc.vue index 796c4844..244b8c37 100644 --- a/theme/src/client/components/VPDoc.vue +++ b/theme/src/client/components/VPDoc.vue @@ -265,7 +265,7 @@ watch( padding: 48px 32px 0; } - .vp-doc-container:not(.has-sidebar) .container, { + .vp-doc-container:not(.has-sidebar) .container { display: flex; justify-content: center; max-width: 992px; diff --git a/theme/src/client/composables/prev-next.ts b/theme/src/client/composables/prev-next.ts index 51b292f7..f4aa8027 100644 --- a/theme/src/client/composables/prev-next.ts +++ b/theme/src/client/composables/prev-next.ts @@ -106,7 +106,7 @@ function resolveFromSidebarItems(sidebarItems: NavItemWithLink[], currentPath: s if (index === -1) return null // eslint-disable-next-line no-cond-assign - while ((index += offset) >= 0) { + while ((index += offset) >= 0 && index < sidebarItems.length) { const targetItem = sidebarItems[index] if (targetItem?.link && !SEPARATOR_RE.test(targetItem.link)) { return targetItem diff --git a/theme/src/node/autoFrontmatter/generate.ts b/theme/src/node/autoFrontmatter/generate.ts index f14133c2..6686cff8 100644 --- a/theme/src/node/autoFrontmatter/generate.ts +++ b/theme/src/node/autoFrontmatter/generate.ts @@ -26,7 +26,7 @@ async function getMarkdownInfo(relativePath: string, cwd: string): Promise<{ const raw = await fs.promises.readFile(filepath, 'utf-8') const { data, content } = matter(raw) return { - data, + data: data as AutoFrontmatterData, context: { filepath, relativePath, diff --git a/theme/src/node/prepare/prepareArticleTagColor.ts b/theme/src/node/prepare/prepareArticleTagColor.ts index 9793b61c..6b258bca 100644 --- a/theme/src/node/prepare/prepareArticleTagColor.ts +++ b/theme/src/node/prepare/prepareArticleTagColor.ts @@ -30,7 +30,7 @@ export const PRESET: TagsColorsItem[] = [ ] // { index: className } -const cache: Record = {} +let cache: Record = {} export async function prepareArticleTagColors(app: App): Promise { perf.mark('prepare:tag-colors') @@ -39,6 +39,10 @@ export async function prepareArticleTagColors(app: App): Promise { await writeTemp(app, 'internal/articleTagColors.css', css) await writeTemp(app, 'internal/articleTagColors.js', js) + if (app.env.isBuild) { + cache = {} + } + perf.log('prepare:tag-colors') } diff --git a/theme/src/node/prepare/prepareCollections.ts b/theme/src/node/prepare/prepareCollections.ts index e252beed..4c2367b4 100644 --- a/theme/src/node/prepare/prepareCollections.ts +++ b/theme/src/node/prepare/prepareCollections.ts @@ -9,7 +9,7 @@ export async function prepareCollections(app: App): Promise { const { collections: fallback, locales } = getThemeConfig() - const data: Record = {} + let data: Record = {} for (const [locale, opt] of Object.entries(locales || {})) { let collections = opt.collections @@ -32,5 +32,9 @@ export async function prepareCollections(app: App): Promise { const content = resolveContent(app, { name: 'collections', content: data }) await writeTemp(app, 'internal/collectionsData.js', content) + if (app.env.isBuild) { + data = {} + } + perf.log('prepare:collections') } diff --git a/theme/src/node/prepare/prepareEncrypt.ts b/theme/src/node/prepare/prepareEncrypt.ts index b1adf32c..ac22b686 100644 --- a/theme/src/node/prepare/prepareEncrypt.ts +++ b/theme/src/node/prepare/prepareEncrypt.ts @@ -42,7 +42,7 @@ export async function prepareEncrypt(app: App): Promise { content: resolvedEncrypt, })) - fsCache?.write([currentHash, resolvedEncrypt]) + fsCache?.write([currentHash, resolvedEncrypt], app.env.isBuild) perf.log('prepare:encrypt') } diff --git a/theme/src/node/prepare/prepareIcons.ts b/theme/src/node/prepare/prepareIcons.ts index 25860317..c6a0b632 100644 --- a/theme/src/node/prepare/prepareIcons.ts +++ b/theme/src/node/prepare/prepareIcons.ts @@ -40,7 +40,7 @@ let locate!: ((name: string) => any) let fsCache: FsCache | null = null // { iconName: { className, content } } -const cache: IconDataMap = {} +let cache: IconDataMap = {} // 旧版本内置图标别名,映射回 simple-icons 集合中的名称 const socialFallbacks: Record = { @@ -135,7 +135,11 @@ export async function prepareIcons(app: App): Promise { })), ]) - fsCache?.write(cache) + fsCache?.write(cache, app.env.isBuild) + + if (app.env.isBuild) { + cache = {} + } perf.log('prepare:icons:total') } diff --git a/theme/src/node/utils/createFsCache.ts b/theme/src/node/utils/createFsCache.ts index 971a994e..0204c047 100644 --- a/theme/src/node/utils/createFsCache.ts +++ b/theme/src/node/utils/createFsCache.ts @@ -12,7 +12,7 @@ export interface FsCache { hash: string data: T | null read: () => Promise - write: (data: T) => Promise + write: (data: T, clear?: boolean) => Promise } const CACHE_BASE = 'markdown' @@ -37,7 +37,7 @@ export function createFsCache(app: App, name: string): FsCache { } let timer: NodeJS.Timeout | null = null - const write = async (data: T) => { + const write = async (data: T, clear?: boolean) => { const currentHash = hash(data) if (cache.hash && currentHash === cache.hash) return @@ -49,6 +49,10 @@ export function createFsCache(app: App, name: string): FsCache { timer = setTimeout(async () => { await fs.mkdir(path.dirname(filepath), { recursive: true }) await fs.writeFile(filepath, JSON.stringify(cache), 'utf-8') + if (clear) { + cache.data = null + cache.hash = '' + } }, 300) }