-
-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat proposal enable fetching external resources using native fetch API in Node.js version 18 later #935
Feat proposal enable fetching external resources using native fetch API in Node.js version 18 later #935
Changes from 7 commits
3473559
c9d9d56
aa0818d
8acd946
74f90a9
54e5d2a
27def4e
f1a51dd
38b7dd5
63954dd
7c1bb6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'wmr': minor | ||
--- | ||
|
||
Add fetching external resources using native fetch API when Node.js version 18 later |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf8" /> | ||
<title>default title</title> | ||
</head> | ||
<body> | ||
<script src="./index.js" type="module"></script> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export async function prerender() { | ||
const html = await fetch('https://preactjs.com').then(res => res.text()); | ||
return { html, links: ['/'] }; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export async function prerender() { | ||
const md = await fetch('content.md').then(res => res.text()); | ||
const md = await fetch('/content.md').then(res => res.text()); | ||
rschristian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return { html: md, links: ['/'] }; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -889,6 +889,31 @@ describe('production', () => { | |
const index = await fs.readFile(indexHtml, 'utf8'); | ||
expect(index).toMatch(/# hello world/); | ||
}); | ||
|
||
it('should not support fetching resources from external link prerender', async () => { | ||
await loadFixture('prerender-external-resource-fetch', env); | ||
instance = await runWmr(env.tmp.path, 'build', '--prerender'); | ||
const code = await instance.done; | ||
|
||
const nodeVersion = process.version; | ||
const isNodeVersionOver18 = Number(nodeVersion.split('.')[0].slice(1)) >= 18; | ||
if (isNodeVersionOver18) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to run tests on multiple versions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, and two versions, 12 and 14, are running now. This felt like a no problem in the future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm also considering breaking down this test. Instead of branching using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I like this idea. There's slightly different behavior between the two that (ideally) we'd test. We can definitely alter the CI config to cover newer versions of Node. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I completely forgot about this. I think to need to modify the test here as well. I think the comments here will be helpful for that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, no correction of above comment was necessary. |
||
// when workflow of wmr runtime uses Node.js 18 | ||
// Until then check locally. | ||
expect(code).toBe(0); | ||
const indexHtml = path.join(env.tmp.path, 'dist', 'index.html'); | ||
const index = await fs.readFile(indexHtml, 'utf8'); | ||
expect(index).toMatch(/Preact/); | ||
} else { | ||
// Throw error when Node.js version under 17 | ||
await withLog(instance.output, async () => { | ||
expect(code).toBe(1); | ||
expect(instance.output.join('\n')).toMatch( | ||
/fetch is not implemented in Node.js, please upgrade to Node.js 18 or above/ | ||
); | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
describe('Code Splitting', () => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comments explaining the workflow here are excellent! Really good stuff.