From 5c3a13bea9d5487b3da20fa9d3ff97ac2e07b04e Mon Sep 17 00:00:00 2001 From: Marco Pedone Date: Mon, 6 Oct 2025 19:23:28 +0200 Subject: [PATCH] sitemap fix --- .../public/{sitemap.xml => bkp.sitemap.xml} | 0 apps/infoalloggi/src/pages/sitemap.xml.ts | 57 +++++++++++++++++++ 2 files changed, 57 insertions(+) rename apps/infoalloggi/public/{sitemap.xml => bkp.sitemap.xml} (100%) create mode 100644 apps/infoalloggi/src/pages/sitemap.xml.ts diff --git a/apps/infoalloggi/public/sitemap.xml b/apps/infoalloggi/public/bkp.sitemap.xml similarity index 100% rename from apps/infoalloggi/public/sitemap.xml rename to apps/infoalloggi/public/bkp.sitemap.xml diff --git a/apps/infoalloggi/src/pages/sitemap.xml.ts b/apps/infoalloggi/src/pages/sitemap.xml.ts new file mode 100644 index 0000000..5520edb --- /dev/null +++ b/apps/infoalloggi/src/pages/sitemap.xml.ts @@ -0,0 +1,57 @@ +import type { GetServerSideProps } from "next"; +import { env } from "~/env.mjs"; +import { generateSSGHelper } from "~/server/utils/ssgHelper"; + +async function generateSiteMap() { + const helper = generateSSGHelper(); + const dynamicUrls = await helper.annunci.getCodici.fetch(); + const staticUrls = [ + env.NEXT_PUBLIC_BASE_URL, + `${env.NEXT_PUBLIC_BASE_URL}/contatti`, + `${env.NEXT_PUBLIC_BASE_URL}/prezzi`, + `${env.NEXT_PUBLIC_BASE_URL}/chi-siamo`, + `${env.NEXT_PUBLIC_BASE_URL}/proprietari`, + `${env.NEXT_PUBLIC_BASE_URL}/guida`, + `${env.NEXT_PUBLIC_BASE_URL}/annunci`, + `${env.NEXT_PUBLIC_BASE_URL}/annunci?tipo=Transitorio`, + `${env.NEXT_PUBLIC_BASE_URL}/annunci?comune=Bassano+del+Grappa`, + `${env.NEXT_PUBLIC_BASE_URL}/annunci?tipo=Stabile`, + `${env.NEXT_PUBLIC_BASE_URL}/annunci?comune=Marostica`, + ]; + + const allUrls = [...staticUrls, ...dynamicUrls]; + + return ` + + ${allUrls + .map((url) => { + return ` + + ${url} + ${new Date().toISOString()} + hourly + 0.7 + + `; + }) + .join("")} + + `; +} + +export const getServerSideProps = (async (context) => { + const sitemap = await generateSiteMap(); + context.res.setHeader("Content-Type", "text/xml"); + context.res.write(sitemap); + context.res.end(); + + return { + props: {}, + }; +}) satisfies GetServerSideProps; + +// You must export a React component as the default export, but it will not be rendered. +// Its sole purpose is to allow Next.js to compile the file as a Page. +export default function Sitemap() { + return null; +}