Skip to content

Commit

Permalink
Increase cache lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish committed Jan 22, 2024
1 parent a8d25d5 commit c5926f4
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 21 deletions.
7 changes: 5 additions & 2 deletions app/(public)/[...slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {getMenu} from "@/lib/drupal/get-menu";
import {isDraftMode} from "@/lib/drupal/is-draft-mode";
import UnpublishedBanner from "@/components/patterns/unpublished-banner";
import {getPathFromContext} from "@/lib/drupal/utils";
import {cache} from "react";

export const revalidate = false;

Expand All @@ -23,7 +24,7 @@ class RedirectError extends Error {
}
}

const fetchNodeData = async (params: Params): Promise<{ node: StanfordNode, fullWidth: boolean }> => {
const fetchNodeData = cache(async (params: Params): Promise<{ node: StanfordNode, fullWidth: boolean }> => {
const draftMode = isDraftMode();
const path = getPathFromContext({params});
const valid = await pathIsValid(path)
Expand Down Expand Up @@ -56,9 +57,11 @@ const fetchNodeData = async (params: Params): Promise<{ node: StanfordNode, full
(node?.type === 'node--sul_library' && node.layout_selection?.resourceIdObjMeta?.drupal_internal__target_id === 'sul_library_full_width');

return {node, fullWidth}
}
})

export const generateMetadata = async ({params}: PageProps): Promise<Metadata> => {
if (isDraftMode()) return {};

try {
const {node} = await fetchNodeData(params);
if (!node) return {};
Expand Down
2 changes: 1 addition & 1 deletion app/(public)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {ParagraphRows} from "@/components/paragraph/rows/rows";
import fetchComponents from "@/lib/fetch-components";
import {notFound} from "next/navigation";

export const revalidate = 1800;
export const revalidate = false;

export const generateMetadata = async (): Promise<Metadata> => {
const node = await getResourceByPath<BasicPage>('/');
Expand Down
1 change: 0 additions & 1 deletion src/lib/drupal/get-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export const getMenu = async(
const url = buildUrl(`/jsonapi/menu_items/${name}`)

const response = await fetch(url.toString(), {
next: {revalidate: 86400},
headers: await buildHeaders(options),
})

Expand Down
21 changes: 4 additions & 17 deletions src/lib/drupal/get-resource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,7 @@ import {buildUrl, buildHeaders, getJsonApiPathForResourceType, getPathFromContex
import {deserialize} from "@/lib/drupal/deserialize";
import {JsonApiParams} from "next-drupal";
import {PageProps} from "@/lib/drupal/drupal";

export const getResources = async <T, >(
items: { type: string, id: string }[],
draftMode: boolean = false
): Promise<T[]> => {
const requests: PromiseLike<any>[] = [];
items.map(item => requests.push(getResource(item.type, item.id, {draftMode})));

// @ts-ignore
return Promise.all(requests.map((p, i): T => p.catch((e: unknown) => {
console.error(`Failed Fetching (probably unpublished) component ${items[i].type}-${items[i].id}`, e);
return null
})));
}
import {cache} from "react";

export const getResourceFromContext = async <T extends JsonApiResource>(
type: string,
Expand Down Expand Up @@ -54,7 +41,7 @@ export const getResourceFromContext = async <T extends JsonApiResource>(
return resource
}

export const getResourceByPath = async <T extends JsonApiResource>(
export const getResourceByPath = cache(async <T extends JsonApiResource>(
path: string,
options?: {
accessToken?: AccessToken
Expand Down Expand Up @@ -86,7 +73,7 @@ export const getResourceByPath = async <T extends JsonApiResource>(
]

const url = buildUrl("/subrequests", {_format: "json"})
console.log('subrequest path', JSON.stringify(path));
console.log('subrequest path', path);

let response = await fetch(url.toString(), {
next: options.next,
Expand Down Expand Up @@ -122,7 +109,7 @@ export const getResourceByPath = async <T extends JsonApiResource>(
if (data.errors) throw new Error(data.errors[0].detail)

return options.deserialize ? deserialize(data) : data
}
})

export const getResourceCollection = async <T extends JsonApiResource>(
type: string,
Expand Down

1 comment on commit c5926f4

@vercel
Copy link

@vercel vercel bot commented on c5926f4 Jan 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.