diff --git a/src/app/(withGlobalData)/layout.tsx b/src/app/(withGlobalData)/layout.tsx deleted file mode 100644 index 6b26bb9..0000000 --- a/src/app/(withGlobalData)/layout.tsx +++ /dev/null @@ -1,19 +0,0 @@ -import { getMovies } from "@/lib/db"; -import { GlobalStoreProvider } from "../store/globalStore"; -import { Navbar } from "@/components/organisms/Navbar"; - -export const revalidate = 0; -export default async function WithGlobalDataLayout({ - children, -}: { - children: React.ReactNode; -}) { - const movies = await getMovies(); - - return ( - - -
{children}
-
- ); -} diff --git a/src/app/api/movies/route.ts b/src/app/api/movies/route.ts new file mode 100644 index 0000000..bc71a12 --- /dev/null +++ b/src/app/api/movies/route.ts @@ -0,0 +1,14 @@ +import { getMovies } from "@/lib/db"; +import { NextResponse } from "next/server"; + +export const GET = async () => { + const movies = await getMovies(); + const res = NextResponse.json(movies); + + res.headers.set( + "Cache-Control", + "public, max-age=3, stale-while-revalidate=30" + ); + + return res; +}; diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 2a0859b..e0c0704 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -2,6 +2,8 @@ import "./globals.css"; import type { Metadata } from "next"; import { Geist, Geist_Mono } from "next/font/google"; +import { Navbar } from "@/components/organisms/Navbar"; +import { GlobalStoreProvider } from "./store/globalStore"; const geistSans = Geist({ variable: "--font-geist-sans", @@ -24,11 +26,14 @@ export default async function RootLayout({ children: React.ReactNode; }>) { return ( - + - {children} + + +
{children}
+
); diff --git a/src/app/(withGlobalData)/odkrywaj/page.tsx b/src/app/odkrywaj/page.tsx similarity index 93% rename from src/app/(withGlobalData)/odkrywaj/page.tsx rename to src/app/odkrywaj/page.tsx index 9494601..e03f475 100644 --- a/src/app/(withGlobalData)/odkrywaj/page.tsx +++ b/src/app/odkrywaj/page.tsx @@ -12,6 +12,7 @@ import { MovieCard } from "@/components/atoms/MovieCard"; export const revalidate = 43200; export default async function Home() { + const lastModified = new Date().toISOString(); const [nowPlayingData, popularData, trendingData, upcomingData] = await Promise.all([ getNowPlayingMovies(), @@ -80,6 +81,10 @@ export default async function Home() { + +
+

Ostatnia aktualizacja: {lastModified}

+
); } diff --git a/src/app/(withGlobalData)/page.tsx b/src/app/page.tsx similarity index 100% rename from src/app/(withGlobalData)/page.tsx rename to src/app/page.tsx diff --git a/src/app/(withGlobalData)/search/page.tsx b/src/app/search/page.tsx similarity index 100% rename from src/app/(withGlobalData)/search/page.tsx rename to src/app/search/page.tsx diff --git a/src/app/store/globalStore.tsx b/src/app/store/globalStore.tsx index 35d84a1..da1e785 100644 --- a/src/app/store/globalStore.tsx +++ b/src/app/store/globalStore.tsx @@ -1,6 +1,6 @@ "use client"; import { movies } from "@/lib/db/schema"; -import { createContext, FC, use, useState } from "react"; +import { createContext, FC, use, useEffect, useState } from "react"; type Movie = typeof movies.$inferSelect; @@ -20,11 +20,19 @@ const globalStore = createContext({ type Props = { children: React.ReactNode; - initialMovies: Movie[]; }; -export const GlobalStoreProvider: FC = ({ children, initialMovies }) => { - const [movies, setMovies] = useState(initialMovies); +export const GlobalStoreProvider: FC = ({ children }) => { + // Optimistic update + const [movies, setMovies] = useState([]); + + useEffect(() => { + fetch("/api/movies") + .then((res) => res.json()) + .then((data) => { + setMovies(data); + }); + }, []); const addMovie = async (movie: Movie) => { if (movies.find((m) => m.id === movie.id)) return;