Skip to content

Commit

Permalink
sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
PixeledCode committed Jan 20, 2022
1 parent 2e1e948 commit b3be66b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ module.exports = {
"@typescript-eslint/explicit-module-boundary-types": "off",
"jsx-a11y/no-onchange": "off",
"jsx-a11y/anchor-is-valid": "off",
"@typescript-eslint/no-empty-function": "off",
},
settings: {
react: {
Expand Down
58 changes: 58 additions & 0 deletions pages/sitemap.xml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import fs from "fs";

const Sitemap = () => {};

export const getServerSideProps = async function ({ res }) {
const dataRes = await fetch(
"https://justicehub.in/api/3/action/package_search?fq=groups:budgets-for-justice&rows=200"
).then((re) => re.json());
const allData = dataRes.result.results;

const staticPages = fs
.readdirSync("pages")
.filter((staticPage) => {
return ![
"_app.tsx",
"_document.tsx",
"_error.tsx",
"sitemap.xml.js",
"explore",
"index.tsx",
].includes(staticPage);
})
.map((staticPagePath) => {
return `https://budgets.justicehub.in/${staticPagePath}`;
});

allData.forEach((scheme) =>
staticPages.push(`https://budgets.justicehub.in/datasets/${scheme.name}`)
);
staticPages.unshift(`https://budgets.justicehub.in/`);

const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${staticPages
.map((url) => {
return `
<url>
<loc>${url}</loc>
<lastmod>${new Date().toISOString()}</lastmod>
<changefreq>monthly</changefreq>
<priority>1.0</priority>
</url>
`;
})
.join("")}
</urlset>
`;

res.setHeader("Content-Type", "text/xml");
res.write(sitemap);
res.end();

return {
props: {},
};
};

export default Sitemap;

0 comments on commit b3be66b

Please sign in to comment.