SSG Build Cache Incremental Invalidation
Speed up static site builds by hashing content inputs per page, reusing unchanged build cache entries, and invalidating only affected pages.
Code
// SSG build cache: invalidate prerender output when page hashes change
import { createHash } from 'node:crypto';
import { readFile, writeFile, mkdir, readdir } from 'node:fs/promises';
async function buildIncremental(pagesDir, cacheDir) {
await mkdir(cacheDir, { recursive: true });
const pages = (await readdir(pagesDir)).filter((f) => f.endsWith('.md'));
let cacheMeta = {};
try {
cacheMeta = JSON.parse(await readFile(cacheDir + '/manifest.json', 'utf8'));
} catch {
cacheMeta = {};
}
const manifest = {};
let rebuilt = 0;
for (const file of pages) {
const raw = await readFile(pagesDir + '/' + file, 'utf8');
const hash = createHash('sha256').update(raw).digest('hex').slice(0, 16);
if (cacheMeta[file] === hash) {
manifest[file] = hash; // 静态内容未变,复用缓存产物
continue;
}
const html = '<article>' + raw.replace(/^# (.*)$/m, '<h1>$1</h1>') + '</article>';
await writeFile(cacheDir + '/' + file.replace(/\.md$/, '.html'), html);
manifest[file] = hash;
rebuilt += 1;
}
await writeFile(cacheDir + '/manifest.json', JSON.stringify(manifest, null, 2));
return { total: pages.length, rebuilt, cached: pages.length - rebuilt };
}
const stats = await buildIncremental('content/pages', 'dist/cache');
console.log('build finished:', stats);What the code does
SSG Build Cache Incremental Invalidation is built around the functions pages and buildIncremental.
The one repetition in this snippet is the loop for (const file of pages). Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed. node:crypto, node:fs/promises supplies createHash, readFile, writeFile, mkdir, readdir — built-in surface, no package involved.
The result comes back as an object carrying length, rebuilt.
Points of care
- The only import,
node:crypto, is part of the Node.js standard library. - This is browser-side code —
cryptowill not resolve in Node.
Applying it
The main signature reads const pages = (await readdir(pagesDir)
The demo output is labeled (build finished:), so every printed line maps back to its originating operation.
The call returns length, rebuilt — destructure what you need at the call site.
Statements that matter
await writeFile(cacheDir + '/manifest.json', JSON.stringify(manifest, null, 2));
const pages = (await readdir(pagesDir)).filter((f) => f.endsWith('.md'));
cacheMeta = JSON.parse(await readFile(cacheDir + '/manifest.json', 'utf8'));
Interactive Tools for This Topic
FAQ
- Does `pages` modify its input?
- The input survives unchanged; what you get back is a newly constructed value.
- Are there third-party dependencies?
- No. The imports resolve to Node.js core modules, which the runtime provides.
Report a problem with this exampleOpens a pre-filled issue on GitHub.