diff --git a/app/[...slug]/page.tsx b/app/[...slug]/page.tsx index a270c5fb..fe2b6e00 100644 --- a/app/[...slug]/page.tsx +++ b/app/[...slug]/page.tsx @@ -6,20 +6,29 @@ import {getPathsFromContext} from "@lib/drupal/get-paths"; import {getNodeMetadata} from "./metadata"; import {getPathFromContext, isDraftMode} from "@lib/drupal/utils"; import {PageProps} from "@lib/types"; -import {graphqlClient} from "@lib/gql/fetcher"; -import {NodeUnion, RouteQuery} from "@lib/gql/__generated__/drupal"; -import {getAccessToken} from "@lib/drupal/get-access-token"; -import {cache} from "@lib/drupal/get-cache"; +import {getEntityFromPath} from "@lib/gql/fetcher"; +import {NodeUnion} from "@lib/gql/__generated__/drupal"; // https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config export const revalidate = false; +const Page = async ({params}: PageProps) => { + const path = getPathFromContext({params}) + const routeInfo = await getEntityFromPath(path) + if (routeInfo?.redirect?.url) redirect(routeInfo.redirect.url) + if (!routeInfo?.entity) notFound(); + + return ( + + ) +} + export const generateMetadata = async ({params}: PageProps): Promise => { const path = getPathFromContext({params}) try { - const node = await getNodeForPath(path) - if (node) return getNodeMetadata(node); + const routeInfo = await getEntityFromPath(path, isDraftMode()) + if (routeInfo?.entity) return getNodeMetadata(routeInfo.entity); } catch (e) { } return {} @@ -60,41 +69,9 @@ export const generateStaticParams = async () => { page++; } - return paths.map(path => typeof path !== "string" ? path?.params : path).slice(0, (completeBuild ? -1 : 1)); -} - -const Page = async ({params}: PageProps) => { - const path = getPathFromContext({params}) - const node = await getNodeForPath(path) - - if (!node) notFound(); - - return ( - - ) -} - -const getNodeForPath = async (path: string): Promise => { - const cacheKey = path.replaceAll('/', '--'); - const token = await getAccessToken(isDraftMode()); - let node = cache.get(cacheKey); - - if (!node || token) { - let query: RouteQuery; - try { - query = await graphqlClient(token?.access_token).Route({path}); - } catch (e) { - console.error(`Error fetching route data for '${path}'. ` + (e instanceof Error && e.message)); - return; - } - - if (query.route?.__typename === 'RouteRedirect') redirect(query.route.url); - node = query.route?.__typename === 'RouteInternal' ? query.route.entity as NodeUnion : undefined - } - - // Just cache the node for 30 seconds so that any remaining queries during the build process will result in the cached data. - if (node) cache.set(cacheKey, node, 30); - return node; + return paths.filter(path => typeof path !== 'object' || path.params?.slug?.[0]) + .map(path => typeof path === "object" ? path?.params : path) + .slice(0, (completeBuild ? -1 : 1)) } export default Page; \ No newline at end of file diff --git a/app/page.tsx b/app/page.tsx index bd9bcfab..3b4363bc 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,43 +1,29 @@ import Rows from "@components/paragraphs/rows/rows"; import Paragraph from "@components/paragraphs/paragraph"; import {notFound} from "next/navigation"; -import {graphqlClient} from "@lib/gql/fetcher"; +import {getEntityFromPath} from "@lib/gql/fetcher"; import {NodeStanfordPage} from "@lib/gql/__generated__/drupal"; import {isDraftMode} from "@lib/drupal/utils"; -import {getAccessToken} from "@lib/drupal/get-access-token"; -// Cache the home page for 24 hours. It should be the most modified and probably changes more frequent. +// Cache the home page for 24 hours. It should be the most modified and probably changes most frequent. export const revalidate = 86400; const Home = async () => { - const node = await getHomePageNode(); - if (!node) notFound(); + const routeInfo = await getEntityFromPath('/', isDraftMode()); + if (!routeInfo?.entity) notFound(); return (
- {node.suPageBanner && + {routeInfo.entity.suPageBanner &&
- +
} - {node.suPageComponents && - + {routeInfo.entity.suPageComponents && + }
) } -const getHomePageNode = async (): Promise => { - let node: NodeStanfordPage | false; - const token = await getAccessToken(isDraftMode()); - try { - const query = await graphqlClient(token?.access_token).Route({path: '/'}); - node = query.route?.__typename === 'RouteInternal' && query.route.entity as NodeStanfordPage - } catch (e) { - console.error('Error fetching home node. ' + (e instanceof Error && e.message)); - return; - } - return node || undefined; -} - export default Home; \ No newline at end of file diff --git a/src/components/elements/button.tsx b/src/components/elements/button.tsx index 010156bd..72fe9f5d 100644 --- a/src/components/elements/button.tsx +++ b/src/components/elements/button.tsx @@ -2,6 +2,7 @@ import Link from "@components/elements/link"; import {twMerge} from 'tailwind-merge' import {HtmlHTMLAttributes, MouseEventHandler} from "react"; import {Maybe} from "@lib/gql/__generated__/drupal"; +import {clsx} from "clsx"; type Props = HtmlHTMLAttributes & { @@ -10,30 +11,26 @@ type Props = HtmlHTMLAttributes & { big?: boolean secondary?: boolean centered?: boolean - className?: Maybe onClick?: MouseEventHandler prefetch?: boolean } -export const Button = ({href, buttonElem = false, big = false, secondary = false, centered = false, children, className = "", ...props}: Props) => { +export const Button = ({href, buttonElem = false, big = false, secondary = false, centered = false, children, className, ...props}: Props) => { - className = twMerge(className, (centered ? "flex items-center w-fit mx-auto" : "inline-block text-center w-fit")) - - if (big) { - // Big button styles. - className = twMerge(`btn btn--big transition text-5xl text-white hocus:text-white bg-digital-red hocus:bg-black no-underline hocus:underline py-6 px-12 font-normal`, className) - } else if (secondary) { - // Secondary button styles. - className = twMerge(`btn btn--secondary transition text-digital-red border-2 border-digital-red hocus:border-black no-underline hocus:underline py-4 px-8 font-normal`, className) - } else { - // Regular button styles. - className = twMerge(`btn bg-digital-red text-white hocus:bg-black hocus:text-white py-4 px-8 no-underline hocus:underline transition`, className) - } + const standardClasses = clsx( + { + 'flex items-center w-fit mx-auto': centered, + 'inline-block text-center w-fit': !centered, + 'btn btn--big transition text-5xl text-white hocus:text-white bg-digital-red hocus:bg-black no-underline hocus:underline py-6 px-12 font-normal': big && !secondary, + 'btn btn--secondary transition text-digital-red border-2 border-digital-red hocus:border-black no-underline hocus:underline py-4 px-8 font-normal': !big && secondary, + 'btn bg-digital-red text-white hocus:bg-black hocus:text-white py-4 px-8 no-underline hocus:underline transition': !big && !secondary, + } + ) if (!href || buttonElem) { return (