diff --git a/app/sitemap.ts b/app/sitemap.ts new file mode 100644 index 000000000..1a0fd8232 --- /dev/null +++ b/app/sitemap.ts @@ -0,0 +1,33 @@ +import { getCollections, getPages, getProducts } from 'lib/shopify'; +import { MetadataRoute } from 'next'; + +const baseUrl = process.env.NEXT_PUBLIC_VERCEL_URL + ? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}` + : 'http://localhost:3000'; + +export default async function sitemap(): Promise>> { + const routesMap = ['', '/search'].map((route) => ({ + url: `${baseUrl}${route}`, + lastModified: new Date().toISOString() + })); + + const collections = await getCollections(); + const collectionsMap = collections.map((collection) => ({ + url: `${baseUrl}${collection.path}`, + lastModified: collection.updatedAt + })); + + const products = await getProducts({}); + const productsMap = products.map((product) => ({ + url: `${baseUrl}/product/${product.handle}`, + lastModified: product.updatedAt + })); + + const pages = await getPages(); + const pagesMap = pages.map((page) => ({ + url: `${baseUrl}/${page.handle}`, + lastModified: page.updatedAt + })); + + return [...routesMap, ...collectionsMap, ...productsMap, ...pagesMap]; +} diff --git a/lib/shopify/fragments/product.ts b/lib/shopify/fragments/product.ts index b66ef05e7..be14dedca 100644 --- a/lib/shopify/fragments/product.ts +++ b/lib/shopify/fragments/product.ts @@ -55,6 +55,7 @@ const productFragment = /* GraphQL */ ` ...seo } tags + updatedAt } ${imageFragment} ${seoFragment} diff --git a/lib/shopify/index.ts b/lib/shopify/index.ts index 2c020b5d5..af76e3e26 100644 --- a/lib/shopify/index.ts +++ b/lib/shopify/index.ts @@ -13,7 +13,7 @@ import { getCollectionsQuery } from './queries/collection'; import { getMenuQuery } from './queries/menu'; -import { getPageQuery } from './queries/page'; +import { getPageQuery, getPagesQuery } from './queries/page'; import { getProductQuery, getProductRecommendationsQuery, @@ -36,6 +36,7 @@ import { ShopifyCreateCartOperation, ShopifyMenuOperation, ShopifyPageOperation, + ShopifyPagesOperation, ShopifyProduct, ShopifyProductOperation, ShopifyProductRecommendationsOperation, @@ -279,7 +280,8 @@ export async function getCollections(): Promise { title: 'All', description: 'All products' }, - path: '/search' + path: '/search', + updatedAt: new Date().toISOString() }, // Filter out the `hidden` collections. // Collections that start with `hidden-*` need to be hidden on the search page. @@ -316,6 +318,14 @@ export async function getPage(handle: string): Promise { return res.body.data.pageByHandle; } +export async function getPages(): Promise { + const res = await shopifyFetch({ + query: getPagesQuery + }); + + return removeEdgesAndNodes(res.body.data.pages); +} + export async function getProduct(handle: string): Promise { const res = await shopifyFetch({ query: getProductQuery, diff --git a/lib/shopify/queries/collection.ts b/lib/shopify/queries/collection.ts index 423cb3217..ac3fb4dd9 100644 --- a/lib/shopify/queries/collection.ts +++ b/lib/shopify/queries/collection.ts @@ -9,6 +9,7 @@ const collectionFragment = /* GraphQL */ ` seo { ...seo } + updatedAt } ${seoFragment} `; diff --git a/lib/shopify/queries/page.ts b/lib/shopify/queries/page.ts index 34ec27d28..ac6f6f986 100644 --- a/lib/shopify/queries/page.ts +++ b/lib/shopify/queries/page.ts @@ -1,21 +1,41 @@ import seoFragment from '../fragments/seo'; -export const getPageQuery = /* GraphQL */ ` - query getPage($handle: String!) { - pageByHandle(handle: $handle) { +const pageFragment = /* GraphQL */ ` + fragment page on Page { + ... on Page { id - ... on Page { - title - handle - body - bodySummary - seo { - ...seo - } - createdAt - updatedAt + title + handle + body + bodySummary + seo { + ...seo } + createdAt + updatedAt } } ${seoFragment} `; + +export const getPageQuery = /* GraphQL */ ` + query getPage($handle: String!) { + pageByHandle(handle: $handle) { + ...page + } + } + ${pageFragment} +`; + +export const getPagesQuery = /* GraphQL */ ` + query getPages { + pages(first: 100) { + edges { + node { + ...page + } + } + } + } + ${pageFragment} +`; diff --git a/lib/shopify/types.ts b/lib/shopify/types.ts index aca4c413b..b18ed3b63 100644 --- a/lib/shopify/types.ts +++ b/lib/shopify/types.ts @@ -105,6 +105,7 @@ export type ShopifyCollection = { title: string; description: string; seo: SEO; + updatedAt: string; }; export type ShopifyProduct = { @@ -124,6 +125,7 @@ export type ShopifyProduct = { images: Connection; seo: SEO; tags: string[]; + updatedAt: string; }; export type ShopifyCartOperation = {