Skip to content

Commit

Permalink
Add unstable sitemap abstraction (#2478)
Browse files Browse the repository at this point in the history
* Add a sitemap abstraction
  • Loading branch information
blittle authored Aug 29, 2024
1 parent 0acf9d6 commit 0c1e511
Show file tree
Hide file tree
Showing 12 changed files with 674 additions and 67 deletions.
7 changes: 7 additions & 0 deletions .changeset/slow-keys-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@shopify/hydrogen': patch
---

Introduce a new abstraction for generating sitemap index and child sitemaps.

See the [sitemap example](https://github.com/Shopify/hydrogen/tree/main/examples/sitemap) for how to use it and read the [docs](https://shopify.dev/docs/api/hydrogen/utilities/getSitemapIndex) for more information.
2 changes: 1 addition & 1 deletion examples/sitemap/app/routes/[sitemap.xml].tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {LoaderFunctionArgs} from '@shopify/remix-oxygen';
import {getSitemapIndex} from 'app/lib/sitemap';
import {unstable__getSitemapIndex as getSitemapIndex} from '@shopify/hydrogen';

export async function loader({
request,
Expand Down
2 changes: 1 addition & 1 deletion examples/sitemap/app/routes/sitemap.$type.$page[.xml].tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {LoaderFunctionArgs} from '@shopify/remix-oxygen';
import {getSitemap} from 'app/lib/sitemap';
import {unstable__getSitemap as getSitemap} from '@shopify/hydrogen';

export async function loader({
request,
Expand Down
5 changes: 5 additions & 0 deletions packages/hydrogen/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,8 @@ export {
createHydrogenContext,
type HydrogenContext,
} from './createHydrogenContext';

export {
getSitemapIndex as unstable__getSitemapIndex,
getSitemap as unstable__getSitemap,
} from './sitemap/sitemap';
43 changes: 43 additions & 0 deletions packages/hydrogen/src/sitemap/getSitemap.doc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {ReferenceEntityTemplateSchema} from '@shopify/generate-docs';

const data: ReferenceEntityTemplateSchema = {
name: 'getSitemap',
category: 'utilities',
isVisualComponent: false,
related: [
{
name: 'getSitemapIndex',
type: 'utilities',
url: '/api/hydrogen/utilities/getSitemapIndex',
},
],
description: `> Caution:\n> This component is in an unstable pre-release state and may have breaking changes in a future release.\n\nGenerate a sitemap for a specific resource type. Returns a standard Response object.`,
type: 'utility',
defaultExample: {
description: 'I am the default example',
codeblock: {
tabs: [
{
title: 'JavaScript',
code: './getSitemap.example.jsx',
language: 'js',
},
{
title: 'TypeScript',
code: './getSitemap.example.tsx',
language: 'ts',
},
],
title: 'Example code',
},
},
definitions: [
{
title: 'getSitemap',
type: 'GetSitemapGeneratedType',
description: '',
},
],
};

export default data;
21 changes: 21 additions & 0 deletions packages/hydrogen/src/sitemap/getSitemap.example.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {unstable__getSitemap as getSitemap} from '@shopify/hydrogen';

export async function loader({request, params, context: {storefront}}) {
const response = await getSitemap({
storefront,
request,
params,
// The locales to include in the sitemap
locales: ['EN-US', 'EN-CA', 'FR-CA'],
// A function to generate a link for a given resource
getLink: ({type, baseUrl, handle, locale}) => {
if (!locale) return `${baseUrl}/${type}/${handle}`;
return `${baseUrl}/${locale}/${type}/${handle}`;
},
});

// Set any custom headers on the sitemap response
response.headers.set('Cache-Control', `max-age=${60 * 60 * 24}`);

return response;
}
26 changes: 26 additions & 0 deletions packages/hydrogen/src/sitemap/getSitemap.example.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type {LoaderFunctionArgs} from '@shopify/remix-oxygen';
import {unstable__getSitemap as getSitemap} from '@shopify/hydrogen';

export async function loader({
request,
params,
context: {storefront},
}: LoaderFunctionArgs) {
const response = await getSitemap({
storefront,
request,
params,
// The locales to include in the sitemap
locales: ['EN-US', 'EN-CA', 'FR-CA'],
// A function to generate a link for a given resource
getLink: ({type, baseUrl, handle, locale}) => {
if (!locale) return `${baseUrl}/${type}/${handle}`;
return `${baseUrl}/${locale}/${type}/${handle}`;
},
});

// Set any custom headers on the sitemap response
response.headers.set('Cache-Control', `max-age=${60 * 60 * 24}`);

return response;
}
43 changes: 43 additions & 0 deletions packages/hydrogen/src/sitemap/getSitemapIndex.doc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {ReferenceEntityTemplateSchema} from '@shopify/generate-docs';

const data: ReferenceEntityTemplateSchema = {
name: 'getSitemapIndex',
category: 'utilities',
isVisualComponent: false,
related: [
{
name: 'getSitemap',
type: 'utilities',
url: '/api/hydrogen/utilities/getSitemap',
},
],
description: `> Caution:\n> This component is in an unstable pre-release state and may have breaking changes in a future release.\n\nGenerate a sitemap index that links to separate child sitemaps for different resource types. Returns a standard Response object.`,
type: 'utility',
defaultExample: {
description: 'I am the default example',
codeblock: {
tabs: [
{
title: 'JavaScript',
code: './getSitemapIndex.example.jsx',
language: 'js',
},
{
title: 'TypeScript',
code: './getSitemapIndex.example.tsx',
language: 'ts',
},
],
title: 'Example code',
},
},
definitions: [
{
title: 'getSitemapIndex',
type: 'GetSitemapIndexGeneratedType',
description: '',
},
],
};

export default data;
21 changes: 21 additions & 0 deletions packages/hydrogen/src/sitemap/getSitemapIndex.example.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {unstable__getSitemapIndex as getSitemapIndex} from '@shopify/hydrogen';

export async function loader({request, context: {storefront}}) {
const response = await getSitemapIndex({
storefront,
request,
types: [
'products',
'pages',
'collections',
'metaObjects',
'articles',
'blogs',
],
});

// Set any custom headers on the sitemap response
response.headers.set('Cache-Control', `max-age=${60 * 60 * 24}`);

return response;
}
25 changes: 25 additions & 0 deletions packages/hydrogen/src/sitemap/getSitemapIndex.example.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type {LoaderFunctionArgs} from '@shopify/remix-oxygen';
import {unstable__getSitemapIndex as getSitemapIndex} from '@shopify/hydrogen';

export async function loader({
request,
context: {storefront},
}: LoaderFunctionArgs) {
const response = await getSitemapIndex({
storefront,
request,
types: [
'products',
'pages',
'collections',
'metaObjects',
'articles',
'blogs',
],
});

// Set any custom headers on the sitemap response
response.headers.set('Cache-Control', `max-age=${60 * 60 * 24}`);

return response;
}
Loading

0 comments on commit 0c1e511

Please sign in to comment.