refactor(loader): read content files with fs/promises

createContentLoader already maps files with bounded concurrency, but
the sync stat/read calls serialized the actual I/O.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
pull/5342/head
Divyansh Singh 2 months ago
parent 343aa13b68
commit cef5a7fc8e

@ -1,5 +1,5 @@
import matter from 'gray-matter' import matter from 'gray-matter'
import fs from 'node:fs' import { readFile, stat } from 'node:fs/promises'
import path from 'node:path' import path from 'node:path'
import pMap from 'p-map' import pMap from 'p-map'
import { normalizePath } from 'vite' import { normalizePath } from 'vite'
@ -116,12 +116,12 @@ export function createContentLoader<T = ContentData[]>(
async (file) => { async (file) => {
if (!file.endsWith('.md')) return null if (!file.endsWith('.md')) return null
const timestamp = fs.statSync(file).mtimeMs const timestamp = (await stat(file)).mtimeMs
const cached = cache.get(file) const cached = cache.get(file)
if (cached && timestamp === cached.timestamp) return cached.data if (cached && timestamp === cached.timestamp) return cached.data
const src = fs.readFileSync(file, 'utf8') const src = await readFile(file, 'utf8')
const renderExcerpt = options.excerpt const renderExcerpt = options.excerpt
const { data: frontmatter, excerpt } = matter( const { data: frontmatter, excerpt } = matter(

Loading…
Cancel
Save