From c37d1ec016bc26c2a59296b0c76c14b513d94306 Mon Sep 17 00:00:00 2001 From: Paul Hachmang Date: Mon, 16 Dec 2024 12:00:24 +0100 Subject: [PATCH] GCOM-1489: Make sure categories and products create the correct URL's in sitemaps --- .changeset/few-adults-relate.md | 5 +++++ .../pages/sitemap/categories.xml.tsx | 14 +++++++----- .../pages/sitemap/content.xml.tsx | 3 +-- .../pages/sitemap/products.xml.tsx | 18 +++++++-------- .../pages/sitemap/categories.xml.tsx | 18 +++++++++------ .../pages/sitemap/content.xml.tsx | 8 +++---- .../pages/sitemap/products.xml.tsx | 22 +++++++++---------- 7 files changed, 49 insertions(+), 39 deletions(-) create mode 100644 .changeset/few-adults-relate.md diff --git a/.changeset/few-adults-relate.md b/.changeset/few-adults-relate.md new file mode 100644 index 0000000000..a052daface --- /dev/null +++ b/.changeset/few-adults-relate.md @@ -0,0 +1,5 @@ +--- +'@graphcommerce/next-config': patch +--- + +Make sure categories and products create the correct URL's in sitemaps diff --git a/examples/magento-graphcms/pages/sitemap/categories.xml.tsx b/examples/magento-graphcms/pages/sitemap/categories.xml.tsx index ecece1a26d..1504583e30 100644 --- a/examples/magento-graphcms/pages/sitemap/categories.xml.tsx +++ b/examples/magento-graphcms/pages/sitemap/categories.xml.tsx @@ -7,17 +7,21 @@ import { } from '@graphcommerce/next-ui' import { GetServerSideProps } from 'next' import { graphqlSsrClient } from '../../lib/graphql/graphqlSsrClient' +import { productListLink } from '@graphcommerce/magento-product' -const excludes = [] -const additionalPaths = [] +const excludes: string[] = [] +const additionalPaths: string[] = [] export const getServerSideProps: GetServerSideProps = async (context) => { const { locale } = context if (!locale) throw Error('Locale not found') - const categoryPaths = await getCategoryStaticPaths(graphqlSsrClient(context), locale, { - limit: false, - }) + const client = graphqlSsrClient(context) + const categoryRoutes = await getCategoryStaticPaths(client, locale, { limit: false }) + + const categoryPaths = categoryRoutes + .map(staticPathsToString) + .map((url) => productListLink({ url, filters: {}, sort: {} }).slice(1)) const paths = [...categoryPaths, ...additionalPaths] .map(staticPathsToString) diff --git a/examples/magento-graphcms/pages/sitemap/content.xml.tsx b/examples/magento-graphcms/pages/sitemap/content.xml.tsx index 2ff494c5b3..b0969d6b55 100644 --- a/examples/magento-graphcms/pages/sitemap/content.xml.tsx +++ b/examples/magento-graphcms/pages/sitemap/content.xml.tsx @@ -11,7 +11,6 @@ import { graphqlSsrClient } from '../../lib/graphql/graphqlSsrClient' const excludes = [ '*page/home', - '*/p/*', '*/product/global', '*/product*', '*/account*', @@ -31,7 +30,7 @@ const excludes = [ '*/cart', '*/checkout', ] -const additionalPaths = [''] +const additionalPaths: string[] = [] export const getServerSideProps: GetServerSideProps = async (context) => { const { locale } = context diff --git a/examples/magento-graphcms/pages/sitemap/products.xml.tsx b/examples/magento-graphcms/pages/sitemap/products.xml.tsx index 21f9a08906..25922ed4ef 100644 --- a/examples/magento-graphcms/pages/sitemap/products.xml.tsx +++ b/examples/magento-graphcms/pages/sitemap/products.xml.tsx @@ -1,4 +1,3 @@ -import { getProductStaticPaths } from '@graphcommerce/magento-product' import { excludeSitemap, staticPathsToString, @@ -7,22 +6,23 @@ import { } from '@graphcommerce/next-ui' import { GetServerSideProps } from 'next' import { graphqlSsrClient } from '../../lib/graphql/graphqlSsrClient' +import { getProductStaticPaths, productPath } from '@graphcommerce/magento-product' -const excludes = [] -const additionalPaths = [] +const excludes: string[] = [] +const additionalPaths: string[] = [] export const getServerSideProps: GetServerSideProps = async (context) => { const { locale } = context - if (!locale) throw Error('Locale not defined') - const productPaths = await getProductStaticPaths(graphqlSsrClient(context), locale, { - limit: false, - }) + const client = graphqlSsrClient(context) + const productRoutes = await getProductStaticPaths(client, locale, { limit: false }) - const paths = [...productPaths, ...additionalPaths] + const productPaths = productRoutes .map(staticPathsToString) - .filter(excludeSitemap(excludes)) + .map((path) => productPath(path).slice(1)) + + const paths = [...productPaths, ...additionalPaths].filter(excludeSitemap(excludes)) return getServerSidePropsSitemap(context, toSitemapFields(context, paths)) } diff --git a/examples/magento-open-source/pages/sitemap/categories.xml.tsx b/examples/magento-open-source/pages/sitemap/categories.xml.tsx index 0e294771cf..1504583e30 100644 --- a/examples/magento-open-source/pages/sitemap/categories.xml.tsx +++ b/examples/magento-open-source/pages/sitemap/categories.xml.tsx @@ -1,23 +1,27 @@ import { getCategoryStaticPaths } from '@graphcommerce/magento-category' import { excludeSitemap, - getServerSidePropsSitemap, staticPathsToString, + getServerSidePropsSitemap, toSitemapFields, } from '@graphcommerce/next-ui' -import type { GetServerSideProps } from 'next' +import { GetServerSideProps } from 'next' import { graphqlSsrClient } from '../../lib/graphql/graphqlSsrClient' +import { productListLink } from '@graphcommerce/magento-product' -const excludes = [] -const additionalPaths = [] +const excludes: string[] = [] +const additionalPaths: string[] = [] export const getServerSideProps: GetServerSideProps = async (context) => { const { locale } = context if (!locale) throw Error('Locale not found') - const categoryPaths = await getCategoryStaticPaths(graphqlSsrClient(context), locale, { - limit: false, - }) + const client = graphqlSsrClient(context) + const categoryRoutes = await getCategoryStaticPaths(client, locale, { limit: false }) + + const categoryPaths = categoryRoutes + .map(staticPathsToString) + .map((url) => productListLink({ url, filters: {}, sort: {} }).slice(1)) const paths = [...categoryPaths, ...additionalPaths] .map(staticPathsToString) diff --git a/examples/magento-open-source/pages/sitemap/content.xml.tsx b/examples/magento-open-source/pages/sitemap/content.xml.tsx index e345529c3d..61bbf87f6d 100644 --- a/examples/magento-open-source/pages/sitemap/content.xml.tsx +++ b/examples/magento-open-source/pages/sitemap/content.xml.tsx @@ -4,11 +4,9 @@ import { staticPathsToString, toSitemapFields, } from '@graphcommerce/next-ui' -import type { GetServerSideProps } from 'next' -import { graphqlSsrClient } from '../../lib/graphql/graphqlSsrClient' +import { GetServerSideProps } from 'next' -const excludes: string[] = [ - '*/p/*', +const excludes = [ '*/account*', '*/wishlist*', '*/cart*', @@ -23,7 +21,7 @@ const excludes: string[] = [ '*/cart', '*/checkout', ] -const additionalPaths = [''] +const additionalPaths: string[] = [] export const getServerSideProps: GetServerSideProps = async (context) => { const { locale } = context diff --git a/examples/magento-open-source/pages/sitemap/products.xml.tsx b/examples/magento-open-source/pages/sitemap/products.xml.tsx index c17a5eee46..25922ed4ef 100644 --- a/examples/magento-open-source/pages/sitemap/products.xml.tsx +++ b/examples/magento-open-source/pages/sitemap/products.xml.tsx @@ -1,28 +1,28 @@ -import { getProductStaticPaths } from '@graphcommerce/magento-product' import { excludeSitemap, - getServerSidePropsSitemap, staticPathsToString, + getServerSidePropsSitemap, toSitemapFields, } from '@graphcommerce/next-ui' -import type { GetServerSideProps } from 'next' +import { GetServerSideProps } from 'next' import { graphqlSsrClient } from '../../lib/graphql/graphqlSsrClient' +import { getProductStaticPaths, productPath } from '@graphcommerce/magento-product' -const excludes = [] -const additionalPaths = [] +const excludes: string[] = [] +const additionalPaths: string[] = [] export const getServerSideProps: GetServerSideProps = async (context) => { const { locale } = context - if (!locale) throw Error('Locale not defined') - const productPaths = await getProductStaticPaths(graphqlSsrClient(context), locale, { - limit: false, - }) + const client = graphqlSsrClient(context) + const productRoutes = await getProductStaticPaths(client, locale, { limit: false }) - const paths = [...productPaths, ...additionalPaths] + const productPaths = productRoutes .map(staticPathsToString) - .filter(excludeSitemap(excludes)) + .map((path) => productPath(path).slice(1)) + + const paths = [...productPaths, ...additionalPaths].filter(excludeSitemap(excludes)) return getServerSidePropsSitemap(context, toSitemapFields(context, paths)) }