Skip to content

Commit

Permalink
Build sitemap post build
Browse files Browse the repository at this point in the history
  • Loading branch information
behrends committed Apr 11, 2022
1 parent 387a846 commit 410262a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"scripts": {
"dev": "next dev",
"start": "next start",
"build": "next build"
"build": "next build",
"postbuild": "node ./scripts/generate-sitemap.mjs"
},
"dependencies": {
"@docsearch/react": "3",
Expand Down
39 changes: 11 additions & 28 deletions pages/sitemap.xml.js → scripts/generate-sitemap.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -37,42 +37,25 @@ function generateSiteMap() {
pages.push(...sections);
});

return `<?xml version="1.0" encoding="UTF-8"?>
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>${baseUrl}</loc>
</url>
<url>
<loc>${baseUrl}/impressum</loc>
</url>
${pages
.map((page) => {
return `
<url>
<loc>${baseUrl}/${page}</loc>
</url>
`;
})
.join('')}
${pages
.map((page) => {
return `
<url>
<loc>${baseUrl}/${page}</loc>
</url>`;
})
.join('')}
</urlset>
`;
fs.writeFileSync('public/sitemap.xml', sitemap);
}

function SiteMap() {
// getServerSideProps will do the heavy lifting
}

export async function getServerSideProps({ res }) {
const sitemap = generateSiteMap();

res.setHeader('Content-Type', 'text/xml');
// we send the XML to the browser
res.write(sitemap);
res.end();

return {
props: {},
};
}

export default SiteMap;
generateSiteMap();

0 comments on commit 410262a

Please sign in to comment.