Skip to content

Commit

Permalink
Static build legacy pages and 404 any others
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Oct 8, 2024
1 parent 65001d4 commit 9172de0
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 41 deletions.
10 changes: 10 additions & 0 deletions app/api/book-paths/route.tsx
Original file line number Diff line number Diff line change
@@ -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())
}
10 changes: 4 additions & 6 deletions app/books/legacy/[id]/comp/page.tsx
Original file line number Diff line number Diff line change
@@ -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<Array<{id: string}>> => {
const params = await getAllLegacyPaths()
const params = await getLegacyBookPaths()
return params.map(item => ({id: item.id.toString()}))
}

Expand Down
10 changes: 4 additions & 6 deletions app/books/legacy/[id]/extra/page.tsx
Original file line number Diff line number Diff line change
@@ -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<Array<{id: string}>> => {
const params = await getAllLegacyPaths()
const params = await getLegacyBookPaths()
return params.map(item => ({id: item.id.toString()}))
}

Expand Down
10 changes: 4 additions & 6 deletions app/books/legacy/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -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<Array<{id: string}>> => {
const params = await getAllLegacyPaths()
const params = await getLegacyBookPaths()
return params.map(item => ({id: item.id.toString()}))
}

Expand Down
3 changes: 1 addition & 2 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ export const revalidate = false
export const dynamic = "force-static"

const Home = async () => {
const {entity, error} = await getEntityFromPath<NodeStanfordPage>("/", isPreviewMode())
const {entity} = await getEntityFromPath<NodeStanfordPage>("/", isPreviewMode())

if (error) throw new Error(error)
if (!entity) notFound()

return (
Expand Down
2 changes: 1 addition & 1 deletion app/sitemap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<MetadataRoute.Sitemap> => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/gql/gql-queries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export const getAllNodes = nextCache(
return nodes
}),
["node-paths"],
{revalidate: 25200}
{revalidate: 60 * 60 * 24 * 7}
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | undefined> => {
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<NodeSupBook>
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
Expand Down

0 comments on commit 9172de0

Please sign in to comment.