From 4cfc368f964cb20e6d84d13cce883e20159d8716 Mon Sep 17 00:00:00 2001 From: Liam Date: Tue, 23 Apr 2024 16:37:33 +0100 Subject: [PATCH] cleanup and cache the paste response 5 mins --- src/app/(pages)/[id]/page.tsx | 13 ++++++++++--- src/app/(pages)/page.tsx | 2 ++ src/app/common/string-utils.ts | 9 --------- src/app/layout.tsx | 8 ++++---- 4 files changed, 16 insertions(+), 16 deletions(-) delete mode 100644 src/app/common/string-utils.ts diff --git a/src/app/(pages)/[id]/page.tsx b/src/app/(pages)/[id]/page.tsx index 6c2f11a..50ec02c 100644 --- a/src/app/(pages)/[id]/page.tsx +++ b/src/app/(pages)/[id]/page.tsx @@ -26,7 +26,7 @@ type Paste = { export async function generateMetadata({ params: { id }, }: PasteProps): Promise { - const data = await getData(id); + const data: Paste | undefined = await getData(id); if (data == undefined) { return { description: "Not found", @@ -40,7 +40,14 @@ export async function generateMetadata({ } async function getData(id: string): Promise { - const response = await fetch(`${process.env.NEXT_PUBLIC_API_ENDPOINT}/${id}`); + const response: Response = await fetch( + `${process.env.NEXT_PUBLIC_API_ENDPOINT}/${id}`, + { + next: { + revalidate: 300, // Keep this response cached for 5 minutes + }, + }, + ); const json = await response.json(); if (json.code && json.message) { @@ -52,7 +59,7 @@ async function getData(id: string): Promise { export default async function Paste({ params: { id }, }: PasteProps): Promise { - const data = await getData(id); + const data: Paste | undefined = await getData(id); if (data == undefined) { return notFound(); diff --git a/src/app/(pages)/page.tsx b/src/app/(pages)/page.tsx index 3d33fe5..a168121 100644 --- a/src/app/(pages)/page.tsx +++ b/src/app/(pages)/page.tsx @@ -16,6 +16,7 @@ export default function Home(): ReactElement { return; } + // Upload the paste to the server const response = await fetch( `${process.env.NEXT_PUBLIC_API_ENDPOINT}/upload`, { @@ -27,6 +28,7 @@ export default function Home(): ReactElement { }, ); + // Redirect to the new paste const data = await response.json(); window.location.href = `/${data.id}`; } diff --git a/src/app/common/string-utils.ts b/src/app/common/string-utils.ts deleted file mode 100644 index 32d0652..0000000 --- a/src/app/common/string-utils.ts +++ /dev/null @@ -1,9 +0,0 @@ -/** - * Capitalizes the first letter of a string - * - * @param str the string to change - * @returns the updated string - */ -export function capitalizeFirstLetter(str: string) { - return str.charAt(0).toUpperCase() + str.slice(1); -} diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 4852981..79a3b1a 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -1,18 +1,18 @@ +import "./globals.css"; import type { Metadata } from "next"; import { ThemeProvider } from "@/app/components/theme-provider"; - -import "./globals.css"; +import { ReactNode } from "react"; import { jetbrainsMono } from "@/app/common/font/font"; export const metadata: Metadata = { title: "Paste", - description: "Simple Pastebin", + description: "A simple Pastebin service", }; export default function RootLayout({ children, }: Readonly<{ - children: React.ReactNode; + children: ReactNode; }>) { return (