1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-10-28 20:35:49 -04:00

fix sitemap.xml timeouts

This commit is contained in:
2022-07-06 03:32:55 -04:00
parent 0f38ab4f0a
commit 8d47958473
5 changed files with 32 additions and 35 deletions

View File

@@ -1,12 +1,20 @@
import { SitemapStream, streamToPromise, SitemapItemLoose, EnumChangefreq } from "sitemap";
import { SitemapStream, SitemapItemLoose, EnumChangefreq } from "sitemap";
import { getAllNotes } from "../lib/helpers/parse-notes";
import { baseUrl } from "../lib/config";
import { RELEASE_DATE } from "../lib/config/constants";
import type { GetServerSideProps } from "next";
export const getServerSideProps: GetServerSideProps<Record<string, never>> = async (context) => {
const { res } = context;
const stream = new SitemapStream({ hostname: baseUrl });
// cache on edge for 12 hours
res.setHeader("cache-control", "public, max-age=0, s-maxage=43200, stale-while-revalidate");
res.setHeader("content-type", "application/xml; charset=utf-8");
// related: https://github.com/vercel/next.js/discussions/15453
stream.pipe(res);
// TODO: make this not manual (serverless functions can't see filesystem at runtime)
const pages: SitemapItemLoose[] = [
{
@@ -32,13 +40,13 @@ export const getServerSideProps: GetServerSideProps<Record<string, never>> = asy
// push notes separately and use their metadata
const notes = await getAllNotes();
notes.forEach((note) =>
notes.forEach((note) => {
pages.push({
url: `/notes/${note.slug}/`,
// pull lastMod from front matter date
lastmod: new Date(note.date).toISOString(),
})
);
});
});
// set lastmod of /notes/ page to most recent post's date
pages.push({
@@ -53,17 +61,9 @@ export const getServerSideProps: GetServerSideProps<Record<string, never>> = asy
pages.forEach((page) => {
stream.write(page);
});
stream.end();
// cache on edge for 12 hours
const { res } = context;
res.setHeader("cache-control", "public, max-age=0, s-maxage=43200, stale-while-revalidate");
res.setHeader("content-type", "application/xml; charset=utf-8");
// finally write the resulting XML
res.write(await streamToPromise(stream));
res.end();
return {
props: {},
};