cleanup and cache the paste response 5 mins
This commit is contained in:
parent
29c483aeb1
commit
4cfc368f96
@ -26,7 +26,7 @@ type Paste = {
|
|||||||
export async function generateMetadata({
|
export async function generateMetadata({
|
||||||
params: { id },
|
params: { id },
|
||||||
}: PasteProps): Promise<Metadata> {
|
}: PasteProps): Promise<Metadata> {
|
||||||
const data = await getData(id);
|
const data: Paste | undefined = await getData(id);
|
||||||
if (data == undefined) {
|
if (data == undefined) {
|
||||||
return {
|
return {
|
||||||
description: "Not found",
|
description: "Not found",
|
||||||
@ -40,7 +40,14 @@ export async function generateMetadata({
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function getData(id: string): Promise<Paste | undefined> {
|
async function getData(id: string): Promise<Paste | undefined> {
|
||||||
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();
|
const json = await response.json();
|
||||||
|
|
||||||
if (json.code && json.message) {
|
if (json.code && json.message) {
|
||||||
@ -52,7 +59,7 @@ async function getData(id: string): Promise<Paste | undefined> {
|
|||||||
export default async function Paste({
|
export default async function Paste({
|
||||||
params: { id },
|
params: { id },
|
||||||
}: PasteProps): Promise<ReactElement> {
|
}: PasteProps): Promise<ReactElement> {
|
||||||
const data = await getData(id);
|
const data: Paste | undefined = await getData(id);
|
||||||
|
|
||||||
if (data == undefined) {
|
if (data == undefined) {
|
||||||
return notFound();
|
return notFound();
|
||||||
|
@ -16,6 +16,7 @@ export default function Home(): ReactElement {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Upload the paste to the server
|
||||||
const response = await fetch(
|
const response = await fetch(
|
||||||
`${process.env.NEXT_PUBLIC_API_ENDPOINT}/upload`,
|
`${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();
|
const data = await response.json();
|
||||||
window.location.href = `/${data.id}`;
|
window.location.href = `/${data.id}`;
|
||||||
}
|
}
|
||||||
|
@ -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);
|
|
||||||
}
|
|
@ -1,18 +1,18 @@
|
|||||||
|
import "./globals.css";
|
||||||
import type { Metadata } from "next";
|
import type { Metadata } from "next";
|
||||||
import { ThemeProvider } from "@/app/components/theme-provider";
|
import { ThemeProvider } from "@/app/components/theme-provider";
|
||||||
|
import { ReactNode } from "react";
|
||||||
import "./globals.css";
|
|
||||||
import { jetbrainsMono } from "@/app/common/font/font";
|
import { jetbrainsMono } from "@/app/common/font/font";
|
||||||
|
|
||||||
export const metadata: Metadata = {
|
export const metadata: Metadata = {
|
||||||
title: "Paste",
|
title: "Paste",
|
||||||
description: "Simple Pastebin",
|
description: "A simple Pastebin service",
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function RootLayout({
|
export default function RootLayout({
|
||||||
children,
|
children,
|
||||||
}: Readonly<{
|
}: Readonly<{
|
||||||
children: React.ReactNode;
|
children: ReactNode;
|
||||||
}>) {
|
}>) {
|
||||||
return (
|
return (
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
|
Reference in New Issue
Block a user