Skip to content

Commit

Permalink
Suport metafield pages better
Browse files Browse the repository at this point in the history
  • Loading branch information
blittle committed Aug 9, 2024
1 parent 562bd91 commit b819aeb
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 13 deletions.
96 changes: 84 additions & 12 deletions examples/sitemap/app/lib/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ export async function getSitemapIndex({
'pages',
'articles',
],
customUrls = [],
}: {
storefront: LoaderFunctionArgs['context']['storefront'];
request: Request;
types: SITEMAP_INDEX_TYPE[];
types?: SITEMAP_INDEX_TYPE[];
customUrls?: string[];
}) {
const data = await storefront.query(SITEMAP_INDEX_QUERY, {
storefrontApiVersion: 'unstable',
Expand All @@ -47,6 +49,13 @@ export async function getSitemapIndex({
${types
.map((type) => getSiteMapLinks(type, data[type].pagesCount.count, baseUrl))
.join('\n')}
${customUrls
.map(
(url) => `<sitemap>
<loc>${url}</loc>
</sitemap>`,
)
.join('\n')}
</urlset>`;

return new Response(body, {
Expand All @@ -66,13 +75,18 @@ type GetSiteMapOptions = {
request: Request;
/** A function that produces a canonical url for a resource. It is called multiple times for each locale supported by the app. */
getLink: (options: {
type: SITEMAP_INDEX_TYPE;
type: string | SITEMAP_INDEX_TYPE;
baseUrl: string;
handle?: string;
locale?: Locale;
}) => string;
/** An array of locales to generate alternate tags */
locales: Locale[];
/** Optionally customize the changefreq property for each URL */
getChangeFreq?: (options: {
type: string | SITEMAP_INDEX_TYPE;
handle: string;
}) => string;
};

/**
Expand Down Expand Up @@ -106,17 +120,19 @@ export async function getSitemap(options: GetSiteMapOptions) {
const body = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
${data.sitemap.resources.items
.map((item: {handle: string; updatedAt: string}) => {
.map((item: {handle: string; updatedAt: string; type?: string}) => {
return `${renderUrlTag({
getChangeFreq: options.getChangeFreq,
url: getLink({
type,
type: item.type ?? type,
baseUrl,
handle: item.handle,
}),
type,
getLink,
updatedAt: item.updatedAt,
handle: item.handle,
metaobjectType: item.type,
locales,
baseUrl,
})}`;
Expand All @@ -135,7 +151,7 @@ ${data.sitemap.resources.items
function getSiteMapLinks(resource: string, count: number, baseUrl: string) {
let links = ``;

for (let i = 0; i < count; i++) {
for (let i = 1; i <= count; i++) {
links += `<sitemap>
<loc>${baseUrl}/sitemap/${resource}/${i}.xml</loc>
</sitemap>`;
Expand All @@ -151,29 +167,40 @@ function renderUrlTag({
getLink,
baseUrl,
handle,
getChangeFreq,
metaobjectType,
}: {
type: SITEMAP_INDEX_TYPE;
baseUrl: string;
handle: string;
metaobjectType?: string;
getLink: (options: {
type: SITEMAP_INDEX_TYPE;
type: string;
baseUrl: string;
handle?: string;
locale?: Locale;
}) => string;
url: string;
updatedAt: string;
locales: Locale[];
getChangeFreq?: (options: {type: string; handle: string}) => string;
}) {
return `<url>
<loc>${url}</loc>
<lastmod>${updatedAt}</lastmod>
<changefreq>weekly</changefreq>
${locales
.map((locale) =>
renderAlternateTag(getLink({type, baseUrl, handle, locale}), locale),
)
.join('\n')}
<changefreq>${
getChangeFreq
? getChangeFreq({type: metaobjectType ?? type, handle})
: 'weekly'
}</changefreq>
${locales
.map((locale) =>
renderAlternateTag(
getLink({type: metaobjectType ?? type, baseUrl, handle, locale}),
locale,
),
)
.join('\n')}
</url>
`.trim();
}
Expand Down Expand Up @@ -221,6 +248,48 @@ const ARTICLE_SITEMAP_QUERY = `#graphql
}
` as const;

const PAGE_SITEMAP_QUERY = `#graphql
query SitemapProducts($page: Int!) {
sitemap(type: PAGE) {
resources(page: $page) {
items {
handle
updatedAt
}
}
}
}
` as const;

const BLOG_SITEMAP_QUERY = `#graphql
query SitemapProducts($page: Int!) {
sitemap(type: BLOG) {
resources(page: $page) {
items {
handle
updatedAt
}
}
}
}
` as const;

const METAOBJECT_SITEMAP_QUERY = `#graphql
query SitemapProducts($page: Int!) {
sitemap(type: METAOBJECT_PAGE) {
resources(page: $page) {
items {
handle
updatedAt
... on SitemapResourceMetaobject {
type
}
}
}
}
}
` as const;

const SITEMAP_INDEX_QUERY = `#graphql
query SitemapIndex {
products: sitemap(type: PRODUCT) {
Expand Down Expand Up @@ -260,4 +329,7 @@ const QUERIES = {
products: PRODUCT_SITEMAP_QUERY,
articles: ARTICLE_SITEMAP_QUERY,
collections: COLLECTION_SITEMAP_QUERY,
pages: PAGE_SITEMAP_QUERY,
blogs: BLOG_SITEMAP_QUERY,
metaObjects: METAOBJECT_SITEMAP_QUERY,
};
1 change: 0 additions & 1 deletion examples/sitemap/app/routes/[sitemap.xml].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export async function loader({
const response = await getSitemapIndex({
storefront,
request,
types: ['products', 'collections'],
});

response.headers.set('Cache-Control', `max-age=${60 * 60 * 24}`);
Expand Down

0 comments on commit b819aeb

Please sign in to comment.