Skip to content

Commit ca955f5

Browse files
committed
add: globalThis.fetch
1 parent 4a85281 commit ca955f5

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

prerender.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@ import { h, options, cloneElement } from 'preact';
22
import renderToString from 'preact-render-to-string';
33

44
let vnodeHook;
5+
let initialized = false;
56

67
const old = options.vnode;
78
options.vnode = vnode => {
89
if (old) old(vnode);
910
if (vnodeHook) vnodeHook(vnode);
1011
};
1112

13+
async function init() {
14+
const fs = (await eval('u=>import(u)')('fs')).promises;
15+
// eslint-disable-next-line no-undef
16+
globalThis.fetch = async url => {
17+
const text = () => fs.readFile('dist/' + String(url).replace(/^\//, ''), 'utf-8');
18+
return { text, json: () => text().then(JSON.parse) };
19+
};
20+
}
21+
1222
/**
1323
* @param {ReturnType<h>} vnode The root JSX element to render (eg: `<App />`)
1424
* @param {object} [options]
@@ -18,6 +28,11 @@ options.vnode = vnode => {
1828
export default async function prerender(vnode, options) {
1929
options = options || {};
2030

31+
if (!initialized) {
32+
initialized = true;
33+
await init();
34+
}
35+
2136
const maxDepth = options.maxDepth || 10;
2237
const props = options.props;
2338
let tries = 0;

0 commit comments

Comments
 (0)