Files
web/utils/sitemap.ts
Erik Tiekstra c93381ca80 Merged in feat/SW-550-sitemap (pull request #981)
feat(SW-550): added sync functionality and sitemap generation

* feat(SW-550): added sync functionality and sitemap generation

* feat(SW-550): Added support for splitting and saving multiple sitemaps when there are 50000+ urls

* feat(SW-550): Updates after PR

* feat(SW-550): Added locale to sitemap data

* feat(SW-550): Added support for locale based sitemapData

* feat(SW-550): Saving alternates of sitemap entries

* feat(SW-550): Refactoring to use sitemap utils file

* feat(SW-550): Using Netlify.env to get environment variables

* feat(SW-550): clarify use of functions


Approved-by: Michael Zetterberg
2025-02-17 10:35:11 +00:00

76 lines
2.1 KiB
TypeScript

import { getStore } from "@netlify/blobs"
import { env } from "@/env/server"
import type { SitemapData, SyncItem } from "@/types/sitemap"
const branch = env.CMS_BRANCH
const environment = env.CMS_ENVIRONMENT
const entriesKey = `${environment}/${branch}/entries`
const syncTokenKey = `${environment}/${branch}/syncToken`
const sitemapDataKey = `${environment}/${branch}/sitemapData`
const lastUpdatedKey = `${environment}/${branch}/lastUpdated`
const MAX_ENTRIES_PER_SITEMAP = 50000
// We need to wrap `getStore` because calling it in the root of the file causes
// it to be executed during build time. This is not supported by Netlify.
function store() {
return getStore("sitemap")
}
export async function saveEntries(entries: SyncItem[]) {
await store().setJSON(entriesKey, entries)
}
export async function saveSitemapData(sitemapData: SitemapData) {
await store().setJSON(sitemapDataKey, sitemapData)
}
export async function saveLastUpdatedDate(lastUpdated: string) {
await store().set(lastUpdatedKey, lastUpdated)
}
export async function saveSyncToken(syncToken: string) {
await store().set(syncTokenKey, syncToken)
}
export async function getSyncToken() {
return await store().get(syncTokenKey)
}
export async function getEntries() {
const entries: SyncItem[] = await store().get(entriesKey, {
type: "json",
})
return entries || []
}
export async function getLastUpdated() {
return await store().get(lastUpdatedKey)
}
export async function getSitemapData() {
const sitemapData: SitemapData | null = await store().get(sitemapDataKey, {
type: "json",
})
return sitemapData || []
}
export async function getSitemapDataById(id: number) {
const sitemapData = await getSitemapData()
const index = id - 1
const start = index * MAX_ENTRIES_PER_SITEMAP
const end = start + MAX_ENTRIES_PER_SITEMAP
return sitemapData.slice(start, end)
}
export async function getSitemapIds() {
const sitemapData = await getSitemapData()
const numberOfSitemaps = Math.ceil(
sitemapData.length / MAX_ENTRIES_PER_SITEMAP
)
return Array.from({ length: numberOfSitemaps }, (_, index) => index + 1)
}