diff --git a/app/api/revalidate/route.tsx b/app/api/revalidate/route.tsx index 8446ba9d..a33d5c09 100644 --- a/app/api/revalidate/route.tsx +++ b/app/api/revalidate/route.tsx @@ -1,12 +1,12 @@ import {NextRequest, NextResponse} from "next/server"; import {revalidatePath, revalidateTag} from "next/cache"; -import {headers} from "next/headers"; +import {getAllDrupalPaths} from "@lib/drupal/get-paths"; + +export const revalidate = 0; // The app router doesn't correctly revalidate the paths on Vercel. This file is meant for testing and future // implementation when it actually works. export const GET = async (request: NextRequest) => { - // Call the headers to prevent the route from getting cached. - headers(); const secret = request.nextUrl.searchParams.get('secret'); if (secret !== process.env.DRUPAL_REVALIDATE_SECRET) { @@ -16,7 +16,14 @@ export const GET = async (request: NextRequest) => { if (!path) { return NextResponse.json({message: 'Missing slug'}, {status: 400}); } - revalidateTag('paths'); + const tagsInvalidated = ['paths']; + + if (path.startsWith('views/')) { + tagsInvalidated.push(path.replace('/', ':')) + } + tagsInvalidated.map(tag => revalidateTag(tag)); + + await getAllDrupalPaths(); revalidatePath(path); - return NextResponse.json({revalidated: true, path, tags: ['paths']}); + return NextResponse.json({revalidated: true, path, tags: tagsInvalidated}); } \ No newline at end of file diff --git a/app/api/revalidate/views/events/route.tsx b/app/api/revalidate/views/events/route.tsx deleted file mode 100644 index 411d9a89..00000000 --- a/app/api/revalidate/views/events/route.tsx +++ /dev/null @@ -1,7 +0,0 @@ -import {NextRequest} from "next/server"; -import {InvalidateViews} from "../invalidate-views"; - -// Invalidate all view tags. -export const GET = async (request: NextRequest) => { - return InvalidateViews(request, 'views:stanford_event'); -} \ No newline at end of file diff --git a/app/api/revalidate/views/invalidate-views.tsx b/app/api/revalidate/views/invalidate-views.tsx deleted file mode 100644 index edacf89e..00000000 --- a/app/api/revalidate/views/invalidate-views.tsx +++ /dev/null @@ -1,12 +0,0 @@ -import {NextRequest, NextResponse} from "next/server"; -import {revalidateTag} from "next/cache"; - -export const InvalidateViews = (request: NextRequest, tags: string | string[]) => { - const secret = request.nextUrl.searchParams.get('secret'); - if (secret !== process.env.DRUPAL_REVALIDATE_SECRET) { - return NextResponse.json({message: 'Invalid token'}, {status: 403}); - } - if (typeof tags === 'string') tags = [tags]; - tags.forEach(tag => revalidateTag(tag)); - return NextResponse.json({revalidated: true, tags}); -} \ No newline at end of file diff --git a/app/api/revalidate/views/news/route.tsx b/app/api/revalidate/views/news/route.tsx deleted file mode 100644 index 15f9afc2..00000000 --- a/app/api/revalidate/views/news/route.tsx +++ /dev/null @@ -1,7 +0,0 @@ -import {NextRequest} from "next/server"; -import {InvalidateViews} from "../invalidate-views"; - -// Invalidate all view tags. -export const GET = async (request: NextRequest) => { - return InvalidateViews(request, 'views:stanford_news'); -} \ No newline at end of file diff --git a/app/api/revalidate/views/pages/route.tsx b/app/api/revalidate/views/pages/route.tsx deleted file mode 100644 index 8891da0c..00000000 --- a/app/api/revalidate/views/pages/route.tsx +++ /dev/null @@ -1,7 +0,0 @@ -import {NextRequest} from "next/server"; -import {InvalidateViews} from "../invalidate-views"; - -// Invalidate all view tags. -export const GET = async (request: NextRequest) => { - return InvalidateViews(request, 'views:stanford_page'); -} \ No newline at end of file diff --git a/app/api/revalidate/views/people/route.tsx b/app/api/revalidate/views/people/route.tsx deleted file mode 100644 index c5b5d4bc..00000000 --- a/app/api/revalidate/views/people/route.tsx +++ /dev/null @@ -1,7 +0,0 @@ -import {NextRequest} from "next/server"; -import {InvalidateViews} from "../invalidate-views"; - -// Invalidate all view tags. -export const GET = async (request: NextRequest) => { - return InvalidateViews(request, 'views:stanford_person'); -} \ No newline at end of file diff --git a/app/api/revalidate/views/publications/route.tsx b/app/api/revalidate/views/publications/route.tsx deleted file mode 100644 index 739eed5f..00000000 --- a/app/api/revalidate/views/publications/route.tsx +++ /dev/null @@ -1,7 +0,0 @@ -import {NextRequest} from "next/server"; -import {InvalidateViews} from "../invalidate-views"; - -// Invalidate all view tags. -export const GET = async (request: NextRequest) => { - return InvalidateViews(request, 'views:stanford_publication'); -} \ No newline at end of file diff --git a/app/api/revalidate/views/route.tsx b/app/api/revalidate/views/route.tsx deleted file mode 100644 index dfa3cc1f..00000000 --- a/app/api/revalidate/views/route.tsx +++ /dev/null @@ -1,7 +0,0 @@ -import {NextRequest} from "next/server"; -import {InvalidateViews} from "./invalidate-views"; - -// Invalidate all view tags. -export const GET = async (request: NextRequest) => { - return InvalidateViews(request, 'views'); -} \ No newline at end of file diff --git a/src/components/paragraphs/stanford-lists/list-paragraph.tsx b/src/components/paragraphs/stanford-lists/list-paragraph.tsx index d99d40ee..eea150c1 100644 --- a/src/components/paragraphs/stanford-lists/list-paragraph.tsx +++ b/src/components/paragraphs/stanford-lists/list-paragraph.tsx @@ -2,12 +2,10 @@ import Wysiwyg from "@components/elements/wysiwyg"; import Button from "@components/elements/button"; import View from "@components/views/view"; import {H2} from "@components/elements/headers"; -import {HtmlHTMLAttributes, Suspense} from "react"; +import {cache, HtmlHTMLAttributes, Suspense} from "react"; import {Maybe, NodeUnion, ParagraphStanfordList} from "@lib/gql/__generated__/drupal"; - import {getParagraphBehaviors} from "@components/paragraphs/get-paragraph-behaviors"; import {graphqlClient} from "@lib/gql/fetcher"; -import {cache} from "@lib/drupal/get-cache"; type Props = HtmlHTMLAttributes & { paragraph: ParagraphStanfordList @@ -61,13 +59,10 @@ const ListParagraph = async ({paragraph, ...props}: Props) => { ) } -const getViewItems = async (viewId: string, displayId: string, contextualFilter?: Maybe): Promise => { - const cacheKey = `views--${viewId}--${displayId}--` + contextualFilter?.join('-') - let items = cache.get(cacheKey); - if (items) return items; - items = []; +const getViewItems = cache(async (viewId: string, displayId: string, contextualFilter?: Maybe): Promise => { + let items: NodeUnion[] = [] - const tags = ['views']; + const tags = ['views:all']; switch (`${viewId}--${displayId}`) { case 'stanford_basic_pages--basic_page_type_list': case 'stanford_basic_pages--viewfield_block_1': @@ -164,11 +159,8 @@ const getViewItems = async (viewId: string, displayId: string, contextualFilter? console.error(`Unable to find query for view: ${viewId} display: ${displayId}`) break; } - - // Cache for long enough that we don't re-fetch for the same view. - cache.set(cacheKey, items, 60); return items; -} +}) const getViewFilters = (keys: string[], values?: Maybe) => { if (!keys || !values) return; diff --git a/src/lib/drupal/get-paths.tsx b/src/lib/drupal/get-paths.tsx index 0f758bd5..00d4e505 100644 --- a/src/lib/drupal/get-paths.tsx +++ b/src/lib/drupal/get-paths.tsx @@ -50,7 +50,10 @@ const getNodePaths = async (): Promise => { params.addPageOffset(page * 50); // Use JSON API to fetch the list of all node paths on the site. - fetchedData = await getPathsFromContext(contentTypes, {params: params.getQueryObject(), next: {tags: ['paths']}}) + fetchedData = await getPathsFromContext(contentTypes, { + params: params.getQueryObject(), + next: {tags: ['paths']} + }) paths = [...paths, ...fetchedData]; fetchMore = fetchedData.length > 0; page++;