Skip to content

Commit

Permalink
⚡ Slug bruker staticprops
Browse files Browse the repository at this point in the history
  • Loading branch information
KenAJoh committed Dec 6, 2023
1 parent 1272561 commit 09ac4d2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
35 changes: 27 additions & 8 deletions aksel.nav.no/website/pages/gp/[slug].tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { groq } from "next-sanity";
import { GetServerSideProps } from "next/types";
import { GetStaticPaths, GetStaticProps } from "next/types";
import { Suspense, lazy } from "react";
import GodPraksisPage from "@/layout/god-praksis-page/GodPraksisPage";
import {
Expand All @@ -14,6 +14,7 @@ import {
HeroNavT,
} from "@/layout/god-praksis-page/types";
import { getClient } from "@/sanity/client.server";
import { getGpTema } from "@/sanity/interface";
import { NextPageT } from "@/types";
import { SEO } from "@/web/seo/SEO";

Expand Down Expand Up @@ -41,17 +42,34 @@ const query = groq`
}
`;

export const getServerSideProps: GetServerSideProps = async (
ctx
): Promise<PageProps> => {
export const getStaticPaths: GetStaticPaths = async () => {
return {
paths: await getGpTema().then((paths) =>
paths.map(({ path }) => ({
params: {
slug: path,
},
}))
),
fallback: "blocking",
};
};

export const getStaticProps: GetStaticProps = async ({
params: { slug },
preview = false,
}: {
params: { slug: string };
preview?: boolean;
}): Promise<PageProps> => {
const {
heroNav,
tema,
innholdstype,
articles,
}: HeroNavT & GpTemaT & GpInnholdstypeT & GpArticleListT =
await getClient().fetch(query, {
slug: ctx.params.slug,
slug,
});

return {
Expand All @@ -65,12 +83,13 @@ export const getServerSideProps: GetServerSideProps = async (
tema,
heroNav: heroNav.filter((x) => x.hasRefs),
innholdstype: innholdstype.filter((x) => x.hasRefs),
slug: ctx.params.slug as string,
preview: ctx.preview ?? false,
slug,
preview,
id: "",
title: "",
},
notFound: !tema || !heroNav.some((nav) => nav.slug === ctx.params.slug),
notFound: !tema || !heroNav.some((nav) => nav.slug === slug),
revalidate: 60,
};
};

Expand Down
23 changes: 23 additions & 0 deletions aksel.nav.no/website/sanity/interface/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@ export async function getAkselTema(
}));
}

export async function getGpTema(
token?: string
): Promise<{ path: string; lastmod: string }[]> {
const client = token ? noCdnClient(token) : getClient();
const tags: {
slug: { current: string };
_updatedAt: string;
hasRefs: boolean;
}[] = await client.fetch(
`*[_type == "gp.tema"]{
slug,
_updatedAt,
"hasRefs": count(*[_type=="aksel_artikkel" && (^._id in undertema[]->tema._ref)]) > 0
}`
);
return tags
.filter((x) => x.hasRefs)
.map((x) => ({
path: x?.slug.current,
lastmod: x?._updatedAt,
}));
}

export async function getDocuments(
source: (typeof allArticleDocuments)[number] | "all",
token?: string
Expand Down

0 comments on commit 09ac4d2

Please sign in to comment.