Next.js Discord

Discord Forum

Why does Next.js use its own types where existing Web APIs exist (URL, URLSearchParams)?

Unanswered
Giant panda posted this in #help-forum
Open in Discord
Giant pandaOP
I'm coming from Svelte/SvelteKit, where whenever something is part of web standards they try to reuse it, like URL, Request, Response, etc, along with their subtypes.

I was wondering if anybody could shed some light into the design decisions behind not reusing those types even in the app router, which as I understand is one of the newest parts of Next.js?

For instance, the searchParams prop passed to a page is not of type URLSearchParams[1]. Another example, which confuses me even further, is that the pages router useRouter hook accepts a "URL object" (which is not of type URL)[2], yet the newer app router useRouter only accepts a string[3].

It just seems like the barrier for learning standard Web APIs and Next.js's own could be much less jarring if some of these types were the same. For instance, a useURL hook in addition to what is already in place. Otherwise there would have to be a hook for every property of the URL I could possibly be interested in (speaking of which, how do I access the hostname if there's no useHostname?).

[1]: https://nextjs.org/docs/app/api-reference/file-conventions/page#searchparams-optional
[2]: https://nextjs.org/docs/pages/api-reference/functions/use-router#with-url-object
[3]: https://nextjs.org/docs/app/api-reference/functions/use-router#userouter

0 Replies