Skip to content

Commit

Permalink
Merge pull request #2135 from graphcommerce-org/feature/GCOM-1217-fix…
Browse files Browse the repository at this point in the history
…-canonicalize

Make `canonicalize()` work correctly when i18n domain routing is used
  • Loading branch information
paales authored Jan 15, 2024
2 parents 1d87862 + 2905aae commit 2274b6a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-bats-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphcommerce/next-ui': patch
---

Fix incorrect canonical URLs when i18n domain routing is used
5 changes: 4 additions & 1 deletion examples/magento-graphcms/graphcommerce.config.js.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const config = {
hygraphEndpoint: 'https://eu-central-1.cdn.hygraph.com/content/ckhx7xadya6xs01yxdujt8i80/master',
magentoEndpoint: 'https://backend.reachdigital.dev/graphql',
canonicalBaseUrl: 'https://graphcommerce.vercel.app',
storefront: [{ locale: 'en', magentoStoreCode: 'en_US' }],
storefront: [
{ locale: 'en', magentoStoreCode: 'en_US', defaultLocale: true },
{ locale: 'nl', magentoStoreCode: 'nl_NL' },
],
recentlyViewedProducts: {
enabled: true,
},
Expand Down
31 changes: 19 additions & 12 deletions packages/next-ui/PageMeta/PageMeta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,25 @@ export function canonicalize(router: PartialNextRouter, incoming?: Canonical) {
const curLocale = router.locale

// Copied from here https://github.com/vercel/next.js/blob/213c42f446874d29d07fa2cca6e6b11fc9c3b711/packages/next/client/link.tsx#L512
const localeDomain =
router.isLocaleDomain &&
getDomainLocale(as, curLocale, router && router.locales, router.domainLocales)

href = localeDomain || addBasePath(addLocale(as, curLocale, router.defaultLocale))

let siteUrl =
storefrontConfig(router.locale)?.canonicalBaseUrl ||
import.meta.graphCommerce.canonicalBaseUrl
if (siteUrl.endsWith('/')) siteUrl = siteUrl.slice(0, -1)

canonical = `${siteUrl}${href}`
const localeDomain = getDomainLocale(
as,
curLocale,
router && router.locales,
router.domainLocales,
)

if (localeDomain) {
canonical = localeDomain
} else {
href = addBasePath(addLocale(as, curLocale, router.defaultLocale))

let siteUrl =
storefrontConfig(router.locale)?.canonicalBaseUrl ||
import.meta.graphCommerce.canonicalBaseUrl
if (siteUrl.endsWith('/')) siteUrl = siteUrl.slice(0, -1)

canonical = `${siteUrl}${href}`
}
}

if (!canonical.startsWith('http')) {
Expand Down

1 comment on commit 2274b6a

@vercel
Copy link

@vercel vercel bot commented on 2274b6a Jan 15, 2024

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

graphcommerce-hygraph-dynamic-rows-ui – ./packages/hygraph-dynamic-rows-ui

graphcommerce-hygraph-dynamic-rows-ui-git-canary-graphcommerce.vercel.app
graphcommerce-hygraph-dynamic-rows-ui.vercel.app
graphcommerce-hygraph-dynamic-rows-ui-graphcommerce.vercel.app

Please sign in to comment.