Skip to content
Advertisement

Why new URL() method removes one part of my url passed as a base url

I created a snippet to show the issue below

I have the following values to create my URL

const baseUrl = 'https://s3.xxx.xxx.com/xyz.xxx.xxx.twilio';
const accessCode = 'accesscode_he-ar_IL.mp3';

When I run new URL() the result is https://s3.xxx.xxx.com/accesscode_he-ar_IL.mp3

Why the middle part is removed as xyz.xxx.xxx.twilio

I have no clue why the method removes part of my URL and this results in a fail for me as I need to get data from that path.

const baseUrl = 'https://s3.xxx.xxx.com/xyz.xxx.xxx.twilio';
const accessCode = 'accesscode_he-ar_IL.mp3';

const res = new URL(accessCode, baseUrl);

console.log(res);

Answer

You forgot to add an extra / at the end of your baseUrl : const baseUrl = 'https://s3.xxx.xxx.com/xyz.xxx.xxx.twilio/'

const baseUrl = 'https://s3.xxx.xxx.com/xyz.xxx.xxx.twilio/';
const accessCode = 'accesscode_he-ar_IL.mp3';

const res = new URL(accessCode, baseUrl);

console.log(res);

The URL() method replace the current page (which in your case was xyz.xxx.xxx.twilio) by the new page passed through the access code.

Advertisement