From bd2a678eed32d8503f75ee8ed509688cd5f6c9f2 Mon Sep 17 00:00:00 2001 From: Jake Laderman Date: Mon, 28 Oct 2024 16:28:02 -0400 Subject: [PATCH] update sitemap generation --- .eslintrc.js | 1 + .gitignore | 4 + next-sitemap.config.js | 10 +++ package.json | 4 +- pages/{about => kubernetes}/[keyword].tsx | 0 pages/sitemap.xml.ts | 92 ----------------------- yarn.lock | 34 ++++++++- 7 files changed, 51 insertions(+), 94 deletions(-) create mode 100644 next-sitemap.config.js rename pages/{about => kubernetes}/[keyword].tsx (100%) delete mode 100644 pages/sitemap.xml.ts diff --git a/.eslintrc.js b/.eslintrc.js index 7162f468..22a4e4f5 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -82,6 +82,7 @@ module.exports = { 'postcss.config.js', 'codegen.ts', 'index-pages.mjs', + 'next-sitemap.config.js', ], parserOptions: { project: null, diff --git a/.gitignore b/.gitignore index 815c235a..3c011011 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,7 @@ yarn-error.log* /build /dist .env.local + +# Sitemap +public/sitemap*.xml + diff --git a/next-sitemap.config.js b/next-sitemap.config.js new file mode 100644 index 00000000..63a4f966 --- /dev/null +++ b/next-sitemap.config.js @@ -0,0 +1,10 @@ +/** @type {import('next-sitemap').IConfig} */ +module.exports = { + siteUrl: 'https://plural.sh', + transform: async (_config, path) => ({ + loc: path, + changefreq: 'daily', + priority: 0.5, + lastmod: new Date().toISOString(), + }), +} diff --git a/package.json b/package.json index 55d93bee..0b84f334 100644 --- a/package.json +++ b/package.json @@ -7,8 +7,9 @@ "scripts": { "dev": "run-s generate:pageindex dev:watch", "dev:watch": "concurrently \"next dev\" \"yarn graphql:codegen:watch\"", - "build": "run-s generate:pageindex build:next", + "build": "run-s generate:pageindex build:next postbuild", "build:next": "next build", + "postbuild": "next-sitemap", "start": "next start", "lint": "run-p lint:format lint:js lint:css", "lint:format": "prettier --check --no-error-on-unmatched-pattern ./{src,pages}/**/*.{js,jsx,ts,tsx,graphql,md,mdpart}", @@ -61,6 +62,7 @@ "moment-timezone": "0.5.43", "next": "13.4.12", "next-compose-plugins": "2.2.1", + "next-sitemap": "4.2.3", "next-transpile-modules": "10.0.0", "octokit": "3.1.0", "query-string": "8.1.0", diff --git a/pages/about/[keyword].tsx b/pages/kubernetes/[keyword].tsx similarity index 100% rename from pages/about/[keyword].tsx rename to pages/kubernetes/[keyword].tsx diff --git a/pages/sitemap.xml.ts b/pages/sitemap.xml.ts deleted file mode 100644 index 7635de1e..00000000 --- a/pages/sitemap.xml.ts +++ /dev/null @@ -1,92 +0,0 @@ -import { until } from '@open-draft/until' - -import { appUrl, stackUrl } from '@src/consts/routes' -import { getRepos } from '@src/data/getRepos' -import { getStacks } from '@src/data/getStacks' - -import pages from '../src/generated/pages.json' - -const S_MAXAGE = 1 * 60 * 60 // 1s * 60s/m * 60m/h = 1 hour -const STALE_WHILE_REVALIDATE = S_MAXAGE * 2 - -function urlTag({ - location, - lastMod, - priority = '0.5', -}: { - location: string - lastMod: string - priority?: string | number -}) { - return ` - ${process.env.NEXT_PUBLIC_ROOT_URL}${location} - ${lastMod} - weekly - ${priority || '0.5'} - ` -} - -function wrapSiteMap(content: string) { - return ` - -${content} - -` -} - -export default function SiteMap() { - // getServerSideProps will do the heavy lifting -} - -function generateSiteMap({ - repos, - stacks, -}: { - repos?: Awaited> - stacks?: Awaited> -} = {}) { - const lastMod = new Date().toISOString() - - // We generate the XML sitemap with the posts data - const sitemap = wrapSiteMap( - `${pages - ?.map((page) => urlTag({ location: `${page.path}`, lastMod })) - .join('\n')} -${stacks - ?.map((stack) => urlTag({ location: stackUrl(stack.name), lastMod })) - .join('\n')} -${repos - ?.map((repo) => urlTag({ location: appUrl(repo.name), lastMod })) - .join('\n')}` - ) - - return sitemap -} - -let cachedSiteMap = generateSiteMap() - -export async function getServerSideProps({ res }) { - // We make an API call to gather the URLs for our site - const { data: repos, error: reposError } = await until(() => getRepos()) - const { data: stacks, error: stacksError } = await until(() => getStacks()) - - let sitemap: string = cachedSiteMap - - if (!reposError && !stacksError) { - sitemap = await generateSiteMap({ repos, stacks }) - cachedSiteMap = sitemap - } - - res.setHeader('Content-Type', 'text/xml') - res.setHeader( - 'Cache-Control', - `public, s-maxage=${S_MAXAGE}, stale-while-revalidate=${STALE_WHILE_REVALIDATE}` - ) - // we send the XML to the browser - res.write(sitemap) - res.end() - - return { - props: {}, - } -} diff --git a/yarn.lock b/yarn.lock index 2c19afb6..bcbd4c99 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2447,6 +2447,13 @@ __metadata: languageName: node linkType: hard +"@corex/deepmerge@npm:^4.0.43": + version: 4.0.43 + resolution: "@corex/deepmerge@npm:4.0.43" + checksum: f0103bfcfb9938ba478868fad522b018b92615ba186b0322c92aa1f57e539b87d4379d88fee3a404264a83bd4b13c384cfb07834377997d6d71cd840a880b4c4 + languageName: node + linkType: hard + "@csstools/css-parser-algorithms@npm:^2.3.0": version: 2.3.0 resolution: "@csstools/css-parser-algorithms@npm:2.3.0" @@ -3809,6 +3816,13 @@ __metadata: languageName: node linkType: hard +"@next/env@npm:^13.4.3": + version: 13.5.7 + resolution: "@next/env@npm:13.5.7" + checksum: 7703ba37a5ad9c280fb8a04af43231b41bec2ed0bcf182a7e9e147b12e9d710ef96bbdc8e67aa27d2cf1abd4b57951d1c12018ba6ff03fbd87f31876362f5f58 + languageName: node + linkType: hard + "@next/eslint-plugin-next@npm:14.1.0": version: 14.1.0 resolution: "@next/eslint-plugin-next@npm:14.1.0" @@ -12717,7 +12731,7 @@ __metadata: languageName: node linkType: hard -"minimist@npm:^1.2.0, minimist@npm:^1.2.6": +"minimist@npm:^1.2.0, minimist@npm:^1.2.6, minimist@npm:^1.2.8": version: 1.2.8 resolution: "minimist@npm:1.2.8" checksum: 75a6d645fb122dad29c06a7597bddea977258957ed88d7a6df59b5cd3fe4a527e253e9bbf2e783e4b73657f9098b96a5fe96ab8a113655d4109108577ecf85b0 @@ -12950,6 +12964,23 @@ __metadata: languageName: node linkType: hard +"next-sitemap@npm:4.2.3": + version: 4.2.3 + resolution: "next-sitemap@npm:4.2.3" + dependencies: + "@corex/deepmerge": ^4.0.43 + "@next/env": ^13.4.3 + fast-glob: ^3.2.12 + minimist: ^1.2.8 + peerDependencies: + next: "*" + bin: + next-sitemap: bin/next-sitemap.mjs + next-sitemap-cjs: bin/next-sitemap.cjs + checksum: 322cbbf11b6549b7bd5acda98f60a4ef6bb6627e6cd5b4a9367aa8950edf6bcc90f47f4da74a18ecdd823f52c0f625b559b4165dbd27f292c731b222e38fa105 + languageName: node + linkType: hard + "next-transpile-modules@npm:10.0.0": version: 10.0.0 resolution: "next-transpile-modules@npm:10.0.0" @@ -13801,6 +13832,7 @@ __metadata: moment-timezone: 0.5.43 next: 13.4.12 next-compose-plugins: 2.2.1 + next-sitemap: 4.2.3 next-transpile-modules: 10.0.0 npm-run-all: 4.1.5 octokit: 3.1.0