Skip to content

Commit

Permalink
Adds sitemap. (vercel#982)
Browse files Browse the repository at this point in the history
  • Loading branch information
manovotny authored Apr 23, 2023
1 parent ee900a4 commit a53ee3e
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 13 deletions.
33 changes: 33 additions & 0 deletions app/sitemap.ts
Original file line number Diff line number Diff line change
@@ -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<Promise<Promise<MetadataRoute.Sitemap>>> {
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];
}
1 change: 1 addition & 0 deletions lib/shopify/fragments/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const productFragment = /* GraphQL */ `
...seo
}
tags
updatedAt
}
${imageFragment}
${seoFragment}
Expand Down
14 changes: 12 additions & 2 deletions lib/shopify/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -36,6 +36,7 @@ import {
ShopifyCreateCartOperation,
ShopifyMenuOperation,
ShopifyPageOperation,
ShopifyPagesOperation,
ShopifyProduct,
ShopifyProductOperation,
ShopifyProductRecommendationsOperation,
Expand Down Expand Up @@ -279,7 +280,8 @@ export async function getCollections(): Promise<Collection[]> {
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.
Expand Down Expand Up @@ -316,6 +318,14 @@ export async function getPage(handle: string): Promise<Page> {
return res.body.data.pageByHandle;
}

export async function getPages(): Promise<Page[]> {
const res = await shopifyFetch<ShopifyPagesOperation>({
query: getPagesQuery
});

return removeEdgesAndNodes(res.body.data.pages);
}

export async function getProduct(handle: string): Promise<Product | undefined> {
const res = await shopifyFetch<ShopifyProductOperation>({
query: getProductQuery,
Expand Down
1 change: 1 addition & 0 deletions lib/shopify/queries/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const collectionFragment = /* GraphQL */ `
seo {
...seo
}
updatedAt
}
${seoFragment}
`;
Expand Down
42 changes: 31 additions & 11 deletions lib/shopify/queries/page.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
import seoFragment from '../fragments/seo';

const pageFragment = /* GraphQL */ `
fragment page on Page {
... on Page {
id
title
handle
body
bodySummary
seo {
...seo
}
createdAt
updatedAt
}
}
${seoFragment}
`;

export const getPageQuery = /* GraphQL */ `
query getPage($handle: String!) {
pageByHandle(handle: $handle) {
id
... on Page {
title
handle
body
bodySummary
seo {
...seo
...page
}
}
${pageFragment}
`;

export const getPagesQuery = /* GraphQL */ `
query getPages {
pages(first: 100) {
edges {
node {
...page
}
createdAt
updatedAt
}
}
}
${seoFragment}
${pageFragment}
`;
2 changes: 2 additions & 0 deletions lib/shopify/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export type ShopifyCollection = {
title: string;
description: string;
seo: SEO;
updatedAt: string;
};

export type ShopifyProduct = {
Expand All @@ -124,6 +125,7 @@ export type ShopifyProduct = {
images: Connection<Image>;
seo: SEO;
tags: string[];
updatedAt: string;
};

export type ShopifyCartOperation = {
Expand Down

0 comments on commit a53ee3e

Please sign in to comment.