mo / abortcontroller-polyfill

Polyfill for the AbortController DOM API and abortable fetch (stub that calls catch, doesn't actually abort request).
MIT License
328 stars 26 forks source link

Incompatible with NextJS middleware #64

Closed David-Rickard closed 2 years ago

David-Rickard commented 2 years ago

Repro steps: 1) Create a NextJS app with pnpm create next-app --ts 2) Create a middleware.ts with import 'abortcontroller-polyfill/dist/polyfill-patch-fetch' 3) Run pnpm i and pnpm dev 4) Go to http://localhost:3000/

Expected result: App runs

Actual result: TypeError: Cannot redefine property: AbortController

It detects the environment as needing a polyfill via this line. The fetch property is missing, but it is unable to redefine the AbortController` property on the global object.

https://github.com/David-Rickard/abortcontroller-polyfill-nextjs-repro

mo commented 2 years ago

Thanks for reporting this. I'm on vacation right now, I will have a look next week or so.

mo commented 2 years ago

I looked into this a little bit today and I think you should use another package. The "abortcontroller-polyfill" package was intended to add an AbortController to legacy browsers and it hasn't been tested in an nextjs environment. If you really want to add such support and do all the required testing etc I can merge it, given that it doesn't break any of the legacy browser support (in particular Safari 11.1.2 etc where there is a buggy AbortController available in the browser).

I recommend that you google a bit for another solution, maybe try this: https://github.com/vercel/next.js/discussions/32736 ... or talk to other people in the nextjs ecosystem to figure out how they usually solve this.

David-Rickard commented 2 years ago

I'm not using it directly; it's coming in via a dependency in another package I'm using, one that tries to work in all environments. I've talked with them about it and the current plan is to remove the polyfill from it.

mo commented 2 years ago

Just out of curiousity, which packages are you talking about here?

David-Rickard commented 2 years ago

It's a shared package internal to DoorDash.