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

chore(docs): fix sitemap #4198

Closed
wants to merge 23 commits into from
Closed
Changes from all commits
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
51 changes: 44 additions & 7 deletions packages/paste-website/src/pages/sitemap.xml.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,65 @@
import fs from "fs";
import path from "path";

import { globby } from "globby-esm";
import type { GetServerSideProps } from "next";
import { unstable_noStore as noStore } from "next/cache";

const Sitemap = (): React.ReactElement | null => {
return null;
};

export const revalidate = "force-cache";

const getRoutes = (): void => {
const pagesDirectory = path.join(process.cwd(), "src/pages");
const fileNames = fs.readdirSync(pagesDirectory);

const files = fileNames
.filter((fileName) => fileName.endsWith(".js") || fileName.endsWith(".jsx"))
.map((fileName) => {
return fileName === "index.js" ? "/" : `/${fileName.replace(/\.js(x)?$/, "")}`;
});

// eslint-disable-next-line no-console
console.log("ai generated", fileNames, files);
};

export const getServerSideProps: GetServerSideProps = async ({ res }) => {
noStore();
const BASE_URL = "https://paste.twilio.design";

getRoutes();
// Get a list of all pages currently in the site, must be mdx and not tsx which they all currently are
const uncompiledPaths = await globby(["**/pages/**/*.mdx", "!**/api/**", "!**/pages/404/**"]);
const uncompiledPaths = await globby(["**/*.mdx"], { cwd: process.cwd() });

// eslint-disable-next-line no-console
console.log(process.cwd(), __dirname);

const urlPaths = uncompiledPaths.map((path) => {
const urlPaths = uncompiledPaths.map((individualPath) => {
// Remove `src/pages/`
let modifiedPath = path.replace(/^src\/pages\//, "");
let modifiedPath = individualPath.replace(/^src\/pages\//, "");
// Remove `.mdx`
modifiedPath = modifiedPath.replace(/\.mdx$/, "");
// Remove `/index` if it's at the end of the path
modifiedPath = modifiedPath.replace(/\/index$/, "");
return `${BASE_URL}/${modifiedPath}`;
return `${BASE_URL}/${individualPath}`;
});

const paths = await globby(["**/*.mdx", "!sitemap.xml.js", "!404.js", "!_*.js"], {
cwd: process.cwd(),
});

// eslint-disable-next-line no-console
console.log("cached pages:", paths);

const staticPaths = paths.map((staticPagePath) => {
const otherPath = staticPagePath.replace(".js", "");
const route = otherPath === "index" ? "" : `${otherPath}/`;

return `${BASE_URL}/${route}`;
});

// eslint-disable-next-line no-console
console.log("staticPaths pages:", staticPaths);

const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
Expand Down
Loading