From 36148a0bcf3a73d1fe3f0c5f33337b679f700053 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Tue, 24 Jun 2025 10:25:29 +0530 Subject: [PATCH] perf: render pages in contentLoader asynchronously --- src/node/contentLoader.ts | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/node/contentLoader.ts b/src/node/contentLoader.ts index 43009d3c..7b32b619 100644 --- a/src/node/contentLoader.ts +++ b/src/node/contentLoader.ts @@ -1,6 +1,7 @@ import fs from 'fs-extra' import matter from 'gray-matter' import path from 'node:path' +import pMap from 'p-map' import { normalizePath } from 'vite' import type { SiteConfig } from './config' import { createMarkdownRenderer } from './markdown/markdown' @@ -103,18 +104,15 @@ export function createContentLoader( config.logger ) - const raw: ContentData[] = [] + const raw = await pMap( + files, + async (file) => { + if (!file.endsWith('.md')) return null - for (const file of files) { - if (!file.endsWith('.md')) continue + const timestamp = fs.statSync(file).mtimeMs + const cached = cache.get(file) - const timestamp = fs.statSync(file).mtimeMs - const cached = cache.get(file) - - if (cached && timestamp === cached.timestamp) { - raw.push(cached.data) - } else { - // + if (cached && timestamp === cached.timestamp) return cached.data const src = fs.readFileSync(file, 'utf-8') @@ -146,11 +144,13 @@ export function createContentLoader( } cache.set(file, { data, timestamp }) - raw.push(data) - } - } + return data + }, + { concurrency: config.buildConcurrency } + ) - return options.transform?.(raw) ?? (raw as T) + const filtered = raw.filter((i) => i !== null) + return options.transform?.(filtered) ?? (filtered as T) } } }