ricokahler / next-plugin-preval

Pre-evaluate async functions during builds and import them like JSON
MIT License
255 stars 12 forks source link

Provide built-in fetch globally #12

Closed ricokahler closed 3 years ago

ricokahler commented 3 years ago

One inconsistency I've noticed is that Next.js will provide a global fetch function in Node environments. Right now next-plugin-preval does not do that and it should!

Vadorequest commented 3 years ago

FYI here is how I fixed this in Next Right Now, I had a similar issue in Jest tests:

/**
 * Importing next during test applies automated polyfills:
 *  - Polyfill the built-in "fetch" provided by Next.js
 *
 * @see https://github.com/vercel/next.js/discussions/13678#discussioncomment-22383 How to use built-in fetch in tests?
 * @see https://nextjs.org/blog/next-9-4#improved-built-in-fetch-support Next.js Blog - Improved Built-in Fetch Support
 * @see https://jestjs.io/docs/en/configuration#setupfilesafterenv-array About setupFilesAfterEnv usage
 */
require('next');

See https://github.com/UnlyEd/next-right-now/blob/30d83e961b6267ee7704d21b70004ee167f95ff5/jest.setup.js#L7-L15

ricokahler commented 3 years ago

oh very cool. seem like a quick one. i'll give this a try and release soon after if it works out

Vadorequest commented 3 years ago

Awesome, I wasn't sure it would be that easy! Glad it is ;)