1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-04-26 15:08:27 -04:00

update server-side sitemap.xml code

This commit is contained in:
Jake Jarvis 2022-05-07 20:47:14 -04:00
parent d8363d131a
commit 00d497b69b
Signed by: jake
GPG Key ID: 2B0C9CF251E69A39
7 changed files with 79 additions and 61 deletions

View File

@ -10,10 +10,6 @@ const Wrapper = styled("header", {
borderBottom: "1px solid $kindaLight", borderBottom: "1px solid $kindaLight",
backgroundColor: "$backgroundHeader", backgroundColor: "$backgroundHeader",
// stick to the top of the page when scrolling
position: "sticky",
top: 0,
// blurry glass-like background effect (except on firefox...?) // blurry glass-like background effect (except on firefox...?)
backdropFilter: "saturate(180%) blur(5px)", backdropFilter: "saturate(180%) blur(5px)",
zIndex: 9999, zIndex: 9999,

View File

@ -23,6 +23,12 @@ const Container = styled("div", {
display: "block", display: "block",
}); });
// stick header to the top of the page when scrolling
const StickyHeader = styled(Header, {
position: "sticky",
top: 0,
});
// footer needs to fill the remaining vertical screen space. doing it here to keep flex stuff together. // footer needs to fill the remaining vertical screen space. doing it here to keep flex stuff together.
const FlexedFooter = styled(Footer, { const FlexedFooter = styled(Footer, {
flex: 1, flex: 1,
@ -43,7 +49,7 @@ const Layout = ({ container = true, children, ...rest }: LayoutProps) => {
</Head> </Head>
<Flex {...rest}> <Flex {...rest}>
<Header /> <StickyHeader />
{/* passing `container={false}` to Layout allows 100% control of the content area on a per-page basis */} {/* passing `container={false}` to Layout allows 100% control of the content area on a per-page basis */}
{container ? ( {container ? (

View File

@ -9,14 +9,12 @@ export const clientScript = () => {
var light = "light"; var light = "light";
var dark = "dark"; var dark = "dark";
var newTheme; var newTheme;
// user's saved preference
var pref = window.localStorage.getItem("__STORAGE_KEY__");
// map of theme -> classname
var classNames = "__CLASS_NAMES__";
// the list of <html>'s current class(es)... // the list of <html>'s current class(es)...
var classList = document.documentElement.classList; var classList = document.documentElement.classList;
// ...from which `classNames` are removed to start fresh // map of theme -> classname
classList.remove(classNames[light], classNames[dark]); var classNames = "__CLASS_NAMES__";
// user's saved preference
var pref = window.localStorage.getItem("__STORAGE_KEY__");
if (pref === light || pref === dark) { if (pref === light || pref === dark) {
// simply restore the local storage preference // simply restore the local storage preference
@ -27,7 +25,9 @@ export const clientScript = () => {
newTheme = window.matchMedia("__MEDIA_QUERY__").matches ? dark : light; newTheme = window.matchMedia("__MEDIA_QUERY__").matches ? dark : light;
} }
// FINALLY set the root class // remove both `classNames` to start fresh...
classList.remove(classNames[light], classNames[dark]);
// ...and then FINALLY set the root class
classList.add(classNames[newTheme]); classList.add(classNames[newTheme]);
} catch (error) {} } catch (error) {}
}; };

View File

@ -13,4 +13,4 @@ export const themeColors = {
}; };
// local storage key // local storage key
export const themeStorageKey = "preferred-theme"; export const themeStorageKey = "theme";

View File

@ -46,7 +46,6 @@
"next-compose-plugins": "^2.2.1", "next-compose-plugins": "^2.2.1",
"next-mdx-remote": "^4.0.3", "next-mdx-remote": "^4.0.3",
"next-seo": "^5.4.0", "next-seo": "^5.4.0",
"next-sitemap": "^2.5.20",
"next-transpile-modules": "^9.0.0", "next-transpile-modules": "^9.0.0",
"node-fetch": "^3.2.4", "node-fetch": "^3.2.4",
"p-map": "^5.3.0", "p-map": "^5.3.0",
@ -60,7 +59,7 @@
"react-innertext": "^1.1.5", "react-innertext": "^1.1.5",
"react-intersection-observer": "^9.1.0", "react-intersection-observer": "^9.1.0",
"react-is": "18.1.0", "react-is": "18.1.0",
"react-player": "^2.10.0", "react-player": "^2.10.1",
"react-textarea-autosize": "^8.3.3", "react-textarea-autosize": "^8.3.3",
"react-twitter-embed": "^4.0.4", "react-twitter-embed": "^4.0.4",
"react-use": "^17.3.2", "react-use": "^17.3.2",
@ -71,6 +70,7 @@
"remove-markdown": "^0.5.0", "remove-markdown": "^0.5.0",
"sanitize-html": "^2.7.0", "sanitize-html": "^2.7.0",
"simple-icons": "^6.20.0", "simple-icons": "^6.20.0",
"sitemap": "^7.1.1",
"stitches-normalize": "^2.0.0", "stitches-normalize": "^2.0.0",
"swr": "^1.3.0", "swr": "^1.3.0",
"url-join": "^5.0.0" "url-join": "^5.0.0"

View File

@ -1,56 +1,63 @@
import { getServerSideSitemap } from "next-sitemap"; import { SitemapStream, streamToPromise, SitemapItemLoose, EnumChangefreq } from "sitemap";
import urlJoin from "url-join";
import { getAllNotes } from "../lib/helpers/parse-notes"; import { getAllNotes } from "../lib/helpers/parse-notes";
import { baseUrl } from "../lib/config"; import { baseUrl } from "../lib/config";
import { RELEASE_DATE } from "../lib/config/constants"; import { RELEASE_DATE } from "../lib/config/constants";
import type { GetServerSideProps } from "next"; import type { GetServerSideProps } from "next";
import type { ISitemapField } from "next-sitemap";
export const getServerSideProps: GetServerSideProps = async (context) => { export const getServerSideProps: GetServerSideProps = async (context) => {
const stream = new SitemapStream({ hostname: baseUrl });
// TODO: make this not manual (serverless functions can't see filesystem at runtime) // TODO: make this not manual (serverless functions can't see filesystem at runtime)
const pages: ISitemapField[] = [ const pages: SitemapItemLoose[] = [
{ {
// homepage // homepage
loc: "/", url: "/",
priority: 1.0, priority: 1.0,
changefreq: "weekly", changefreq: EnumChangefreq.WEEKLY,
lastmod: RELEASE_DATE, lastmod: RELEASE_DATE,
}, },
{ loc: "/notes/", changefreq: "weekly", lastmod: RELEASE_DATE }, { url: "/notes/", changefreq: EnumChangefreq.WEEKLY, lastmod: RELEASE_DATE },
{ loc: "/birthday/" }, { url: "/birthday/" },
{ loc: "/cli/" }, { url: "/cli/" },
{ loc: "/contact/" }, { url: "/contact/" },
{ loc: "/hillary/" }, { url: "/hillary/" },
{ loc: "/leo/" }, { url: "/leo/" },
{ loc: "/license/", priority: 0.1, changefreq: "yearly" }, { url: "/license/", priority: 0.1, changefreq: EnumChangefreq.YEARLY },
{ loc: "/previously/" }, { url: "/previously/" },
{ loc: "/privacy/", priority: 0.1, changefreq: "yearly" }, { url: "/privacy/", priority: 0.1, changefreq: EnumChangefreq.YEARLY },
{ loc: "/projects/", changefreq: "daily" }, { url: "/projects/", changefreq: EnumChangefreq.DAILY },
{ loc: "/stats/", priority: 0.1, changefreq: "yearly" }, { url: "/stats/", priority: 0.1, changefreq: EnumChangefreq.YEARLY },
{ loc: "/uses/" }, { url: "/uses/" },
{ loc: "/y2k/" }, { url: "/y2k/" },
]; ];
// push notes separately and use their metadata // push notes separately and use their metadata
const notes = await getAllNotes(); const notes = await getAllNotes();
notes.forEach((note) => notes.forEach((note) =>
pages.push({ pages.push({
loc: urlJoin("/notes/", note.slug, "/"), url: `/notes/${note.slug}/`,
// pull lastMod from front matter date // pull lastMod from front matter date
lastmod: new Date(note.date).toISOString(), lastmod: new Date(note.date).toISOString(),
priority: 0.7,
}) })
); );
// make all relative URLs absolute // translate array of all pages to sitemap's stream
pages.forEach((page) => (page.loc = urlJoin(baseUrl, page.loc))); pages.forEach((page) => {
stream.write(page);
});
stream.end();
// cache on edge for 12 hours // cache on edge for 12 hours
const { res } = context; const { res } = context;
res.setHeader("cache-control", "s-maxage=43200, stale-while-revalidate=3600"); res.setHeader("cache-control", "s-maxage=43200, stale-while-revalidate=3600");
// next-sitemap takes care of the rest of the response for us // finally write the resulting XML
return getServerSideSitemap(context, pages); res.write(await streamToPromise(stream));
res.end();
return {
props: {},
};
}; };
// eslint-disable-next-line import/no-anonymous-default-export // eslint-disable-next-line import/no-anonymous-default-export

View File

@ -1023,11 +1023,6 @@
"@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==
"@eslint/eslintrc@^1.2.3": "@eslint/eslintrc@^1.2.3":
version "1.2.3" version "1.2.3"
resolved "https://registry.yarnpkg.com/@eslint/eslintrc/-/eslintrc-1.2.3.tgz#fcaa2bcef39e13d6e9e7f6271f4cc7cae1174886" resolved "https://registry.yarnpkg.com/@eslint/eslintrc/-/eslintrc-1.2.3.tgz#fcaa2bcef39e13d6e9e7f6271f4cc7cae1174886"
@ -1647,7 +1642,7 @@
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197" resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197"
integrity sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA== integrity sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA==
"@types/node@*": "@types/node@*", "@types/node@^17.0.5":
version "17.0.31" version "17.0.31"
resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.31.tgz#a5bb84ecfa27eec5e1c802c6bbf8139bdb163a5d" resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.31.tgz#a5bb84ecfa27eec5e1c802c6bbf8139bdb163a5d"
integrity sha512-AR0x5HbXGqkEx9CadRH3EBYx/VkiUgZIhP4wvPn/+5KIsgpNoyFaRlVe0Zlx9gRtg8fA06a9tskE2MSN7TcG4Q== integrity sha512-AR0x5HbXGqkEx9CadRH3EBYx/VkiUgZIhP4wvPn/+5KIsgpNoyFaRlVe0Zlx9gRtg8fA06a9tskE2MSN7TcG4Q==
@ -1712,6 +1707,13 @@
dependencies: dependencies:
htmlparser2 "^6.0.0" htmlparser2 "^6.0.0"
"@types/sax@^1.2.1":
version "1.2.4"
resolved "https://registry.yarnpkg.com/@types/sax/-/sax-1.2.4.tgz#8221affa7f4f3cb21abd22f244cfabfa63e6a69e"
integrity sha512-pSAff4IAxJjfAXUG6tFkO7dsSbTmf8CtUpfhhZ5VhkRpC4628tJhh3+V6H1E+/Gs9piSzYKT5yzHO5M4GG9jkw==
dependencies:
"@types/node" "*"
"@types/scheduler@*": "@types/scheduler@*":
version "0.16.2" version "0.16.2"
resolved "https://registry.yarnpkg.com/@types/scheduler/-/scheduler-0.16.2.tgz#1a62f89525723dde24ba1b01b092bf5df8ad4d39" resolved "https://registry.yarnpkg.com/@types/scheduler/-/scheduler-0.16.2.tgz#1a62f89525723dde24ba1b01b092bf5df8ad4d39"
@ -1910,6 +1912,11 @@ ansi-styles@^6.0.0:
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-6.1.0.tgz#87313c102b8118abd57371afab34618bf7350ed3" resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-6.1.0.tgz#87313c102b8118abd57371afab34618bf7350ed3"
integrity sha512-VbqNsoz55SYGczauuup0MFUyXNQviSpFTj1RQtFzmQLk18qbVSpTFFGMT293rmDaQuKCT6InmbuEyUne4mTuxQ== integrity sha512-VbqNsoz55SYGczauuup0MFUyXNQviSpFTj1RQtFzmQLk18qbVSpTFFGMT293rmDaQuKCT6InmbuEyUne4mTuxQ==
arg@^5.0.0:
version "5.0.1"
resolved "https://registry.yarnpkg.com/arg/-/arg-5.0.1.tgz#eb0c9a8f77786cad2af8ff2b862899842d7b6adb"
integrity sha512-e0hDa9H2Z9AwFkk2qDlwhoMYE4eToKarchkQHovNdLTCYMHZHeRjI71crOh+dio4K6u1IcwubQqo79Ga4CyAQA==
argparse@^1.0.7: argparse@^1.0.7:
version "1.0.10" version "1.0.10"
resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911" resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911"
@ -2559,9 +2566,9 @@ eastasianwidth@^0.2.0:
integrity sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA== integrity sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==
electron-to-chromium@^1.4.118: electron-to-chromium@^1.4.118:
version "1.4.136" version "1.4.137"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.136.tgz#b6a3595a9c29d6d8f60e092d40ac24f997e4e7ef" resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.137.tgz#186180a45617283f1c012284458510cd99d6787f"
integrity sha512-GnITX8rHnUrIVnTxU9UlsTnSemHUA2iF+6QrRqxFbp/mf0vfuSc/goEyyQhUX3TUUCE3mv/4BNuXOtaJ4ur0eA== integrity sha512-0Rcpald12O11BUogJagX3HsCN3FE83DSqWjgXoHo5a72KUKMSfI39XBgJpgNNxS9fuGzytaFjE06kZkiVFy2qA==
emoji-regex@^8.0.0: emoji-regex@^8.0.0:
version "8.0.0" version "8.0.0"
@ -4705,14 +4712,6 @@ next-seo@^5.4.0:
resolved "https://registry.yarnpkg.com/next-seo/-/next-seo-5.4.0.tgz#37a7784b30b3f70cec3fa0d77f9dde5990822d24" resolved "https://registry.yarnpkg.com/next-seo/-/next-seo-5.4.0.tgz#37a7784b30b3f70cec3fa0d77f9dde5990822d24"
integrity sha512-R9DhajPwJnR/lsF2hZ8cN8uqr5CVITsRrCG1AF5+ufcaybKYOvnH8sH9MaH4/hpkps3PQ9H71S7J7SPYixAYzQ== integrity sha512-R9DhajPwJnR/lsF2hZ8cN8uqr5CVITsRrCG1AF5+ufcaybKYOvnH8sH9MaH4/hpkps3PQ9H71S7J7SPYixAYzQ==
next-sitemap@^2.5.20:
version "2.5.20"
resolved "https://registry.yarnpkg.com/next-sitemap/-/next-sitemap-2.5.20.tgz#7124e138506c2f10e460c3dc77907baf4d134478"
integrity sha512-Qbm4N2WGA6VHemFN0C9PNWQav9RKwMEbVrNgVvDQhG0sDBmNBgKii54WklOjCvVJVHgQPgtXLBhlNaJGS+RVQA==
dependencies:
"@corex/deepmerge" "^2.6.148"
minimist "^1.2.6"
next-transpile-modules@^9.0.0: next-transpile-modules@^9.0.0:
version "9.0.0" version "9.0.0"
resolved "https://registry.yarnpkg.com/next-transpile-modules/-/next-transpile-modules-9.0.0.tgz#133b1742af082e61cc76b02a0f12ffd40ce2bf90" resolved "https://registry.yarnpkg.com/next-transpile-modules/-/next-transpile-modules-9.0.0.tgz#133b1742af082e61cc76b02a0f12ffd40ce2bf90"
@ -5156,10 +5155,10 @@ react-is@^16.13.1, react-is@^16.7.0:
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4" resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ== integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==
react-player@^2.10.0: react-player@^2.10.1:
version "2.10.0" version "2.10.1"
resolved "https://registry.yarnpkg.com/react-player/-/react-player-2.10.0.tgz#bd3e0f10ae756cc644efb14177a515d455d97e20" resolved "https://registry.yarnpkg.com/react-player/-/react-player-2.10.1.tgz#f2ee3ec31393d7042f727737545414b951ffc7e4"
integrity sha512-PccIqea9nxSHAdai6R+Yj9lp6tb2lyXWbaF6YVHi5uO4FiXYMKKr9rMXJrivwV5vXwQa65rYKBmwebsBmRTT3w== integrity sha512-ova0jY1Y1lqLYxOehkzbNEju4rFXYVkr5rdGD71nsiG4UKPzRXQPTd3xjoDssheoMNjZ51mjT5ysTrdQ2tEvsg==
dependencies: dependencies:
deepmerge "^4.0.0" deepmerge "^4.0.0"
load-script "^1.0.0" load-script "^1.0.0"
@ -5635,6 +5634,16 @@ sirv@^1.0.7:
mrmime "^1.0.0" mrmime "^1.0.0"
totalist "^1.0.0" totalist "^1.0.0"
sitemap@^7.1.1:
version "7.1.1"
resolved "https://registry.yarnpkg.com/sitemap/-/sitemap-7.1.1.tgz#eeed9ad6d95499161a3eadc60f8c6dce4bea2bef"
integrity sha512-mK3aFtjz4VdJN0igpIJrinf3EO8U8mxOPsTBzSsy06UtjZQJ3YY3o3Xa7zSc5nMqcMrRwlChHZ18Kxg0caiPBg==
dependencies:
"@types/node" "^17.0.5"
"@types/sax" "^1.2.1"
arg "^5.0.0"
sax "^1.2.4"
slash@^3.0.0: slash@^3.0.0:
version "3.0.0" version "3.0.0"
resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634" resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634"