-
Notifications
You must be signed in to change notification settings - Fork 140
Add Pagefind incremental indexing #2890
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
428efd5
4152c79
b1b192b
c954ca9
e821cbb
326ce05
c2ae33e
14dd6dc
555d1c3
d62750d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -35,7 +35,8 @@ const addHandler = (site: any, onePagePath?: boolean) => (filePath: string): voi | |||||||||||
| } | ||||||||||||
| Promise.resolve().then(async () => { | ||||||||||||
| if (site.isFilepathAPage(filePath) || site.isDependencyOfPage(filePath)) { | ||||||||||||
| return site.rebuildSourceFiles(); | ||||||||||||
| await site.rebuildSourceFiles(); | ||||||||||||
| return await site.updatePagefindIndex(filePath); | ||||||||||||
| } | ||||||||||||
| return site.buildAsset(filePath); | ||||||||||||
| }).catch((err: Error) => { | ||||||||||||
|
|
@@ -59,7 +60,8 @@ const changeHandler = (site: any, onePagePath?: boolean) => (filePath: string): | |||||||||||
| return site.reloadSiteConfig(); | ||||||||||||
| } | ||||||||||||
| if (site.isDependencyOfPage(filePath)) { | ||||||||||||
| return site.rebuildAffectedSourceFiles(filePath); | ||||||||||||
| await site.rebuildAffectedSourceFiles(filePath); | ||||||||||||
| return await site.updatePagefindIndex(filePath); | ||||||||||||
|
||||||||||||
| return await site.updatePagefindIndex(filePath); | |
| return await site.indexSiteWithPagefind(); |
Copilot
AI
Apr 16, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removeHandler now calls site.indexSiteWithPagefind() unconditionally after rebuildSourceFiles(). Unlike generate(), this bypasses the enableSearch guard and will attempt to build a Pagefind index even when enableSearch: false. Add an enableSearch check before calling indexSiteWithPagefind, or make Site/SiteGenerationManager.indexSiteWithPagefind a no-op when search is disabled.
| return await site.indexSiteWithPagefind(); | |
| if (site.siteConfig?.enableSearch) { | |
| return await site.indexSiteWithPagefind(); | |
| } | |
| return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fair enuf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
regarding the asynchronous update, perhaps we don't await this instead
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -84,6 +84,9 @@ export class SiteGenerationManager { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| currentOpenedPages: string[]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| toRebuild: Set<string>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Pagefind index state (kept in memory for serve mode for incremental updates) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pagefindIndex: any; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| constructor(rootPath: string, outputPath: string, onePagePath: string, forceReload = false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| siteConfigPath = SITE_CONFIG_NAME, isDevMode: any, backgroundBuildMode: boolean, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| postBackgroundBuildFunc: () => void) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -105,6 +108,9 @@ export class SiteGenerationManager { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.currentOpenedPages = []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.toRebuild = new Set(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Pagefind index state (kept in memory for serve mode for incremental updates) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.pagefindIndex = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| configure(siteAssets: SiteAssetsManager, sitePages: SitePagesManager) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -317,7 +323,15 @@ export class SiteGenerationManager { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await this.siteAssets.copyMaterialIconsAsset(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await this.writeSiteData(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (this.siteConfig.enableSearch) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const indexingSucceeded = await this.indexSiteWithPagefind(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let indexingSucceeded: boolean; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (this.onePagePath) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const builtPages = this.sitePages.pages.filter(page => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fs.existsSync(page.pageConfig.resultPath), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| indexingSucceeded = await this.updatePagefindIndex(builtPages); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| indexingSucceeded = await this.indexSiteWithPagefind(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.sitePages.pagefindIndexingSucceeded = indexingSucceeded; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.calculateBuildTimeForGenerate(startTime, lazyWebsiteGenerationString); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -440,6 +454,11 @@ export class SiteGenerationManager { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this._setTimestampVariable(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await this.runPageGenerationTasks([pageGenerationTask]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await this.writeSiteData(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (this.siteConfig.enableSearch && this.pagefindIndex) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await this.updatePagefindIndex(pagesToRebuild); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SiteGenerationManager.calculateBuildTimeForRebuildPagesBeingViewed(startTime); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (err) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await SiteGenerationManager.rejectHandler(err, [this.tempPath, this.outputPath]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -466,6 +485,11 @@ export class SiteGenerationManager { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const isCompleted = await this.generatePagesMarkedToRebuild(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isCompleted) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.info('Background building completed!'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (this.siteConfig.enableSearch) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await this.indexSiteWithPagefind(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.postBackgroundBuildFunc(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -872,29 +896,49 @@ export class SiteGenerationManager { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Indexes all the pages of the site using pagefind. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @returns true if indexing succeeded and pagefind assets were written, false otherwise. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Initializes a new Pagefind index with proper configuration. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @returns The created index object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private async initializePagefindIndex(): Promise<any> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { createIndex } = pagefind; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const pagefindConfig = this.siteConfig.pagefind || {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const createIndexOptions: Record<string, unknown> = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| keepIndexUrl: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| verbose: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logfile: 'debug.log', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (pagefindConfig.exclude_selectors) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| createIndexOptions.excludeSelectors = pagefindConfig.exclude_selectors; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { index } = await createIndex(createIndexOptions); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return index; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Indexes all the pages of the site using pagefind. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Performs a full rebuild of the search index. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @returns true if indexing succeeded and pagefind assets were written, false otherwise. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async indexSiteWithPagefind(): Promise<boolean> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const startTime = new Date(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.info('Creating Pagefind Search Index...'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { createIndex, close } = pagefind; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const pagefindConfig = this.siteConfig.pagefind || {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const createIndexOptions: Record<string, unknown> = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| keepIndexUrl: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| verbose: true, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logfile: 'debug.log', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (pagefindConfig.exclude_selectors) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| createIndexOptions.excludeSelectors = pagefindConfig.exclude_selectors; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Clean up existing in-memory index if it exists | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (this.pagefindIndex) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await this.pagefindIndex.deleteIndex(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.pagefindIndex = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { index } = await createIndex(createIndexOptions); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const index = await this.initializePagefindIndex(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { close } = pagefind; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (index) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Store index in memory for incremental updates in serve mode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.pagefindIndex = index; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Filter pages that should be indexed (searchable !== false) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const searchablePages = this.sitePages.pages.filter( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| page => page.pageConfig.searchable, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -939,10 +983,22 @@ export class SiteGenerationManager { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.info(`Pagefind indexed ${totalPageCount} pages in ${totalTime}s`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const pagefindOutputPath = path.join(this.outputPath, TEMPLATE_SITE_ASSET_FOLDER_NAME, 'pagefind'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Clear output directory before writing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await fs.emptyDir(pagefindOutputPath); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await fs.ensureDir(pagefindOutputPath); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await index.writeFiles({ outputPath: pagefindOutputPath }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.info(`Pagefind assets written to ${pagefindOutputPath}`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await close(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Only close the index in build/deploy mode; keep it in memory for serve mode | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Detect serve mode by checking if postBackgroundBuildFunc has a name (named function = serve) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const isServeMode = this.postBackgroundBuildFunc.name !== ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const shouldClose = !isServeMode; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+992
to
+995
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (shouldClose) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await close(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.pagefindIndex = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.error('Pagefind failed to create index'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -954,6 +1010,57 @@ export class SiteGenerationManager { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Updates the search index for changed pages only (incremental update). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Requires the index to be kept in memory from a prior indexSiteWithPagefind() call. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param pages Array of pages that were modified/added | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @returns true if update succeeded, false otherwise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async updatePagefindIndex(pages: Page[]): Promise<boolean> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!this.pagefindIndex) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.info('Pagefind index not in memory, auto-creating...'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.pagefindIndex = await this.initializePagefindIndex(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const pagefindOutputPath = path.join(this.outputPath, TEMPLATE_SITE_ASSET_FOLDER_NAME, 'pagefind'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const searchablePages = pages.filter(page => page.pageConfig.searchable); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await Promise.all(searchablePages.map(async (page) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious, is really necessary for we to await? or can we have it run asynchronously. By awaiting we would increase the time taken for each update to be shown. I don't think we would need the search result to be immediately ready too. It would require time for the users to click on the text box and then start searching, which is presumably sufficient for the pagefind to complete indexing.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea u right, but the updating process usually only takes a few second maximum to complete so I felt like the latency reduction is negligible. Users already wait for the rebuild to complete anyway.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep i think the idea is to avoid increasing the rebuild time haha it can be annoying (to me at least) wld there be any downsides to dropping the await? |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const content = await fs.readFile(page.pageConfig.resultPath, 'utf8'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const relativePath = path.relative(this.outputPath, page.pageConfig.resultPath); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return this.pagefindIndex.addHTMLFile({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sourcePath: relativePath, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| content, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (err) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const pageResultPath = page.pageConfig.resultPath; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.warn(`Skipping index update for ${pageResultPath}: file not built yet`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1031
to
+1043
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | |
| const content = await fs.readFile(page.pageConfig.resultPath, 'utf8'); | |
| const relativePath = path.relative(this.outputPath, page.pageConfig.resultPath); | |
| return this.pagefindIndex.addHTMLFile({ | |
| sourcePath: relativePath, | |
| content, | |
| }); | |
| } catch (err) { | |
| const pageResultPath = page.pageConfig.resultPath; | |
| logger.warn(`Skipping index update for ${pageResultPath}: file not built yet`); | |
| return null; | |
| } | |
| let content; | |
| try { | |
| content = await fs.readFile(page.pageConfig.resultPath, 'utf8'); | |
| } catch (err) { | |
| const pageResultPath = page.pageConfig.resultPath; | |
| logger.warn(`Skipping index update for ${pageResultPath}: file not built yet`); | |
| return null; | |
| } | |
| const relativePath = path.relative(this.outputPath, page.pageConfig.resultPath); | |
| return this.pagefindIndex.addHTMLFile({ | |
| sourcePath: relativePath, | |
| content, | |
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it necessary to clear the files after adding getting them here?
on line 1035 of this file:
return this.pagefindIndex.addHTMLFile({
sourcePath: relativePath,
content,
});There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep it is!
addHTMLFile() updates entries in the in-memory index but doesn't clear old on-disk files, so emptyDir() removes stale files before we updated files from getFiles() writes the complete current index.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ops sorry for not being clear there, i meant clearing the in-memory index added by addHTMLPages on each call to this function
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -86,6 +86,18 @@ export class Site { | |||||||||
| return this.generationManager.rebuildSourceFiles(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| async indexSiteWithPagefind(): Promise<boolean> { | ||||||||||
| return this.generationManager.indexSiteWithPagefind(); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| async updatePagefindIndex(filePaths: string | string[]): Promise<boolean> { | ||||||||||
| const paths = Array.isArray(filePaths) ? filePaths : [filePaths]; | ||||||||||
| const pages = this.generationManager.sitePages.pages.filter(page => | ||||||||||
| paths.some(p => page.pageConfig.sourcePath === p), | ||||||||||
|
||||||||||
| paths.some(p => page.pageConfig.sourcePath === p), | |
| paths.some(p => | |
| page.pageConfig.sourcePath === p || page.isDependency(p), | |
| ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updatePagefindIndexcan auto-create a Pagefind index even when search is disabled, because it doesn’t checksiteConfig.enableSearch. SinceserveUtilnow callssite.updatePagefindIndex(...)on every page/dependency change, this can generate Pagefind assets unexpectedly for sites withenableSearch: false. Add a guard (either here inserveUtil, or insideSite.updatePagefindIndex/SiteGenerationManager.updatePagefindIndex) to no-op whenenableSearchis false.