From 2ecf1b21caf964678468db0811ff4d49944856f1 Mon Sep 17 00:00:00 2001 From: Max Leiter Date: Wed, 6 Apr 2022 10:41:42 -0700 Subject: [PATCH] client: remove server as build requirement This means public/unlisted posts and the home-page are no longer generated at build-time, so TTFB may be increased for the first user to load/access a page. Cache-Control headers are set so if the RP / server supports it the results should be cached for future users. --- client/pages/index.tsx | 12 ++++++-- client/pages/post/[id].tsx | 44 ++++++++++++------------------ server/src/lib/__tests__/config.ts | 1 - 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/client/pages/index.tsx b/client/pages/index.tsx index 8e8bdd4f..2ab906c6 100644 --- a/client/pages/index.tsx +++ b/client/pages/index.tsx @@ -2,11 +2,11 @@ import styles from '@styles/Home.module.css' import Header from '@components/header' import PageSeo from '@components/page-seo' import HomeComponent from '@components/home' -import { Page, Text, Spacer, Tabs, Textarea, Card } from '@geist-ui/core' +import { Page, Text } from '@geist-ui/core' +import { GetServerSideProps } from 'next' -export async function getStaticProps() { +export const getServerSideProps: GetServerSideProps = async ({ res }) => { try { - const resp = await fetch(process.env.API_URL + `/welcome`, { method: "GET", @@ -17,6 +17,12 @@ export async function getStaticProps() { }) const { title, content, rendered } = await resp.json() + + res.setHeader( + 'Cache-Control', + `public, s-maxage=${60 * 60 * 24 * 360}, max-age=${60 * 60 * 24 * 360}` + ) + return { props: { introContent: content || null, diff --git a/client/pages/post/[id].tsx b/client/pages/post/[id].tsx index dd3ced57..59df7c5b 100644 --- a/client/pages/post/[id].tsx +++ b/client/pages/post/[id].tsx @@ -1,4 +1,4 @@ -import type { GetStaticPaths, GetStaticProps } from "next"; +import type { GetServerSideProps, GetStaticPaths, GetStaticProps } from "next"; import type { Post } from "@lib/types"; import PostPage from "@components/post-page"; @@ -11,25 +11,7 @@ const PostView = ({ post }: PostProps) => { return } -export const getStaticPaths: GetStaticPaths = async () => { - const posts = await fetch(process.env.API_URL + `/posts/`, { - method: "GET", - headers: { - "Content-Type": "application/json", - "x-secret-key": process.env.SECRET_KEY || "", - } - }) - - const json = await posts.json() - const filtered = json.filter((post: Post) => post.visibility === "public" || post.visibility === "unlisted") - const paths = filtered.map((post: Post) => ({ - params: { id: post.id } - })) - - return { paths, fallback: 'blocking' } -} - -export const getStaticProps: GetStaticProps = async ({ params }) => { +export const getServerSideProps: GetServerSideProps = async ({ params, res }) => { const post = await fetch(process.env.API_URL + `/posts/${params?.id}`, { method: "GET", headers: { @@ -38,21 +20,29 @@ export const getStaticProps: GetStaticProps = async ({ params }) => { } }) - if (!post.ok) { + const sMaxAge = 60 * 60 * 24 + res.setHeader( + 'Cache-Control', + `public, s-maxage=${sMaxAge}, max-age=${sMaxAge}` + ) + + if (!post.ok || post.status !== 200) { return { redirect: { - destination: "/404", + destination: '/404', + permanent: false, }, - props: { - post: null - } + props: {} } } + + const json = await post.json(); + return { props: { - post: await post.json() - }, + post: json + } } } diff --git a/server/src/lib/__tests__/config.ts b/server/src/lib/__tests__/config.ts index 8a9aacac..a577891c 100644 --- a/server/src/lib/__tests__/config.ts +++ b/server/src/lib/__tests__/config.ts @@ -13,7 +13,6 @@ describe("Config", () => { expect(result).toHaveProperty("enable_admin") expect(result).toHaveProperty("secret_key") expect(result).toHaveProperty("registration_password") - expect(result).toHaveProperty("welcome_content") expect(result).toHaveProperty("welcome_title") })