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

Use shop.primaryDomain to mark internal links in demo store #1036

Merged
merged 4 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/silly-stingrays-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'demo-store': patch
---

Fix demostore to check for `shop.primaryDomain` host for navigation menu items.
17 changes: 8 additions & 9 deletions templates/demo-store/app/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function resolveToFromType(
/*
Parse each menu link and adding, isExternal, to and target
*/
function parseItem(customPrefixes = {}) {
function parseItem(primaryDomain: string, customPrefixes = {}) {
return function (
item:
| MenuFragment['items'][number]
Expand All @@ -161,13 +161,9 @@ function parseItem(customPrefixes = {}) {
}

// extract path from url because we don't need the origin on internal to attributes
const {pathname} = new URL(item.url);
const {host, pathname} = new URL(item.url);

/*
Currently the MenuAPI only returns online store urls e.g — xyz.myshopify.com/..
Note: update logic when API is updated to include the active qualified domain
*/
const isInternalLink = /\.myshopify\.com/g.test(item.url);
const isInternalLink = host === new URL(primaryDomain).host;

const parsedItem = isInternalLink
? // internal links
Expand All @@ -188,7 +184,9 @@ function parseItem(customPrefixes = {}) {
if ('items' in item) {
return {
...parsedItem,
items: item.items.map(parseItem(customPrefixes)).filter(Boolean),
items: item.items
.map(parseItem(primaryDomain, customPrefixes))
.filter(Boolean),
} as EnhancedMenu['items'][number];
} else {
return parsedItem as EnhancedMenu['items'][number]['items'][number];
Expand All @@ -203,6 +201,7 @@ function parseItem(customPrefixes = {}) {
*/
export function parseMenu(
menu: MenuFragment,
primaryDomain: string,
customPrefixes = {},
): EnhancedMenu | null {
if (!menu?.items) {
Expand All @@ -211,7 +210,7 @@ export function parseMenu(
return null;
}

const parser = parseItem(customPrefixes);
const parser = parseItem(primaryDomain, customPrefixes);

const parsedMenu = {
...menu,
Expand Down
4 changes: 2 additions & 2 deletions templates/demo-store/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,11 @@ async function getLayoutData({storefront}: AppLoadContext) {
const customPrefixes = {BLOG: '', CATALOG: 'products'};

const headerMenu = data?.headerMenu
? parseMenu(data.headerMenu, customPrefixes)
? parseMenu(data.headerMenu, data.shop.primaryDomain.url, customPrefixes)
: undefined;

const footerMenu = data?.footerMenu
? parseMenu(data.footerMenu, customPrefixes)
? parseMenu(data.footerMenu, data.shop.primaryDomain.url, customPrefixes)
: undefined;

return {shop: data.shop, headerMenu, footerMenu};
Expand Down