Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MWPW-150668] Get utils ready for Helix 5 #2376

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions libs/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export const MILO_EVENTS = { DEFERRED: 'milo:deferred' };

const LANGSTORE = 'langstore';
const PAGE_URL = new URL(window.location.href);
const SLD = PAGE_URL.hostname.includes('.aem.') ? 'aem' : 'hlx';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We wanted to export the const to re-use it in other files, right?
Since we don't use it right now, I'd add that later on though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember we discussed it, but I think the conclusion for all other files was: you either port your whole project or you don't. So it's one or the other, we shouldn't have a need to detect it outside utils, where we can't be sure whether the consumer has migrated or not.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And then in the core blocks where needed, we'd also just have the check for new URL(window.location.href).hostname.includes('.aem.') ? 'aem' : 'hlx'; ?

So we can just check for the whole pattern after consumers migrated and remove the code again, rather than caring about other projects having imported that variable?


function getEnv(conf) {
const { host } = window.location;
Expand All @@ -146,8 +147,8 @@ function getEnv(conf) {
if (query) return { ...ENVS[query], consumer: conf[query] };
if (host.includes('localhost')) return { ...ENVS.local, consumer: conf.local };
/* c8 ignore start */
if (host.includes('hlx.page')
|| host.includes('hlx.live')
if (host.includes(`${SLD}.page`)
|| host.includes(`${SLD}.live`)
|| host.includes('stage.adobe')
|| host.includes('corp.adobe')) {
return { ...ENVS.stage, consumer: conf.stage };
Expand Down Expand Up @@ -230,7 +231,7 @@ export const [setConfig, updateConfig, getConfig] = (() => {
console.log('Invalid or missing locale:', e);
}
config.locale.contentRoot = `${origin}${config.locale.prefix}${config.contentRoot ?? ''}`;
config.useDotHtml = !PAGE_URL.origin.includes('.hlx.')
config.useDotHtml = !PAGE_URL.origin.includes(`.${SLD}.`)
&& (conf.useDotHtml ?? PAGE_URL.pathname.endsWith('.html'));
config.entitlements = handleEntitlements;
config.consumerEntitlements = conf.entitlements || [];
Expand Down Expand Up @@ -489,7 +490,7 @@ export function decorateSVG(a) {
? new URL(`${window.location.origin}${a.href}`)
: new URL(a.href);

const src = textUrl.hostname.includes('.hlx.') ? textUrl.pathname : textUrl;
const src = textUrl.hostname.includes(`.${SLD}.`) ? textUrl.pathname : textUrl;

const img = createTag('img', { loading: 'lazy', src });
if (altText) img.alt = altText;
Expand All @@ -515,7 +516,7 @@ export function decorateImageLinks(el) {
const [source, alt, icon] = img.alt.split('|');
try {
const url = new URL(source.trim());
const href = url.hostname.includes('.hlx.') ? `${url.pathname}${url.hash}` : url.href;
const href = url.hostname.includes(`.${SLD}.`) ? `${url.pathname}${url.hash}` : url.href;
if (alt?.trim().length) img.alt = alt.trim();
const pic = img.closest('picture');
const picParent = pic.parentElement;
Expand Down Expand Up @@ -1026,7 +1027,7 @@ function initSidekick() {

function decorateMeta() {
const { origin } = window.location;
const contents = document.head.querySelectorAll('[content*=".hlx."]');
const contents = document.head.querySelectorAll(`[content*=".${SLD}."]`);
contents.forEach((meta) => {
if (meta.getAttribute('property') === 'hlx:proxyUrl') return;
try {
Expand Down
Loading