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

Multi source support for commerce configs and placeholders #286

Draft
wants to merge 23 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5af1925
refactor: update fetchPlaceholders import path and enhance multi-stor…
fnhipster Jan 28, 2025
6d845e6
fix: disable eslint rule for cyclic imports in configs.js
fnhipster Jan 28, 2025
a60bc9c
refactor: update fetchPlaceholders function for improved multi-store …
fnhipster Jan 28, 2025
1433d1a
index all pages in the two-tiers structure
fnhipster Jan 28, 2025
4174060
refactor: update helix-query.yaml to exclude drafts, enrichment, and …
fnhipster Jan 28, 2025
3ef2451
refactor: remove tiers configuration from helix-query.yaml
fnhipster Jan 30, 2025
5517558
refactor: simplify buildConfigURL and remove unused root path handlin…
fnhipster Jan 30, 2025
09f2ed1
fix: ensure checkIsAuthenticated returns a boolean value
fnhipster Jan 30, 2025
f50a66e
refactor: enhance buildConfigURL to accept dynamic root path and upda…
fnhipster Jan 30, 2025
6bccb24
refactor: simplify fetchPlaceholders to support single store fetching
fnhipster Jan 30, 2025
f7458e3
refactor: update fetchPlaceholders import paths to use aem.js
fnhipster Jan 30, 2025
4af4b69
Update scripts/configs.js
fnhipster Jan 30, 2025
30be7c5
refactor: enhance fetchPlaceholders to support multiple source fetchi…
fnhipster Jan 31, 2025
396deb2
refactor: update fetchPlaceholders to handle empty string for overrides
fnhipster Jan 31, 2025
a0fa27a
refactor: update loadFragment to use getRootPath for dynamic URL cons…
fnhipster Jan 31, 2025
5f78829
USF-1918: Sharepoint documents have a two-tier structure with English…
anthoula Jan 31, 2025
9156bd9
fix: correct logical condition for link href validation
fnhipster Feb 1, 2025
7e9133b
USF-1918: Sharepoint documents have a two-tier structure with English…
anthoula Feb 3, 2025
91a34b7
feat: add dynamic metadata fetching for 404 page based on URL patterns
fnhipster Feb 3, 2025
1a7c265
Merge branch 'multistore' of https://github.com/hlxsites/aem-boilerpl…
fnhipster Feb 3, 2025
a2e1271
USF-1918: Sharepoint documents have a two-tier structure with English…
anthoula Feb 3, 2025
7c6a4f2
USF-1918: Sharepoint documents have a two-tier structure with English…
anthoula Feb 3, 2025
e63b1f6
Update scripts/scripts.js
fnhipster Feb 4, 2025
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
2 changes: 1 addition & 1 deletion blocks/commerce-order-header/commerce-order-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { events } from '@dropins/tools/event-bus.js';
import { Header, provider as uiProvider } from '@dropins/tools/components.js';
import { CUSTOMER_ORDER_DETAILS_PATH, CUSTOMER_ORDERS_PATH } from '../../scripts/constants.js';
import { fetchPlaceholders } from '../../scripts/aem.js';
import { fetchPlaceholders } from '../../scripts/scripts.js';

