Files
web/apps/scandic-web/netlify/functions/sitemap.mts
Linus Flood 3b9d01af9d Merged in feat/sw-3006-timeout-fetch (pull request #2335)
feat(SW-3006): added default timeout to all requests

* feat(sw-3006): added default timeout to all requests

* Fixed spreading


Approved-by: Joakim Jäderberg
2025-06-11 12:39:58 +00:00

40 lines
1.3 KiB
TypeScript

/* eslint-disable import/no-anonymous-default-export */
import type { Config, Context } from "@netlify/functions"
export default async (request: Request, _context: Context) => {
const { next_run } = await request.json()
const SITEMAP_SYNC_SECRET = Netlify.env.get("SITEMAP_SYNC_SECRET")
const PUBLIC_URL = Netlify.env.get("PUBLIC_URL")
console.info(
`Started sitemap sync at: ${new Date().toISOString()}! Next invocation at: ${next_run}`
)
const headers = new Headers()
headers.set("x-sitemap-sync-secret", SITEMAP_SYNC_SECRET!)
try {
const url = new URL(
"/api/web/sitemap",
PUBLIC_URL || "https://www.scandichotels.com"
)
const response = await fetch(url, {
headers,
signal: AbortSignal.timeout(30_000),
})
if (!response.ok) {
const text = await response.text()
throw new Error(
`HTTP error syncing sitemap! status: ${response.status}, url: ${url.href}, message: ${text}`
)
}
} catch (error) {
console.error(`Error syncing sitemap: ${error}`)
return new Response("Failed to sync sitemap", { status: 500 })
}
console.log(`Sitemap synced initiated successfully`)
return new Response("Sitemap synced successfully", { status: 200 })
}
export const config: Config = {
schedule: "0 2/12 * * *", // every 12 hours, start at 02.00
}