Get the latest tech news

The problem with new URL(), and how URL.parse() fixes that


As someone building a browser I need to parse a lot of URLs. Partially to validate them, but also to normalize them or get specific parts out of the URL. The URL API in browsers lets you do that, but it’s ergonomics aren’t ideal. The problem with new URL() The “new” in front of new […]

That's a lot more lines of code, has more visual noise and it means you have to change not_a_url from a const to a let to be able to overwrite it. Rather than being constructive and writing my own little function to abstract that try...catch or canParse away from my regular code base, I decided to do the right thing and complain on Twitter: Not much later Anne van Kesteren replied with a link to a GitHub issue discussing the addition of a "parse" function to URL that would not throw.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of problem

problem

Photo of fixes

fixes

Photo of new URL

new URL

Related news:

News photo

She was accused of faking an incriminating video of teenage cheerleaders. She was arrested, outcast and condemned. The problem? Nothing was fake after all

News photo

Elon Musk’s Neuralink faces problem with its tiny wires in brain

News photo

Google's new 'Ask Photos' AI solves a problem I have every day