1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-04-27 14:56:21 -04:00

use next-sitemap server-side instead of manually generating the xml

This commit is contained in:
Jake Jarvis 2022-02-11 10:36:27 -05:00
parent 93356e9472
commit bb807e0e90
Signed by: jake
GPG Key ID: 2B0C9CF251E69A39
5 changed files with 45 additions and 57 deletions

View File

@ -51,6 +51,7 @@
"next-compose-plugins": "^2.2.1", "next-compose-plugins": "^2.2.1",
"next-mdx-remote": "4.0.0-rc.1", "next-mdx-remote": "4.0.0-rc.1",
"next-seo": "^5.1.0", "next-seo": "^5.1.0",
"next-sitemap": "^2.1.14",
"next-themes": "^0.0.15", "next-themes": "^0.0.15",
"next-transpile-modules": "^9.0.0", "next-transpile-modules": "^9.0.0",
"node-fetch": "^3.2.0", "node-fetch": "^3.2.0",

View File

@ -1,8 +1,6 @@
import { buildFeed } from "../lib/build-feed"; import { buildFeed } from "../lib/build-feed";
import type { GetServerSideProps } from "next"; import type { GetServerSideProps } from "next";
const AtomFeed = () => null;
export const getServerSideProps: GetServerSideProps = async (context) => { export const getServerSideProps: GetServerSideProps = async (context) => {
const feed = buildFeed(); const feed = buildFeed();
const { res } = context; const { res } = context;
@ -18,4 +16,5 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
}; };
}; };
export default AtomFeed; // eslint-disable-next-line import/no-anonymous-default-export
export default () => null;

View File

@ -1,8 +1,6 @@
import { buildFeed } from "../lib/build-feed"; import { buildFeed } from "../lib/build-feed";
import type { GetServerSideProps } from "next"; import type { GetServerSideProps } from "next";
const RssFeed = () => null;
export const getServerSideProps: GetServerSideProps = async (context) => { export const getServerSideProps: GetServerSideProps = async (context) => {
const feed = buildFeed(); const feed = buildFeed();
const { res } = context; const { res } = context;
@ -18,4 +16,5 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
}; };
}; };
export default RssFeed; // eslint-disable-next-line import/no-anonymous-default-export
export default () => null;

View File

@ -1,72 +1,48 @@
// WARNING: THIS FILE CONTAINS HISTORICAL LEVELS OF HACKINESS AND SHOULD NOT BE REPLICATED NOR ADMIRED. import { getServerSideSitemap } from "next-sitemap";
import { getAllNotes } from "../lib/parse-notes"; import { getAllNotes } from "../lib/parse-notes";
import { baseUrl } from "../lib/config"; import { baseUrl } from "../lib/config";
import type { GetServerSideProps } from "next"; import type { GetServerSideProps } from "next";
import type { ISitemapField } from "next-sitemap";
const Sitemap = () => null;
type Page = {
relUrl: string;
priority?: number;
changeFreq?: string;
lastMod?: string | Date;
};
export const getServerSideProps: GetServerSideProps = async (context) => { export const getServerSideProps: GetServerSideProps = async (context) => {
// TODO: make this not manual (serverless functions can't see /pages at runtime) // TODO: make this not manual (serverless functions can't see filesystem at runtime)
const pages: Page[] = [ const pages: ISitemapField[] = [
{ relUrl: "/", priority: 1.0, changeFreq: "weekly" }, // homepage { loc: "/", priority: 1.0, changefreq: "weekly" }, // homepage
{ relUrl: "/notes/", changeFreq: "weekly" }, { loc: "/notes/", changefreq: "weekly" },
{ relUrl: "/birthday/" }, { loc: "/birthday/" },
{ relUrl: "/cli/" }, { loc: "/cli/" },
{ relUrl: "/contact/" }, { loc: "/contact/" },
{ relUrl: "/hillary/" }, { loc: "/hillary/" },
{ relUrl: "/leo/" }, { loc: "/leo/" },
{ relUrl: "/license/", priority: 0.1, changeFreq: "yearly" }, { loc: "/license/", priority: 0.1, changefreq: "yearly" },
{ relUrl: "/previously/" }, { loc: "/previously/" },
{ relUrl: "/privacy/", priority: 0.1, changeFreq: "yearly" }, { loc: "/privacy/", priority: 0.1, changefreq: "yearly" },
{ relUrl: "/projects/", changeFreq: "daily" }, { loc: "/projects/", changefreq: "daily" },
{ relUrl: "/uses/" }, { loc: "/uses/" },
{ relUrl: "/y2k/" }, { loc: "/y2k/" },
]; ];
// push notes separately and use their metadata // push notes separately and use their metadata
const notes = getAllNotes(); const notes = getAllNotes();
notes.map((note) => notes.map((note) =>
pages.push({ pages.push({
relUrl: `/notes/${note.slug}/`, loc: `/notes/${note.slug}/`,
// pull lastMod from front matter date // pull lastMod from front matter date
lastMod: note.date, lastmod: note.date,
priority: 0.7,
}) })
); );
const xml = `<?xml version="1.0" encoding="UTF-8"?> // make all relative URLs absolute
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> pages.map((page) => (page.loc = `${baseUrl}${page.loc}`));
${pages
.map(
(page) => `
<url>
<loc>${baseUrl}${page.relUrl}</loc>
<priority>${page.priority ? page.priority.toFixed(1) : 0.7}</priority>
<changefreq>${page.changeFreq || "monthly"}</changefreq>
<lastmod>${page.lastMod || new Date().toISOString()}</lastmod>
</url>`
)
.join("")
.trim()}
</urlset>`;
const { res } = context;
res.setHeader("content-type", "application/xml; charset=utf-8");
// cache on edge for one hour // cache on edge for one hour
const { res } = context;
res.setHeader("cache-control", "s-maxage=3600, stale-while-revalidate"); res.setHeader("cache-control", "s-maxage=3600, stale-while-revalidate");
res.write(xml);
res.end();
return { // next-sitemap takes care of the rest of the response for us
props: {}, return getServerSideSitemap(context, pages);
};
}; };
export default Sitemap; // eslint-disable-next-line import/no-anonymous-default-export
export default () => null;

