Skip to content

Commit

Permalink
MWPW-126165 - Clean up meta URLs (adobecom#485)
Browse files Browse the repository at this point in the history
* Make .hlx. content in meta tags same-domain
  • Loading branch information
auniverseaway authored Feb 17, 2023
1 parent d99875c commit f5af504
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
15 changes: 15 additions & 0 deletions libs/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,20 @@ function initSidekick() {
}
}

function decorateMeta() {
const { origin } = window.location;
const contents = document.head.querySelectorAll('[content*=".hlx."]');
contents.forEach((meta) => {
try {
const url = new URL(meta.content);
meta.setAttribute('content', `${origin}${url.pathname}${url.search}${url.hash}`);
} catch (e) {
/* c8 ignore next 2 */
window.lana.log('Cannot make URL from metadata');
}
});
}

export async function loadArea(area = document) {
const config = getConfig();
const isDoc = area === document;
Expand All @@ -633,6 +647,7 @@ export async function loadArea(area = document) {
await decoratePlaceholders(area, config);

if (isDoc) {
decorateMeta();
decorateHeader();

import('./samplerum.js').then(({ addRumListeners }) => {
Expand Down
1 change: 1 addition & 0 deletions test/utils/mocks/head.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<meta name="interlinks" content="on">
<link rel="icon" href="data:,">
<meta name="hlx-url" content="https://www.hlx.live/otis">
11 changes: 8 additions & 3 deletions test/utils/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,22 @@ describe('Utils', () => {
expect(document.querySelector('header')).to.not.exist;
expect(document.querySelector('footer')).to.not.exist;
});

it('Decorates placeholder', () => {
const paragraphs = [...document.querySelectorAll('p')];
const lastPara = paragraphs.pop();
expect(lastPara.textContent).to.equal('nothing to see here');
});


it('Decorates meta helix url', () => {
const meta = document.head.querySelector('[name="hlx-url"]');
expect(meta.content).to.equal('http://localhost:2000/otis');
});

it('getLocale default return', () => {
expect(utils.getLocale().ietf).to.equal('en-US');
});

it('getLocale for different paths', () => {
const locales = {
'': { ietf: 'en-US', tk: 'hah7vzn.css' },
Expand Down

0 comments on commit f5af504

Please sign in to comment.