fix: more speedup hopefully

This commit is contained in:
2026-02-22 08:48:55 +01:00
parent 71722282b2
commit c0eddb953f
3 changed files with 66 additions and 2 deletions

View File

@@ -679,7 +679,7 @@ export class BlogGenerationEngine {
reportUnitProgress('Assets copied');
}
const renderRoute = this.createSharedRouteRenderer(options, maxPostsPerPage);
const renderRoute = this.createSharedRouteRenderer(options, maxPostsPerPage, publishedPosts);
let pagesGenerated = 0;
@@ -1285,7 +1285,7 @@ export class BlogGenerationEngine {
const htmlDir = path.join(options.dataDir, 'html');
await fs.mkdir(htmlDir, { recursive: true });
const renderRoute = this.createSharedRouteRenderer(options, maxPostsPerPage);
const renderRoute = this.createSharedRouteRenderer(options, maxPostsPerPage, publishedPosts);
const onPageGenerated = (_message: string) => {
// no-op for applyValidation
};
@@ -1440,6 +1440,7 @@ export class BlogGenerationEngine {
private createSharedRouteRenderer(
options: BlogGenerationOptions,
maxPostsPerPage: number,
publishedPostsForLookup: PostData[] = [],
): (pathname: string) => Promise<string | null> {
const metadata: ProjectMetadata = {
name: options.projectName,
@@ -1464,6 +1465,16 @@ export class BlogGenerationEngine {
const postsByFilterPromiseCache = new Map<string, Promise<PostData[]>>();
const publishedSnapshotByIdPromiseCache = new Map<string, Promise<PostData | null>>();
type PostFilterInput = Parameters<typeof this.postEngine.getPostsFiltered>[0];
const publishedBySlugIndex = new Map<string, PostData[]>();
for (const post of publishedPostsForLookup) {
const existing = publishedBySlugIndex.get(post.slug);
if (existing) {
existing.push(post);
} else {
publishedBySlugIndex.set(post.slug, [post]);
}
}
const serializeFilter = (filter: PostFilterInput): string => {
const normalizeValue = (value: unknown): unknown => {
@@ -1506,6 +1517,24 @@ export class BlogGenerationEngine {
publishedSnapshotByIdPromiseCache.set(postId, promise);
return promise;
},
findPublishedBySlug: async (slug: string, dateFilter?: { year: number; month: number }) => {
const candidates = publishedBySlugIndex.get(slug);
if (!candidates || candidates.length === 0) {
return null;
}
if (!dateFilter) {
return candidates[0] ?? null;
}
const match = candidates.find((candidate) => {
const createdAt = candidate.createdAt;
return createdAt.getFullYear() === dateFilter.year
&& createdAt.getMonth() === dateFilter.month;
});
return match ?? null;
},
getPost: (postId: string) => this.postEngine.getPost(postId),
hasPublishedVersion: (postId: string) => this.postEngine.hasPublishedVersion(postId),
setProjectContext: (projectId: string, dataDir?: string) => {

View File

@@ -36,6 +36,7 @@ interface PostEngineContract {
getPost: (id: string) => Promise<PostData | null>;
hasPublishedVersion: (id: string) => Promise<boolean>;
getPublishedVersion: (id: string) => Promise<PostData | null>;
findPublishedBySlug?: (slug: string, dateFilter?: { year: number; month: number }) => Promise<PostData | null>;
setProjectContext: (projectId: string, dataDir?: string) => void;
}
@@ -522,6 +523,13 @@ export class PreviewServer {
private async findPublishedPostBySlug(slug: string, dateFilter?: { year: number; month: number }): Promise<PostData | null> {
if (!slug) return null;
if (this.postEngine.findPublishedBySlug) {
const directMatch = await this.postEngine.findPublishedBySlug(slug, dateFilter);
if (directMatch) {
return directMatch;
}
}
const filter: PostFilter = {
...(dateFilter ? { year: dateFilter.year, month: dateFilter.month } : {}),
};