From 9172de0720ddb417ad258b377256074f02cf5400 Mon Sep 17 00:00:00 2001 From: Mike Decker Date: Mon, 7 Oct 2024 20:00:20 -0700 Subject: [PATCH] Static build legacy pages and 404 any others --- app/api/book-paths/route.tsx | 10 ++++++++++ app/books/legacy/[id]/comp/page.tsx | 10 ++++------ app/books/legacy/[id]/extra/page.tsx | 10 ++++------ app/books/legacy/[id]/page.tsx | 10 ++++------ app/page.tsx | 3 +-- app/sitemap.tsx | 2 +- src/lib/gql/gql-queries.tsx | 2 +- .../lib/utils/getLegacyBookPaths.tsx | 20 +------------------ 8 files changed, 26 insertions(+), 41 deletions(-) create mode 100644 app/api/book-paths/route.tsx rename app/books/getBookPathFromWorkId.tsx => src/lib/utils/getLegacyBookPaths.tsx (58%) diff --git a/app/api/book-paths/route.tsx b/app/api/book-paths/route.tsx new file mode 100644 index 00000000..e81994dd --- /dev/null +++ b/app/api/book-paths/route.tsx @@ -0,0 +1,10 @@ +import {NextRequest, NextResponse} from "next/server" +import {revalidateTag, unstable_cache as nextCache} from "next/cache" +import {getEntityFromPath} from "@lib/gql/gql-queries" +import {getLegacyBookPaths} from "@lib/utils/getLegacyBookPaths" + +export const revalidate = 86400 + +export const GET = async () => { + return NextResponse.json(await getLegacyBookPaths()) +} diff --git a/app/books/legacy/[id]/comp/page.tsx b/app/books/legacy/[id]/comp/page.tsx index e6e45ebe..0cafe638 100644 --- a/app/books/legacy/[id]/comp/page.tsx +++ b/app/books/legacy/[id]/comp/page.tsx @@ -1,22 +1,20 @@ import {notFound, redirect} from "next/navigation" -import {getAllLegacyPaths, getBookPathFromWorkId} from "../../../getBookPathFromWorkId" +import {getLegacyBookPaths} from "@lib/utils/getLegacyBookPaths" export const revalidate = false export const dynamic = "force-static" +export const dynamicParams = false const LegacyBookPage = async ({params}: {params: {id: string}}) => { - const legacyPaths = await getAllLegacyPaths() + const legacyPaths = await getLegacyBookPaths() const legacyBook = legacyPaths.find(book => book.id === parseInt(params.id)) if (legacyBook) redirect(legacyBook.path + "/desk-examination-copy-requests") - const bookPath = /^\d+$/.test(params.id) && (await getBookPathFromWorkId(parseInt(params.id))) - if (bookPath) redirect(bookPath + "/desk-examination-copy-requests") - notFound() } export const generateStaticParams = async (): Promise> => { - const params = await getAllLegacyPaths() + const params = await getLegacyBookPaths() return params.map(item => ({id: item.id.toString()})) } diff --git a/app/books/legacy/[id]/extra/page.tsx b/app/books/legacy/[id]/extra/page.tsx index 14a99a61..601e8af0 100644 --- a/app/books/legacy/[id]/extra/page.tsx +++ b/app/books/legacy/[id]/extra/page.tsx @@ -1,22 +1,20 @@ import {notFound, redirect} from "next/navigation" -import {getAllLegacyPaths, getBookPathFromWorkId} from "../../../getBookPathFromWorkId" +import {getLegacyBookPaths} from "@lib/utils/getLegacyBookPaths" export const revalidate = false export const dynamic = "force-static" +export const dynamicParams = false const LegacyBookPage = async ({params}: {params: {id: string}}) => { - const legacyPaths = await getAllLegacyPaths() + const legacyPaths = await getLegacyBookPaths() const legacyBook = legacyPaths.find(book => book.id === parseInt(params.id)) if (legacyBook) redirect(legacyBook.path + "/excerpts") - const bookPath = /^\d+$/.test(params.id) && (await getBookPathFromWorkId(parseInt(params.id))) - if (bookPath) redirect(bookPath + "/excerpts") - notFound() } export const generateStaticParams = async (): Promise> => { - const params = await getAllLegacyPaths() + const params = await getLegacyBookPaths() return params.map(item => ({id: item.id.toString()})) } diff --git a/app/books/legacy/[id]/page.tsx b/app/books/legacy/[id]/page.tsx index 0c380ce5..f0b58cd3 100644 --- a/app/books/legacy/[id]/page.tsx +++ b/app/books/legacy/[id]/page.tsx @@ -1,22 +1,20 @@ import {notFound, redirect} from "next/navigation" -import {getAllLegacyPaths, getBookPathFromWorkId} from "../../getBookPathFromWorkId" +import {getLegacyBookPaths} from "@lib/utils/getLegacyBookPaths" export const revalidate = false export const dynamic = "force-static" +export const dynamicParams = false const LegacyBookPage = async ({params}: {params: {id: string}}) => { - const legacyPaths = await getAllLegacyPaths() + const legacyPaths = await getLegacyBookPaths() const legacyBook = legacyPaths.find(book => book.id === parseInt(params.id)) if (legacyBook) redirect(legacyBook.path) - const bookPath = /^\d+$/.test(params.id) && (await getBookPathFromWorkId(parseInt(params.id))) - if (bookPath) redirect(bookPath) - notFound() } export const generateStaticParams = async (): Promise> => { - const params = await getAllLegacyPaths() + const params = await getLegacyBookPaths() return params.map(item => ({id: item.id.toString()})) } diff --git a/app/page.tsx b/app/page.tsx index b6aa3333..dce4b6f7 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -12,9 +12,8 @@ export const revalidate = false export const dynamic = "force-static" const Home = async () => { - const {entity, error} = await getEntityFromPath("/", isPreviewMode()) + const {entity} = await getEntityFromPath("/", isPreviewMode()) - if (error) throw new Error(error) if (!entity) notFound() return ( diff --git a/app/sitemap.tsx b/app/sitemap.tsx index 538954ff..c627fcb9 100644 --- a/app/sitemap.tsx +++ b/app/sitemap.tsx @@ -2,7 +2,7 @@ import {MetadataRoute} from "next" import {getAllNodes} from "@lib/gql/gql-queries" // https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config -export const revalidate = 25200 +export const revalidate = 604800 export const dynamic = "force-static" const Sitemap = async (): Promise => { diff --git a/src/lib/gql/gql-queries.tsx b/src/lib/gql/gql-queries.tsx index e2d92435..09542e22 100644 --- a/src/lib/gql/gql-queries.tsx +++ b/src/lib/gql/gql-queries.tsx @@ -162,7 +162,7 @@ export const getAllNodes = nextCache( return nodes }), ["node-paths"], - {revalidate: 25200} + {revalidate: 60 * 60 * 24 * 7} ) /** diff --git a/app/books/getBookPathFromWorkId.tsx b/src/lib/utils/getLegacyBookPaths.tsx similarity index 58% rename from app/books/getBookPathFromWorkId.tsx rename to src/lib/utils/getLegacyBookPaths.tsx index 3ebf936f..4ac59b56 100644 --- a/app/books/getBookPathFromWorkId.tsx +++ b/src/lib/utils/getLegacyBookPaths.tsx @@ -2,25 +2,7 @@ import {BooksWorkIdQuery, BooksWorkIdQueryVariables, Maybe, NodeSupBook} from "@ import {graphqlClient} from "@lib/gql/gql-client" import {unstable_cache as nextCache} from "next/cache" -export const getBookPathFromWorkId = nextCache( - async (workId: number): Promise => { - const contextualFilters = { - term_node_taxonomy_name_depth: '""', - term_node_taxonomy_name_depth_2: '""', - term_node_taxonomy_name_depth_1: '""', - term_node_taxonomy_name_depth_3: '""', - sup_book_work_id_number_value: workId.toString(), - } - - const graphqlResponse = await graphqlClient({cache: "no-cache"}).supBooks({contextualFilters}) - const book = graphqlResponse.supBooksView?.results.pop() as Maybe - return book?.path - }, - ["legacy-book"], - {tags: ["legacy-books"]} -) - -export const getAllLegacyPaths = nextCache( +export const getLegacyBookPaths = nextCache( async () => { const nodes: Array<{id: number; path: string}> = [] let fetchMore = true