amirmoghi3.ir/pages/sitemap.xml.tsx

55 lines
1.2 KiB
TypeScript

import React from "react";
import type { NextApiResponse } from 'next'
const Sitemap = () => {
return null;
};
type Props = {
res: NextApiResponse
}
export const getServerSideProps = async ({ res }: Props) => {
const BASE_URL =
process.env.NEXT_PUBLIC_URL || "http://localhost:3000";
const staticPaths = [
{
url: `${BASE_URL}/`,
priority: 1.0,
},
{
url: `${BASE_URL}/resume`,
priority: 1.0,
},
].sort((a, b) => b.priority - a.priority)
const allPaths = [...staticPaths];
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${allPaths
.map(({ url, priority }) => {
return `
<url>
<loc>${url}</loc>
<lastmod>${new Date().toISOString()}</lastmod>
<changefreq>weekly</changefreq>
<priority>${priority}</priority>
</url>
`;
})
.join("")}
</urlset>`;
res.setHeader("Content-Type", "text/xml");
res.write(sitemap);
res.end();
return {
props: {},
};
};
export default Sitemap;