Files
web/apps/scandic-web/utils/sitemap.ts
Anton Gunnarsson f79ff9b570 Merged in chore/cleanup-unused (pull request #3461)
chore: Cleanup unused vars, exports, types

* Cleanup some unused exports

* Remove more

* Readd CampaignPageIncludedHotelsRef

* Add alias comment to procedure exports

* Remove unused exports


Approved-by: Linus Flood
2026-01-22 12:34:07 +00:00

90 lines
2.6 KiB
TypeScript

import { getStore } from "@netlify/blobs"
import { env } from "@/env/server"
import type { HotelFilterEntry, SitemapData, SyncItem } from "@/types/sitemap"
const branch = env.CMS_BRANCH
const environment = env.CMS_ENVIRONMENT
const entriesKey = `${environment}/${branch}/entries`
const hotelFiltersKey = `${environment}/${branch}/hotelFilters`
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.
// To run this locally, you need to change the arguments to `getStore` like this:
// return getStore({
// name: "sitemap",
// siteID: "SITE_ID from netlify",
// token: "Personal access token from Netlify",
// })
// See https://docs.netlify.com/build/data-and-storage/netlify-blobs/#getstore for more info.
function store() {
return getStore("sitemap")
}
export async function saveEntries(entries: SyncItem[]) {
await store().setJSON(entriesKey, entries)
}
export async function saveHotelFilters(
hotelFilters: Record<string, HotelFilterEntry[]>
) {
await store().setJSON(hotelFiltersKey, hotelFilters)
}
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)
}
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)
}