sitemap fix
This commit is contained in:
parent
da21fb8f86
commit
5c3a13bea9
2 changed files with 57 additions and 0 deletions
57
apps/infoalloggi/src/pages/sitemap.xml.ts
Normal file
57
apps/infoalloggi/src/pages/sitemap.xml.ts
Normal file
|
|
@ -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 `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
|
||||
${allUrls
|
||||
.map((url) => {
|
||||
return `
|
||||
<url>
|
||||
<loc>${url}</loc>
|
||||
<lastmod>${new Date().toISOString()}</lastmod>
|
||||
<changefreq>hourly</changefreq>
|
||||
<priority>0.7</priority>
|
||||
</url>
|
||||
`;
|
||||
})
|
||||
.join("")}
|
||||
</urlset>
|
||||
`;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue