forked from Shopify/hydrogen-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import type { LoaderArgs } from "@remix-run/cloudflare"; | ||
import { flattenConnection } from "@shopify/hydrogen-ui-alpha"; | ||
import { getSitemap } from "~/data"; | ||
|
||
const MAX_URLS = 250; // the google limit is 50K, however, SF API only allow querying for 250 resources each time | ||
|
||
export async function loader({ request }: LoaderArgs) { | ||
const data = await getSitemap({ | ||
language: "EN", | ||
urlLimits: MAX_URLS, | ||
}); | ||
|
||
return new Response( | ||
shopSitemap({ data, baseUrl: new URL(request.url).origin }), | ||
{ | ||
headers: { | ||
"content-type": "application/xml", | ||
// Cache for 24 hours | ||
"cache-control": `max-age=${60 * 60 * 24}`, | ||
}, | ||
} | ||
); | ||
} | ||
|
||
interface ProductEntry { | ||
url: string; | ||
lastMod: string; | ||
changeFreq: string; | ||
image?: { | ||
url: string; | ||
title?: string; | ||
caption?: string; | ||
}; | ||
} | ||
|
||
function shopSitemap({ | ||
data, | ||
baseUrl, | ||
}: { | ||
data: Awaited<ReturnType<typeof getSitemap>>; | ||
baseUrl: string; | ||
}) { | ||
const productsData = flattenConnection(data.products) | ||
.filter((product) => product.onlineStoreUrl) | ||
.map((product) => { | ||
const url = `${baseUrl}/products/${product.handle}`; | ||
|
||
const finalObject: ProductEntry = { | ||
url, | ||
lastMod: product.updatedAt!, | ||
changeFreq: "daily", | ||
}; | ||
|
||
if (product.featuredImage?.url) { | ||
finalObject.image = { | ||
url: product.featuredImage!.url, | ||
}; | ||
|
||
if (product.title) { | ||
finalObject.image.title = product.title; | ||
} | ||
|
||
if (product.featuredImage!.altText) { | ||
finalObject.image.caption = product.featuredImage!.altText; | ||
} | ||
} | ||
|
||
return finalObject; | ||
}); | ||
|
||
const collectionsData = flattenConnection(data.collections) | ||
.filter((collection) => collection.onlineStoreUrl) | ||
.map((collection) => { | ||
const url = `${baseUrl}/collections/${collection.handle}`; | ||
|
||
return { | ||
url, | ||
lastMod: collection.updatedAt, | ||
changeFreq: "daily", | ||
}; | ||
}); | ||
|
||
const pagesData = flattenConnection(data.pages) | ||
.filter((page) => page.onlineStoreUrl) | ||
.map((page) => { | ||
const url = `${baseUrl}/pages/${page.handle}`; | ||
|
||
return { | ||
url, | ||
lastMod: page.updatedAt, | ||
changeFreq: "weekly", | ||
}; | ||
}); | ||
|
||
const urlsDatas = [...productsData, ...collectionsData, ...pagesData]; | ||
|
||
return ` | ||
<urlset | ||
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" | ||
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" | ||
> | ||
${urlsDatas.map((url) => renderUrlTag(url!)).join("")} | ||
</urlset>`; | ||
} | ||
|
||
function renderUrlTag({ | ||
url, | ||
lastMod, | ||
changeFreq, | ||
image, | ||
}: { | ||
url: string; | ||
lastMod?: string; | ||
changeFreq?: string; | ||
image?: { | ||
url: string; | ||
title?: string; | ||
caption?: string; | ||
}; | ||
}) { | ||
return ` | ||
<url> | ||
<loc>${url}</loc> | ||
<lastmod>${lastMod}</lastmod> | ||
<changefreq>${changeFreq}</changefreq> | ||
${ | ||
image | ||
? ` | ||
<image:image> | ||
<image:loc>${image.url}</image:loc> | ||
<image:title>${image.title ?? ""}</image:title> | ||
<image:caption>${image.caption ?? ""}</image:caption> | ||
</image:image>` | ||
: "" | ||
} | ||
</url> | ||
`; | ||
} |