Files
web/apps/scandic-web/netlify/functions/sitemap.mts
Linus Flood e956e148bc Merged in fix/sitemap-api-route (pull request #1813)
Fix/sitemap - moved sitemap api route to api/web

* fix:sitemap - moved api route to api/web

* Moved files


Approved-by: Joakim Jäderberg
2025-04-16 06:52:55 +00:00

39 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,
})
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
}