Skip to content

Prerendering async data #950

Answered by rschristian
chopfitzroy asked this question in Q&A
Jan 23, 2023 · 3 comments · 2 replies
Discussion options

You must be logged in to vote

The docs site works by patching fetch() to a fs.readFile() implementation, which you can find here:

const fs = (await eval('u=>import(u)')('fs')).promises;
globalThis.fetch = async url => {
const text = () => fs.readFile('dist/' + String(url).replace(/^\//, ''), 'utf-8');
return { text, json: () => text().then(JSON.parse) };
};

This has become built-in as of #934 (as it's pretty handy) though, so you won't need to do that manually if that's the behavior you want. Fetching a local file (using a relative specifier, like fetch('/foo.js')) will automatically use fs when prerendering.

However, for exter…

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by chopfitzroy
Comment options

You must be logged in to vote
2 replies
@rschristian
Comment options

@chopfitzroy
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants