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
113 lines
2.9 KiB
TypeScript
113 lines
2.9 KiB
TypeScript
import { Region, Stack } from "contentstack"
|
|
|
|
import { env } from "@/env/server"
|
|
|
|
import {
|
|
syncEntriesCounter,
|
|
syncEntriesFailCounter,
|
|
syncEntriesPaginationCounter,
|
|
syncEntriesSuccessCounter,
|
|
} from "./telemetry"
|
|
|
|
import type { SyncResponse } from "@/types/sitemap"
|
|
|
|
const environment = env.CMS_ENVIRONMENT
|
|
const stack = Stack({
|
|
api_key: env.CMS_API_KEY,
|
|
delivery_token: env.CMS_ACCESS_TOKEN,
|
|
branch: env.CMS_BRANCH,
|
|
environment,
|
|
region: Region.EU,
|
|
})
|
|
|
|
export async function contentstackSync(syncToken: string | null) {
|
|
const entries = []
|
|
const syncOptions = syncToken ? { sync_token: syncToken } : { init: true }
|
|
|
|
syncEntriesCounter.add(1, {
|
|
environment,
|
|
...syncOptions,
|
|
})
|
|
console.info(
|
|
"sitemap.entries.sync start",
|
|
JSON.stringify({
|
|
environment,
|
|
...syncOptions,
|
|
})
|
|
)
|
|
try {
|
|
let syncResponse: SyncResponse = await stack.sync(syncOptions)
|
|
|
|
entries.push(...syncResponse.items)
|
|
|
|
// Check if there is a pagination token, and fetch more data if needed
|
|
while (syncResponse.pagination_token && !syncResponse.sync_token) {
|
|
syncEntriesPaginationCounter.add(1, {
|
|
environment,
|
|
paginationToken: syncResponse.pagination_token,
|
|
})
|
|
console.info(
|
|
"sitemap.entries.sync.pagination start",
|
|
JSON.stringify({
|
|
environment,
|
|
paginationToken: syncResponse.pagination_token,
|
|
})
|
|
)
|
|
syncResponse = await stack.sync({
|
|
pagination_token: syncResponse.pagination_token,
|
|
})
|
|
|
|
entries.push(...syncResponse.items)
|
|
|
|
syncEntriesPaginationCounter.add(1, {
|
|
environment,
|
|
paginationToken: syncResponse.pagination_token,
|
|
entriesCount: syncResponse.items.length,
|
|
})
|
|
console.info(
|
|
"sitemap.entries.sync.pagination success",
|
|
JSON.stringify({
|
|
environment,
|
|
paginationToken: syncResponse.pagination_token,
|
|
entriesCount: syncResponse.items.length,
|
|
})
|
|
)
|
|
}
|
|
|
|
if (syncResponse.sync_token) {
|
|
syncEntriesSuccessCounter.add(1, {
|
|
environment,
|
|
...syncOptions,
|
|
newSyncToken: syncResponse.sync_token,
|
|
entriesCount: entries.length,
|
|
})
|
|
console.info(
|
|
"sitemap.entries.sync success",
|
|
JSON.stringify({
|
|
environment,
|
|
...syncOptions,
|
|
newSyncToken: syncResponse.sync_token,
|
|
entriesCount: entries.length,
|
|
})
|
|
)
|
|
return {
|
|
syncToken: syncResponse.sync_token,
|
|
entries,
|
|
}
|
|
} else {
|
|
throw new Error("No sync token received, something went wrong")
|
|
}
|
|
} catch (error) {
|
|
const errorMessage =
|
|
error instanceof Error ? error.message : JSON.stringify(error)
|
|
syncEntriesFailCounter.add(1, {
|
|
environment,
|
|
...syncOptions,
|
|
error: errorMessage,
|
|
})
|
|
console.error("sitemap.entries.sync error", errorMessage)
|
|
|
|
throw new Error("Failed to sync entries")
|
|
}
|
|
}
|