Skip to content

Commit

Permalink
Use shop.primaryDomain to mark internal links in demo store (#1036)
Browse files Browse the repository at this point in the history
Use shop.primaryDomain to mark internal links

---------

Co-authored-by: Bret Little <[email protected]>
  • Loading branch information
2 people authored and juanpprieto committed Jul 10, 2023
1 parent 2601408 commit c3b5cde
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
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

0 comments on commit c3b5cde

Please sign in to comment.