Skip to content

Commit

Permalink
Use per-request caching
Browse files Browse the repository at this point in the history
  • Loading branch information
zoul committed Feb 16, 2023
1 parent 0a85115 commit 7c47788
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 41 deletions.
16 changes: 10 additions & 6 deletions app/[...path]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import PostCard from "shared/post-card";
import PressReleaseListing from "shared/press-releases";
import { Author } from "shared/author";
import { BlogPost, PostMetadata, stripBlogPostBody } from "shared/post";
import { siteData } from "shared/site-data";
import {
getAllAuthors,
getAllBlogPosts,
getAllPressReleases,
} from "shared/site-data";
import { markdownToHTML } from "shared/utils";
import { Metadata } from "next";

Expand All @@ -16,9 +20,9 @@ type Props = {

const Post = ({ params }: Props) => {
const post = postForPath(params.path);
const author = siteData.authors.find((a) => a.id === post.authorId)!;
const authors = siteData.authors;
const otherPosts = siteData.posts
const authors = getAllAuthors();
const author = authors.find((a) => a.id === post.authorId)!;
const otherPosts = getAllBlogPosts()
.filter((p) => p.path !== post.path)
.map(stripBlogPostBody)
.slice(0, 3);
Expand Down Expand Up @@ -90,12 +94,12 @@ const PostBody = ({ post, author }: { post: BlogPost; author: Author }) => {

const postForPath = (path: string[]) => {
const mergedPath = "/" + path.join("/");
const allPosts = [...siteData.posts, ...siteData.pressReleases];
const allPosts = [...getAllBlogPosts(), ...getAllPressReleases()];
return allPosts.find((post) => post.path === mergedPath)!;
};

export async function generateStaticParams(): Promise<Params[]> {
const allPosts = [...siteData.posts, ...siteData.pressReleases];
const allPosts = [...getAllBlogPosts(), ...getAllPressReleases()];
return allPosts.map((post) => ({
path: post.path.split("/").slice(1),
}));
Expand Down
6 changes: 3 additions & 3 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { PostMetadata } from "shared/post";
import { siteData } from "shared/site-data";
import { getAllAuthors, getAllBlogPosts } from "shared/site-data";
import PostCard from "shared/post-card";
import PressReleaseListing from "shared/press-releases";
import { Metadata } from "next";

const Home = () => {
const authors = siteData.authors;
const [firstPost, ...otherPosts] = siteData.posts;
const authors = getAllAuthors();
const [firstPost, ...otherPosts] = getAllBlogPosts();
const authorOf = (post: PostMetadata) =>
authors.find((a) => a.id === post.authorId)!;
return (
Expand Down
6 changes: 3 additions & 3 deletions pages/api/articles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { NextApiRequest, NextApiResponse } from "next";
import { BlogPost } from "shared/post";
import { siteData } from "shared/site-data";
import { getAllBlogPosts } from "shared/site-data";

interface PublicParams {
url: string;
Expand Down Expand Up @@ -30,7 +30,7 @@ export default async function handler(
request: NextApiRequest,
response: NextApiResponse
) {
const posts = siteData.posts.map(getPublicParamsForPost);
const posts = getAllBlogPosts().map(getPublicParamsForPost);
response.setHeader("Content-Type", "application/json");
response.status(200).send(JSON.stringify(posts, null, 2));
}
6 changes: 3 additions & 3 deletions pages/api/feed.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { NextApiRequest, NextApiResponse } from "next";
import { Feed } from "feed";
import { siteData } from "shared/site-data";
import { getAllBlogPosts } from "shared/site-data";
import { feedItemFromBlogPost } from "shared/utils";

export default async function handler(
Expand All @@ -16,7 +16,7 @@ export default async function handler(
image: "https://data.cesko.digital/img/172a1526.png",
copyright: "Česko.Digital a přispěvatelé",
});
siteData.posts.map(feedItemFromBlogPost).forEach(feed.addItem);
getAllBlogPosts().map(feedItemFromBlogPost).forEach(feed.addItem);
response.setHeader(
"Content-Type",
request.query.plain ? "text/plain" : "application/rss+xml"
Expand Down
4 changes: 2 additions & 2 deletions shared/press-releases.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { siteData } from "shared/site-data";
import { getAllPressReleases } from "shared/site-data";
import Link from "next/link";

const PressReleaseListing = () => {
const posts = siteData.pressReleases.slice(0, 6);
const posts = getAllPressReleases().slice(0, 6);
const formatDate = (stamp: string) =>
new Date(stamp).toLocaleDateString("cs-CZ", { dateStyle: "medium" });
return (
Expand Down
38 changes: 14 additions & 24 deletions shared/site-data.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
import { Author, getAllAuthors } from "./author";
import { BlogPost, compareBlogPostsByDate } from "./post";
import { getAllAuthors as getAllAuthorsFromFile } from "./author";
import { compareBlogPostsByDate } from "./post";
import { getAllPosts } from "./post-loading";
import { join, resolve } from "path";
import { cache } from "react";

export interface SiteData {
posts: readonly BlogPost[];
pressReleases: readonly BlogPost[];
authors: readonly Author[];
}
const contentRoot = resolve(process.cwd(), "content");

function loadSiteData(): SiteData {
const content = resolve(process.cwd(), "content");
console.log(`Loading data files from ${content}`);
const byDate = compareBlogPostsByDate;
const posts = getAllPosts(join(content, "posts")).sort(byDate);
const pressReleases = getAllPosts(join(content, "press")).sort(byDate);
const authors = getAllAuthors(join(content, "authors.yaml"));
console.log(
`Loaded ${posts.length} posts, ${pressReleases.length} press releases, ${authors.length} authors.`
);
return {
posts,
pressReleases,
authors,
};
}
export const getAllBlogPosts = cache(() =>
getAllPosts(join(contentRoot, "posts")).sort(compareBlogPostsByDate)
);

export const siteData = loadSiteData();
export const getAllPressReleases = cache(() =>
getAllPosts(join(contentRoot, "press")).sort(compareBlogPostsByDate)
);

export const getAllAuthors = cache(() =>
getAllAuthorsFromFile(join(contentRoot, "authors.yaml"))
);

0 comments on commit 7c47788

Please sign in to comment.