Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static sitemap generator ts #10

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions generateSitemap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const fs = require("fs");
const globby = require("globby");
const path = require("path");

const {
getAllPostsWithSlug,
getAllProductCategoriesWithSlug,
getAllProductsWithSlug,
} = require("./lib"); // this doesn't work because typescript and/or es6 modules MODULE_NOT_FOUND

// const getAllPostsWithSlug = require("./lib/news");

const lambdaPagesFilePath = path.join(__dirname, ".", "lib", "sitemap.xml");

const createSitemap = (routes, host) => `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${routes
.map((route) => `<url><loc>https://cascinarampina.it${route}</loc></url>`)
.join("")}
</urlset>`;

(async () => {
const globbs = await globby([
"pages/**/*.tsx",
"!pages/api",
"!pages/**/\\[*\\].tsx",
"!pages/**/*.xml.tsx",
]);
const pages = globbs
.map((route) =>
route.replace("pages", "").replace(".tsx", "").replace("/index", "")
)
.sort();

const news = (await getAllPostsWithSlug()).edges.map(
nkint marked this conversation as resolved.
Show resolved Hide resolved
({ node }) => `/news/${node.slug}`
);

const productCategories = (await getAllProductCategoriesWithSlug()).edges.map(
({ node }) => `/categorie-prodotti/${node.slug}`
);

const products = (await getAllProductsWithSlug()).edges.map(
({ node }) => `/prodotti/${node.slug}`
);

const sitemap = createSitemap(
[...pages, ...products, ...productCategories, ...news],
req.headers.host
);

fs.writeFileSync(lambdaPagesFilePath, JSON.stringify(pages, null, 2), {
encoding: "utf-8",
});
console.log(`Built ${lambdaPagesFilePath}`);

process.exit(0);
})();