Open supernes opened 5 days ago
@supernes Taking a look if this is something feasible to add!
Thanks for looking into this. I would classify this is a mostly Developer Experience issue, but it can lead to a deceptive mismatch between development and production behavior.
The reference documentation shows the API as widely available, and it works in local development on an up-to-date software stack, but will actually fail with a "client error" when deployed for around 30% of global users. Even on Node v20 LTS it can fail unpredictably, as in this case - it builds on Vercel, but not on CodeSandbox (who are several minor versions behind).
Eventually I just rewrote the code around the call, but it seems like an easy improvement to polyfill it.
Link to the code that reproduces this issue
https://codesandbox.io/p/devbox/determined-knuth-87352t?file=%2Fapp%2Fpage.tsx%3A3%2C16
To Reproduce
Current vs. Expected behavior
Expected result is no error Actual result is a runtime/buildtime error
./app/page.tsx:3:28 Type error: Property 'parse' does not exist on type '{ new (url: string | URL, base?: string | URL): URL; prototype: URL; canParse(url: string | URL, base?: string): boolean; createObjectURL(obj: Blob | MediaSource): string; revokeObjectURL(url: string): void; }'.
1 | /* Add your relevant code here for the issue to reproduce / 2 | export default function Home() {
Provide environment information
Which area(s) are affected? (Select all that apply)
Runtime
Which stage(s) are affected? (Select all that apply)
next dev (local), next build (local), next start (local), Vercel (Deployed)
Additional context
URL.parse()
is a static method on theURL
class supported on all major platforms, but the Next.js polyfill clobbers it on both server and client - only thecanParse()
method is included, even though core-js has a polyfill forparse()
as well.https://github.com/vercel/next.js/blob/c50109b4468bb274c29dfc0eb5aaa906736dc4d9/packages/next-polyfill-nomodule/src/index.js#L52
Additionally, Safari does not support this method until v18 (released two months ago). Found this out when I started to get runtime errors on my phone on a Vercel production deployment.
Node v20 LTS - https://nodejs.org/docs/latest-v20.x/api/url.html#urlparseinput-base MDN - https://developer.mozilla.org/en-US/docs/Web/API/URL/parse_static Can I Use? - https://caniuse.com/mdn-api_url_parse_static
It's been added in Node v22 and backported to LTS. Here's an example from my dev environment that shows it working: