diff --git a/apps/scandic-web/netlify/functions/sitemap-manual.mts b/apps/scandic-web/netlify/functions/sitemap-manual.mts new file mode 100644 index 000000000..2b2132c65 --- /dev/null +++ b/apps/scandic-web/netlify/functions/sitemap-manual.mts @@ -0,0 +1,33 @@ +/* eslint-disable import/no-anonymous-default-export */ +import type { Context } from "@netlify/functions" + +export default async (request: Request, _context: Context) => { + const SITEMAP_SYNC_SECRET = request.headers.get("x-sitemap-sync-secret") + const PUBLIC_URL = Netlify.env.get("PUBLIC_URL") + console.info(`Started sitemap sync at: ${new Date().toISOString()}!`) + const headers = new Headers() + headers.set("x-sitemap-sync-secret", SITEMAP_SYNC_SECRET!) + + try { + const url = new URL( + "/api/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}` + ) + } + const data = await response.json() + console.log(`Sitemap data: ${JSON.stringify(data)}`) + } 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 }) +} diff --git a/apps/scandic-web/netlify/functions/sitemap.mts b/apps/scandic-web/netlify/functions/sitemap.mts index 01fa13e56..05403b21f 100644 --- a/apps/scandic-web/netlify/functions/sitemap.mts +++ b/apps/scandic-web/netlify/functions/sitemap.mts @@ -12,14 +12,24 @@ export default async (request: Request, _context: Context) => { headers.set("x-sitemap-sync-secret", SITEMAP_SYNC_SECRET!) try { - await fetch(`${PUBLIC_URL}/api/sitemap`, { + const url = new URL( + "/api/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 }) }