fix: faster blog generation by caching
This commit is contained in:
@@ -1459,9 +1459,80 @@ export class BlogGenerationEngine {
|
|||||||
projectDescription: options.projectDescription,
|
projectDescription: options.projectDescription,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const routeHtmlCache = new Map<string, Promise<string | null>>();
|
||||||
|
const mediaItemsPromiseCache = new Map<string, Promise<Awaited<ReturnType<typeof this.mediaEngine.getAllMedia>>>>();
|
||||||
|
const postsByFilterPromiseCache = new Map<string, Promise<PostData[]>>();
|
||||||
|
const publishedSnapshotByIdPromiseCache = new Map<string, Promise<PostData | null>>();
|
||||||
|
type PostFilterInput = Parameters<typeof this.postEngine.getPostsFiltered>[0];
|
||||||
|
|
||||||
|
const serializeFilter = (filter: PostFilterInput): string => {
|
||||||
|
const normalizeValue = (value: unknown): unknown => {
|
||||||
|
if (Array.isArray(value)) {
|
||||||
|
return value.map((entry) => normalizeValue(entry));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value && typeof value === 'object') {
|
||||||
|
const sortedEntries = Object.entries(value as Record<string, unknown>)
|
||||||
|
.sort(([left], [right]) => left.localeCompare(right))
|
||||||
|
.map(([key, nestedValue]) => [key, normalizeValue(nestedValue)] as const);
|
||||||
|
return Object.fromEntries(sortedEntries);
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
};
|
||||||
|
|
||||||
|
return JSON.stringify(normalizeValue(filter));
|
||||||
|
};
|
||||||
|
|
||||||
|
const cachedPostEngine = {
|
||||||
|
getPostsFiltered: (filter: PostFilterInput) => {
|
||||||
|
const cacheKey = serializeFilter(filter);
|
||||||
|
const cached = postsByFilterPromiseCache.get(cacheKey);
|
||||||
|
if (cached) {
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
|
||||||
|
const promise = this.postEngine.getPostsFiltered(filter);
|
||||||
|
postsByFilterPromiseCache.set(cacheKey, promise);
|
||||||
|
return promise;
|
||||||
|
},
|
||||||
|
getPublishedVersion: (postId: string) => {
|
||||||
|
const cached = publishedSnapshotByIdPromiseCache.get(postId);
|
||||||
|
if (cached) {
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
|
||||||
|
const promise = this.postEngine.getPublishedVersion(postId);
|
||||||
|
publishedSnapshotByIdPromiseCache.set(postId, promise);
|
||||||
|
return promise;
|
||||||
|
},
|
||||||
|
getPost: (postId: string) => this.postEngine.getPost(postId),
|
||||||
|
hasPublishedVersion: (postId: string) => this.postEngine.hasPublishedVersion(postId),
|
||||||
|
setProjectContext: (projectId: string, dataDir?: string) => {
|
||||||
|
this.postEngine.setProjectContext(projectId, dataDir);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const cachedMediaEngine = {
|
||||||
|
getAllMedia: () => {
|
||||||
|
const cacheKey = `${options.projectId}:${options.dataDir ?? ''}`;
|
||||||
|
const cached = mediaItemsPromiseCache.get(cacheKey);
|
||||||
|
if (cached) {
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
|
||||||
|
const promise = this.mediaEngine.getAllMedia();
|
||||||
|
mediaItemsPromiseCache.set(cacheKey, promise);
|
||||||
|
return promise;
|
||||||
|
},
|
||||||
|
setProjectContext: (projectId: string, dataDir?: string, internalDir?: string) => {
|
||||||
|
this.mediaEngine.setProjectContext?.(projectId, dataDir, internalDir);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
const previewServer = new PreviewServer({
|
const previewServer = new PreviewServer({
|
||||||
postEngine: this.postEngine,
|
postEngine: cachedPostEngine,
|
||||||
mediaEngine: this.mediaEngine,
|
mediaEngine: cachedMediaEngine,
|
||||||
postMediaEngine: this.postMediaEngine,
|
postMediaEngine: this.postMediaEngine,
|
||||||
settingsEngine: {
|
settingsEngine: {
|
||||||
setProjectContext: () => {},
|
setProjectContext: () => {},
|
||||||
@@ -1475,12 +1546,21 @@ export class BlogGenerationEngine {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return async (pathname: string): Promise<string | null> => {
|
return async (pathname: string): Promise<string | null> => {
|
||||||
return previewServer.renderRouteForContext(pathname, {
|
const normalizedPathname = decodeURIComponent(pathname.replace(/\/+$/, '') || '/');
|
||||||
|
const cached = routeHtmlCache.get(normalizedPathname);
|
||||||
|
if (cached) {
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
|
||||||
|
const promise = previewServer.renderRouteForContext(normalizedPathname, {
|
||||||
projectContext,
|
projectContext,
|
||||||
metadata,
|
metadata,
|
||||||
menu,
|
menu,
|
||||||
maxPostsPerPage,
|
maxPostsPerPage,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
routeHtmlCache.set(normalizedPathname, promise);
|
||||||
|
return promise;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -586,6 +586,33 @@ describe('BlogGenerationEngine', () => {
|
|||||||
expect(result.pagesGenerated).toBe(7);
|
expect(result.pagesGenerated).toBe(7);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('reuses shared render snapshot and media lookups across full-site routes', async () => {
|
||||||
|
const posts = [
|
||||||
|
makePost({ id: '1', slug: 'post-1', categories: ['news'], tags: ['t1'], createdAt: new Date('2025-01-15T10:00:00Z') }),
|
||||||
|
makePost({ id: '2', slug: 'post-2', categories: ['news'], tags: ['t2'], createdAt: new Date('2025-01-14T10:00:00Z') }),
|
||||||
|
makePost({ id: '3', slug: 'page-1', categories: ['page'], tags: [], createdAt: new Date('2025-01-13T10:00:00Z') }),
|
||||||
|
];
|
||||||
|
|
||||||
|
setupPosts(posts);
|
||||||
|
|
||||||
|
const { BlogGenerationEngine } = await import('../../src/main/engine/BlogGenerationEngine');
|
||||||
|
const engine = new BlogGenerationEngine();
|
||||||
|
await engine.generate({
|
||||||
|
projectId: 'test',
|
||||||
|
projectName: 'Test Blog',
|
||||||
|
dataDir: tempDir,
|
||||||
|
baseUrl: 'https://example.com',
|
||||||
|
maxPostsPerPage: 1,
|
||||||
|
}, vi.fn());
|
||||||
|
|
||||||
|
const filteredCallCount = mockPostEngine.getPostsFiltered.mock.calls.length;
|
||||||
|
const publishedVersionCallCount = mockPostEngine.getPublishedVersion.mock.calls.length;
|
||||||
|
|
||||||
|
expect(filteredCallCount).toBeLessThanOrEqual(20);
|
||||||
|
expect(publishedVersionCallCount).toBeLessThanOrEqual(10);
|
||||||
|
expect(mockMediaEngine.getAllMedia).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
it('validates sitemap against html folder without rendering missing pages', async () => {
|
it('validates sitemap against html folder without rendering missing pages', async () => {
|
||||||
const posts = [
|
const posts = [
|
||||||
makePost({
|
makePost({
|
||||||
|
|||||||
Reference in New Issue
Block a user