Skip to content

Commit

Permalink
feat: handle absolute URIs in PUBLIC_PATH (openedx#568)
Browse files Browse the repository at this point in the history
Webpack allows `publicPath` to be an absolute URI (https://webpack.js.org/configuration/output/#outputpublicpath), in order to support deployments to a CDN.  However, the browser history module expects a relative path for `basename` but uses the same environment variable (`PUBLIC_PATH`) to configure it.  Since the path is always expected to be be suffixed to the absolute URI, we avoid introducing another configuration variable (thus maintaining full backwards compatibility) by extracting it at initialization time.
  • Loading branch information
johanseto authored and bra-i-am committed Apr 30, 2024
1 parent 3fc470d commit 8d84715
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 24 deletions.
12 changes: 4 additions & 8 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,9 @@ export function convertKeyNames(object, nameMap) {
* @returns {Object}
*/
export function parseURL(url) {
if (typeof document !== 'undefined') {
const parser = document.createElement('a');
parser.href = url;
return parser;
}

return {};
const parser = document.createElement('a');
parser.href = url;
return parser;
}

/**
Expand All @@ -155,7 +151,7 @@ export function parseURL(url) {
* @returns {string}
*/
export function getPath(url) {
return typeof document !== 'undefined' ? parseURL(url)?.pathname : '';
return parseURL(url).pathname;
}

/**
Expand Down
16 changes: 0 additions & 16 deletions src/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,6 @@ describe('getQueryParameters', () => {
describe('ParseURL', () => {
const testURL = 'http://example.com:3000/pathname/?search=test#hash';
const parsedURL = parseURL(testURL);
let originalDocument;

beforeEach(() => {
originalDocument = global.document;
});

afterEach(() => {
global.document = originalDocument;
});

it('String URL is correctly parsed', () => {
expect(parsedURL.toString()).toEqual(testURL);
expect(parsedURL.href).toEqual(testURL);
Expand Down Expand Up @@ -162,12 +152,6 @@ describe('ParseURL', () => {
it('should return host from URL', () => {
expect(parsedURL.host).toEqual('example.com:3000');
});

it('should return empty object in case of document being undefined', () => {
delete global.document;

expect(parseURL(testURL)).toEqual({});
});
});

describe('getPath', () => {
Expand Down

0 comments on commit 8d84715

Please sign in to comment.