View File

@ -1030,6 +1030,11 @@
"@babel/helper-validator-identifier" "^7.16.7" "@babel/helper-validator-identifier" "^7.16.7"
to-fast-properties "^2.0.0" to-fast-properties "^2.0.0"
"@corex/deepmerge@^2.6.148":
version "2.6.148"
resolved "https://registry.yarnpkg.com/@corex/deepmerge/-/deepmerge-2.6.148.tgz#8fa825d53ffd1cbcafce1b6a830eefd3dcc09dd5"
integrity sha512-6QMz0/2h5C3ua51iAnXMPWFbb1QOU1UvSM4bKBw5mzdT+WtLgjbETBBIQZ+Sh9WvEcGwlAt/DEdRpIC3XlDBMA==
"@csstools/postcss-font-format-keywords@^1.0.0": "@csstools/postcss-font-format-keywords@^1.0.0":
version "1.0.0" version "1.0.0"
resolved "https://registry.yarnpkg.com/@csstools/postcss-font-format-keywords/-/postcss-font-format-keywords-1.0.0.tgz#7e7df948a83a0dfb7eb150a96e2390ac642356a1" resolved "https://registry.yarnpkg.com/@csstools/postcss-font-format-keywords/-/postcss-font-format-keywords-1.0.0.tgz#7e7df948a83a0dfb7eb150a96e2390ac642356a1"
@ -4843,6 +4848,14 @@ next-seo@^5.1.0:
resolved "https://registry.yarnpkg.com/next-seo/-/next-seo-5.1.0.tgz#aa9fd6249a11bf93e6da06fa2a6bc89268936edf" resolved "https://registry.yarnpkg.com/next-seo/-/next-seo-5.1.0.tgz#aa9fd6249a11bf93e6da06fa2a6bc89268936edf"
integrity sha512-ampuQfNTOi1x+xtRIb6CZGunIo6rQXtMo2Tyu861d5GjJFIwfOXsA4lzCa4+e2rLkyXDyVpavNNUZWa3US9ELw== integrity sha512-ampuQfNTOi1x+xtRIb6CZGunIo6rQXtMo2Tyu861d5GjJFIwfOXsA4lzCa4+e2rLkyXDyVpavNNUZWa3US9ELw==
next-sitemap@^2.1.14:
version "2.1.14"
resolved "https://registry.yarnpkg.com/next-sitemap/-/next-sitemap-2.1.14.tgz#d800a85c2d045dfdfe8982c8fdb3304087950515"
integrity sha512-7IqZjCGYSoA7csSc7B3m3m7hlFYd7Csg2PrNX97wH6/yJZ58nXiBR2JYQhHOZE3CzP3BAUKxwhiv2SlDiRzNXA==
dependencies:
"@corex/deepmerge" "^2.6.148"
minimist "^1.2.5"
next-themes@^0.0.15: next-themes@^0.0.15:
version "0.0.15" version "0.0.15"
resolved "https://registry.yarnpkg.com/next-themes/-/next-themes-0.0.15.tgz#ab0cee69cd763b77d41211f631e108beab39bf7d" resolved "https://registry.yarnpkg.com/next-themes/-/next-themes-0.0.15.tgz#ab0cee69cd763b77d41211f631e108beab39bf7d"