Skip to content

Commit

Permalink
MWPW-157210 STE Promo card not appearing (#2837)
Browse files Browse the repository at this point in the history
* MWPW-157210 STE Promo card not appearing

* MWPW-157210 STE Promo card not appearing

* MWPW-157210 STE Promo card not appearing

* Trigger Build

---------

Co-authored-by: Bozo Jovicic <[email protected]>
  • Loading branch information
bozojovicic and Bozo Jovicic authored Sep 11, 2024
1 parent a234e2b commit 7921bf8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion libs/blocks/merch-card-collection/merch-card-collection.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { overrideUrlOrigin } from '../../utils/helpers.js';
import {
createTag, decorateLinks, getConfig, loadBlock, loadStyle, localizeLink,
} from '../../utils/utils.js';
Expand Down Expand Up @@ -57,7 +58,7 @@ async function getCardsRoot(config, html) {
}

const fetchOverrideCard = (action, config) => new Promise((resolve, reject) => {
fetch(`${localizeLink(action?.target, config)}.plain.html`).then((res) => {
fetch(`${localizeLink(overrideUrlOrigin(action?.target))}.plain.html`).then((res) => {
if (res.ok) {
res.text().then((cardContent) => {
const response = { path: action.target, cardContent: /^<div>(.*)<\/div>$/.exec(cardContent.replaceAll('\n', ''))[1] };
Expand Down
18 changes: 18 additions & 0 deletions libs/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,21 @@ export function updateLinkWithLangRoot(link) {
return link;
}
}

/**
* Replaces the origin of the provided link with location.origin.
*
* @param link
* @returns {string|*}
*/
export function overrideUrlOrigin(link) {
try {
const url = new URL(link);
if (url.hostname !== window.location.hostname) {
return link.replace(url.origin, window.location.origin);
}
} catch (e) {
// ignore
}
return link;
}
14 changes: 14 additions & 0 deletions test/utils/helpers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { expect } from '@esm-bundle/chai';
import { overrideUrlOrigin } from '../../libs/utils/helpers.js';

describe('overrideUrlOrigin', () => {
it('Change origin to http://localhost:2000', async () => {
const link = overrideUrlOrigin('http://www.qa.adobe.com/some/page.html?a=b#hash');
expect(link).to.equal('http://localhost:2000/some/page.html?a=b#hash');
});

it('Ignore relative URLs', async () => {
const link = overrideUrlOrigin('/some/page.html?a=b#hash');
expect(link).to.equal('/some/page.html?a=b#hash');
});
});

0 comments on commit 7921bf8

Please sign in to comment.