-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprewarm-cache.mjs
More file actions
40 lines (35 loc) · 1.34 KB
/
Copy pathprewarm-cache.mjs
File metadata and controls
40 lines (35 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { createElement } from 'react';
import { renderToPipeableStream } from 'react-server-dom-webpack/server.node';
import { PassThrough } from 'node:stream';
import { mkdirSync } from 'node:fs';
import { cachedComponents } from './src/cache-registry.js';
import { writeCacheEntry } from './src/flight-cache.js';
function collectBuffer(stream) {
return new Promise((resolve, reject) => {
const chunks = [];
const pt = new PassThrough();
stream.pipe(pt);
pt.on('data', c => chunks.push(Buffer.from(c)));
pt.on('end', () => resolve(Buffer.concat(chunks)));
pt.on('error', reject);
});
}
async function prewarm() {
mkdirSync('./.rsc_cache', { recursive: true });
const webpackMap = {};
for (const [name, { createElement: elFactory }] of Object.entries(cachedComponents)) {
const element = elFactory();
const wrapped = createElement('div', { 'data-cache-key': name }, element);
const stream = renderToPipeableStream(wrapped, webpackMap, {});
const buf = await collectBuffer(stream);
await writeCacheEntry(name, {}, buf);
console.log(` cached ${name}: ${buf.length} bytes`);
}
console.log(`RSC cache prewarm complete: ${Object.keys(cachedComponents).length} entries`);
}
prewarm()
.then(() => process.exit(0))
.catch((err) => {
console.error('Cache prewarm failed:', err);
process.exit(1);
});