Skip to content

Always build URLs with new URL(), not template literals

Today I learned,

i used to build API URLs like ${baseURL}/endpoint with template literals and it worked fine for months. Then one day an endpoint started 404ing in prod with no deploy on either side. The env var ended with a slash, so the URL was actually //endpoint, and something upstream had been quietly normalizing it for us. Until it wasn’t. Most libraries we use every day, like axios, normalize URLs internally. Raw fetch doesn’t, it just sends what you give it. So if your code works with raw fetch, it might be working by accident, saved by some normalizer between you and the server that can stop doing its job at any time. Now I always use new URL(path, baseURL).toString() so there’s nothing left to normalize.

© 2026 All rights reserved.