export default async function decorate(block) {
block.innerHTML = '';
Expand Down
2 changes: 1 addition & 1 deletion blocks/commerce-return-header/commerce-return-header.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { events } from '@dropins/tools/event-bus.js';
import { Header, provider as uiProvider } from '@dropins/tools/components.js';
import { CUSTOMER_RETURN_DETAILS_PATH, CUSTOMER_RETURNS_PATH } from '../../scripts/constants.js';
import { fetchPlaceholders } from '../../scripts/aem.js';
import { fetchPlaceholders } from '../../scripts/scripts.js';

export default async function decorate(block) {
block.innerHTML = '';
Expand Down
2 changes: 1 addition & 1 deletion blocks/product-details/product-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import ProductGallery from '@dropins/storefront-pdp/containers/ProductGallery.js

// Libs
import { setJsonLd } from '../../scripts/commerce.js';
import { fetchPlaceholders } from '../../scripts/aem.js';
import { fetchPlaceholders } from '../../scripts/scripts.js';

// Initializers
import { IMAGES_SIZES } from '../../scripts/initializers/pdp.js';
Expand Down
1 change: 0 additions & 1 deletion head.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
<script src="/scripts/configs.js" type="module"></script>
<script src="/scripts/commerce.js" type="module"></script>

<link rel="preload" href="/placeholders.json" as="fetch" crossorigin="anonymous" />
<link rel="modulepreload" href="/scripts/initializers/index.js" />
<link rel="modulepreload" href="/scripts/initializers/auth.js" />
<link rel="modulepreload" href="/scripts/initializers/cart.js" />
Expand Down
23 changes: 17 additions & 6 deletions scripts/configs.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getRootPath } from './scripts.js';

Check failure on line 1 in scripts/configs.js

View workflow job for this annotation

GitHub Actions / build

Dependency cycle via ./initializers/pdp.js:191

const ALLOWED_CONFIGS = ['prod', 'stage', 'dev'];

/**
Expand Down Expand Up @@ -29,21 +31,22 @@
return environment;
};

function buildConfigURL(environment) {
function buildConfigURL(environment, root = '/') {
const env = environment || calcEnvironment();
let fileName = 'configs.json';
if (env !== 'prod') {
fileName = `configs-${env}.json`;
}
const configURL = new URL(`${window.location.origin}/${fileName}`);
const configURL = new URL(`${window.location.origin}${root}${fileName}`);
return configURL;
}

const getConfigForEnvironment = async (environment) => {
const env = environment || calcEnvironment();
const root = getRootPath() || '/';

try {
const configJSON = window.sessionStorage.getItem(`config:${env}`);
const configJSON = window.sessionStorage.getItem(`config:${env}:${root}:${root}`);
fnhipster marked this conversation as resolved.
Show resolved Hide resolved
if (!configJSON) {
throw new Error('No config in session storage');
}
Expand All @@ -55,13 +58,13 @@

return parsedConfig;
} catch (e) {
let configJSON = await fetch(buildConfigURL(env));
let configJSON = await fetch(buildConfigURL(env, root));
if (!configJSON.ok) {
throw new Error(`Failed to fetch config for ${env}`);
}
configJSON = await configJSON.json();
configJSON[':expiry'] = Math.round(Date.now() / 1000) + 7200;
window.sessionStorage.setItem(`config:${env}`, JSON.stringify(configJSON));
window.sessionStorage.setItem(`config:${env}:${root}`, JSON.stringify(configJSON));
return configJSON;
}
};
Expand All @@ -87,13 +90,21 @@
export const getHeaders = async (scope, environment) => {
const env = environment || calcEnvironment();
const config = await getConfigForEnvironment(env);
const configElements = config.data.filter((el) => el?.key.includes(`headers.${scope}`));
const configElements = config.data.filter((el) => el?.key.includes('headers.all') || el?.key.includes(`headers.${scope}`));

return configElements.reduce((obj, item) => {
let { key } = item;

// global values
if (key.includes('commerce.headers.all.')) {
herzog31 marked this conversation as resolved.
Show resolved Hide resolved
key = key.replace('commerce.headers.all.', '');
}

// scoped values
if (key.includes(`commerce.headers.${scope}.`)) {
key = key.replace(`commerce.headers.${scope}.`, '');
}

return { ...obj, [key]: item.value };
}, {});
};
Expand Down
2 changes: 1 addition & 1 deletion scripts/initializers/account.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { initializers } from '@dropins/tools/initializer.js';
import { initialize, setFetchGraphQlHeaders } from '@dropins/storefront-account/api.js';
import { initializeDropin } from './index.js';
import { fetchPlaceholders } from '../aem.js';
import { fetchPlaceholders } from '../scripts.js';
import { getHeaders } from '../configs.js';

await initializeDropin(async () => {
Expand Down
2 changes: 1 addition & 1 deletion scripts/initializers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { initializers } from '@dropins/tools/initializer.js';
import { initialize, setFetchGraphQlHeaders } from '@dropins/storefront-auth/api.js';
import { initializeDropin } from './index.js';
import { fetchPlaceholders } from '../aem.js';
import { fetchPlaceholders } from '../scripts.js';
import { getHeaders } from '../configs.js';

await initializeDropin(async () => {
Expand Down
2 changes: 1 addition & 1 deletion scripts/initializers/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { initializers } from '@dropins/tools/initializer.js';
import { initialize, setFetchGraphQlHeaders } from '@dropins/storefront-cart/api.js';
import { initializeDropin } from './index.js';
import { fetchPlaceholders } from '../aem.js';
import { fetchPlaceholders } from '../scripts.js';
import { getHeaders } from '../configs.js';

await initializeDropin(async () => {
Expand Down
2 changes: 1 addition & 1 deletion scripts/initializers/checkout.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { initializers } from '@dropins/tools/initializer.js';
import { initialize, setFetchGraphQlHeaders } from '@dropins/storefront-checkout/api.js';
import { initializeDropin } from './index.js';
import { fetchPlaceholders } from '../aem.js';
import { fetchPlaceholders } from '../scripts.js';
import { getHeaders } from '../configs.js';

await initializeDropin(async () => {
Expand Down
2 changes: 1 addition & 1 deletion scripts/initializers/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { initializers } from '@dropins/tools/initializer.js';
import { initialize, setFetchGraphQlHeaders } from '@dropins/storefront-order/api.js';
import { checkIsAuthenticated, getHeaders } from '../configs.js';
import { initializeDropin } from './index.js';
import { fetchPlaceholders } from '../aem.js';
import { fetchPlaceholders } from '../scripts.js';

import {
CUSTOMER_ORDER_DETAILS_PATH,
Expand Down
4 changes: 2 additions & 2 deletions scripts/initializers/pdp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable import/no-cycle */
/* eslint-disable import/prefer-default-export */
/* eslint import/no-cycle: [2, { maxDepth: 1 }] */

import { initializers } from '@dropins/tools/initializer.js';
import { Image, provider as UI } from '@dropins/tools/components.js';
Expand All @@ -17,7 +17,7 @@ import {
loadErrorPage,
} from '../commerce.js';
import { getHeaders } from '../configs.js';
import { fetchPlaceholders } from '../aem.js';
import { fetchPlaceholders } from '../scripts.js';

export const IMAGES_SIZES = {
width: 960,
Expand Down
17 changes: 17 additions & 0 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
loadSections,
loadCSS,
sampleRUM,
fetchPlaceholders as _fetchPlaceholders,
} from './aem.js';
import { trackHistory } from './commerce.js';
import initializeDropins from './initializers/index.js';
Expand Down Expand Up @@ -356,6 +357,22 @@ export async function fetchIndex(indexFile, pageSize = 500) {
return newIndex;
}

/**
* Get root path
*/
export function getRootPath() {
window.ROOT_PATH = window.rootPath || getMetadata('root') || '/';
fnhipster marked this conversation as resolved.
Show resolved Hide resolved
return window.ROOT_PATH;
}

/**
* Fetch Placeholders with multi-store support.
*/
export async function fetchPlaceholders() {
const lang = getRootPath().replace(/\/$/, '') ?? undefined;
return _fetchPlaceholders(lang);
}

/**
* Check if consent was given for a specific topic.
* @param {*} topic Topic identifier
Expand Down
Loading