Skip to content

Commit

Permalink
Merge pull request #661 from Dark-Matter-Labs/staging
Browse files Browse the repository at this point in the history
Fix caching issues by adding sanityFetch helper
  • Loading branch information
theocampbell authored Sep 10, 2024
2 parents 36ec907 + 5c2483c commit cab5ebd
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 242 deletions.
21 changes: 6 additions & 15 deletions app/[productChain]/[thema]/categorie/page.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { THEME_PATHS_QUERY, CATEGORIE_PAGE_QUERY, THEME_METADATA_QUERY } from '@/lib/queries';
import { client } from '@/lib/sanity';
import { client, sanityFetch } from '@/lib/sanity';
import ExpertiseLayout from '@/components/layouts/expertise-layout';

export async function generateMetadata({ params }, parent) {
Expand Down Expand Up @@ -41,21 +41,12 @@ export async function generateStaticParams() {

export const dynamicParams = false;

async function getCategoryData(params) {
const thema = params;
const categorieData = await client.fetch(
CATEGORIE_PAGE_QUERY,
{ thema },
{ next: { tags: ['instrument'] } },
);
if (!categorieData) {
throw new Error('could not get categorie data');
}
return categorieData;
}

export default async function CategoriePage({ params }) {
const categorieContent = await getCategoryData(params.thema);
const categorieContent = await sanityFetch({
query: CATEGORIE_PAGE_QUERY,
qParams: params,
tags: ['instrument'],
});
return (
<ExpertiseLayout
thema={params?.thema}
Expand Down
23 changes: 6 additions & 17 deletions app/[productChain]/[thema]/instrumenten/[slug]/page.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Instrument from '@/components/instrument';
import { INSTRUMENT_PATHS_QUERY, INSTRUMENT_PAGE_QUERY, INSTRUMENT_META_DATA } from '@/lib/queries';
import { client } from '@/lib/sanity';
import { client, sanityFetch } from '@/lib/sanity';

export async function generateMetadata({ params }, parent) {
// read route params
Expand Down Expand Up @@ -36,7 +36,6 @@ export async function generateMetadata({ params }, parent) {

export async function generateStaticParams() {
const slugs = await client.fetch(INSTRUMENT_PATHS_QUERY, { next: { tags: ['instrument'] } });

return slugs.map((slug) => ({
thema: slug.thema,
productChain: slug.productChain,
Expand All @@ -46,21 +45,11 @@ export async function generateStaticParams() {

export const dynamicParams = false;

async function getInstrumentData(params) {
const slug = params;
const instrumentData = await client.fetch(
INSTRUMENT_PAGE_QUERY,
{ slug },
{ next: { tags: ['instrument'] } },
);
if (!instrumentData) {
throw new Error('could not get instrument data');
}
return instrumentData;
}

export default async function InstrumentPage({ params }) {
const instrumentContent = await getInstrumentData(params.slug);

const instrumentContent = await sanityFetch({
query: INSTRUMENT_PAGE_QUERY,
qParams: params,
tags: ['instrument'],
});
return <Instrument data={instrumentContent} />;
}
22 changes: 6 additions & 16 deletions app/[productChain]/[thema]/overheidsbevoegdheid/page.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { THEME_PATHS_QUERY, GOV_LEVEL_QUERY, THEME_METADATA_QUERY } from '@/lib/queries';
import { client } from '@/lib/sanity';
import { client, sanityFetch } from '@/lib/sanity';
import GovLevelLayout from '@/components/layouts/gov-level-layout';
import placeholderImage from '@/public/gov-level-placeholder-mobile.png';

Expand Down Expand Up @@ -42,22 +42,12 @@ export async function generateStaticParams() {

export const dynamicParams = false;

async function getGovLeveldData(params) {
const thema = params;
const govLevelData = await client.fetch(
GOV_LEVEL_QUERY,
{ thema },
{ next: { tags: ['instrument'] } },
);
if (!govLevelData) {
throw new Error('could not get gov level data');
}
return govLevelData;
}

export default async function GovernmentLevelPage({ params }) {
const govLevelContent = await getGovLeveldData(params.thema);

const govLevelContent = await sanityFetch({
query: GOV_LEVEL_QUERY,
qParams: params,
tags: ['instrument'],
});
if (params.thema === 'matrasketen') {
return (
<GovLevelLayout
Expand Down
21 changes: 6 additions & 15 deletions app/[productChain]/[thema]/page.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { client } from '@/lib/sanity';
import { client, sanityFetch } from '@/lib/sanity';
import { THEME_PATHS_QUERY, THEME_QUERY, THEME_METADATA_QUERY } from '@/lib/queries';
import SimpleThemaLayout from '@/components/layouts/simple-thema-layout';
import ThemeLayout from '@/components/layouts/theme-index-layout';
Expand Down Expand Up @@ -42,21 +42,12 @@ export async function generateStaticParams() {

export const dynamicParams = false;

async function getThemeData(params) {
const thema = params;
const themaData = await client.fetch(
THEME_QUERY,
{ thema },
{ next: { tags: ['thema', 'simpleThema', 'instrument'] } },
);
if (!themaData) {
throw new Error('could not get theme data');
}
return themaData;
}

export default async function ThemePage({ params }) {
const themeData = await getThemeData(params.thema);
const themeData = await sanityFetch({
query: THEME_QUERY,
qParams: params,
tags: ['thema', 'simpleThema', 'instrument'],
});
if (themeData.thema._type === 'simpleThema') {
return (
<SimpleThemaLayout
Expand Down
41 changes: 11 additions & 30 deletions app/[productChain]/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
PRODUCT_CHAIN_METADATA_QUERY,
} from '@/lib/queries';
import PCLayout from '@/components/layouts/product-chain-layout';
import { client } from '@/lib/sanity';
import { client, sanityFetch } from '@/lib/sanity';

// metadata
export async function generateMetadata({ params }, parent) {
Expand Down Expand Up @@ -48,36 +48,17 @@ export async function generateStaticParams() {

export const dynamicParams = false;

// page data
async function getProductChainData(params) {
const productChain = params;
const productChainContent = await client.fetch(
PRODUCT_CHAIN_PAGE_QUERY,
{ productChain },
{ next: { tags: ['transitionAgenda'] } },
);
if (!productChainContent) {
throw new Error('could not get product chain data');
}
return productChainContent;
}

async function getThemeByPCData(params) {
const productChain = params;
const themeByPCData = await client.fetch(
THEMES_BY_PC_QUERY,
{ productChain },
{ next: { tags: ['thema', 'simpleThema'] } },
);
if (!themeByPCData) {
throw new Error('count not get theme data');
}
return themeByPCData;
}

export default async function ProductChainPage({ params }) {
const productChainData = await getProductChainData(params.productChain);
const themeByPCData = await getThemeByPCData(params.productChain);
const productChainData = await sanityFetch({
query: PRODUCT_CHAIN_PAGE_QUERY,
qParams: params,
tags: ['transitionAgenda'],
});
const themeByPCData = await sanityFetch({
query: THEMES_BY_PC_QUERY,
qParams: params,
tags: ['thema', 'simpleThema'],
});
return (
<PCLayout
productChainData={productChainData}
Expand Down
37 changes: 11 additions & 26 deletions app/eu-wetgeving/[law]/page.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Link from 'next/link';
import { client } from '@/lib/sanity';
import { client, sanityFetch } from '@/lib/sanity';
import SocialButtons from '@/components/social-buttons';
import Tabs from '@/components/eu-law/tabs';
import TabContent from '@/components/eu-law/tab-content';
Expand Down Expand Up @@ -51,33 +51,18 @@ export async function generateStaticParams() {

export const dynamicParams = false;

async function getTabData(params) {
const law = params;
const tabContent = client.fetch(
LAW_TAB_QUERY,
{ law },
{ next: { tags: ['euEuropeTab', 'euCircularEconomyTab', 'euLocalTab'] } },
);
if (!tabContent) {
throw new Error('data could not be fetched');
}
return tabContent;
}

async function getSummaryData(params) {
const law = params;
const summary = client.fetch(LAW_SUMMARY_QUERY, { law }, { next: { tags: ['euLaw'] } });
if (!summary) {
throw new Error('data could not be fetched');
}
return summary;
}

export default async function EULawPage({ params, searchParams }) {
const summaryData = await getSummaryData(params.law);
const tabData = await getTabData(params.law);
const summaryData = await sanityFetch({
query: LAW_SUMMARY_QUERY,
qParams: params,
tags: ['euLaw'],
});
const tabData = await sanityFetch({
query: LAW_TAB_QUERY,
qParams: params,
tags: ['euEuropeTab', 'euCircularEconomyTab', 'euLocalTab'],
});
const initialTab = searchParams.tab;

return (
<>
<div className='relative'>
Expand Down
13 changes: 2 additions & 11 deletions app/eu-wetgeving/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Image from 'next/image';
import { ArrowDownIcon } from '@heroicons/react/outline';
import CustomButton from '@/components/custom-button';
import EULawCard from '@/components/eu-law/eu-law-card';
import { client } from '@/lib/sanity';
import { sanityFetch } from '@/lib/sanity';
import { EU_LAW_OVERVIEW_QUERY } from '@/lib/queries';
import ScrollButton from '@/components/scroll-button';
import globalMeta from '@/utils/global-meta';
Expand All @@ -25,17 +25,8 @@ export const metadata = {
},
};

async function getEuLaws() {
const res = client.fetch(EU_LAW_OVERVIEW_QUERY, { next: { tags: ['euLaw'] } });
if (!res) {
throw new Error('failed to fetch data');
}
return res;
}

export default async function Page() {
const data = await getEuLaws();

const data = await sanityFetch({ query: EU_LAW_OVERVIEW_QUERY, tags: ['euLaw'] });
return (
<>
<div className='bg-[url("/bg-eu.png")] pt-3 overflow-hidden bg-center bg-no-repeat bg-cover'>
Expand Down
38 changes: 6 additions & 32 deletions app/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { LinkedInInsightTag } from 'nextjs-linkedin-insight-tag';

import globalMeta from '@/utils/global-meta';
import Layout from '@/components/layouts/layout';
import { client } from '@/lib/sanity';
import { sanityFetch } from '@/lib/sanity';
import { NAV_QUERY, PARTNERS_QUERY } from '@/lib/queries';
import { getCookie, hasCookie } from 'cookies-next';
import { cookies } from 'next/headers';
Expand Down Expand Up @@ -39,38 +39,12 @@ export const metadata = {
},
};

async function getNavData() {
const navData = await client.fetch(NAV_QUERY, {
next: {
tags: [
'aboutPages',
'navigation',
'thema',
'simpleThema',
'euLaw',
'siteConfig',
'navigation',
],
},
});
if (!navData) {
throw new Error('could not fetch navData');
}
return navData;
}

async function getPartnerLogos() {
const partnerLogos = await client.fetch(PARTNERS_QUERY, { next: { tags: ['partners'] } });
if (!partnerLogos) {
throw new Error('could not fetch partnerLogos');
}
return partnerLogos;
}

export default async function RootLayout({ children }) {
const navData = await getNavData();
const partnerLogos = await getPartnerLogos();

const partnerLogos = await sanityFetch({ query: PARTNERS_QUERY, tags: ['partners'] });
const navData = await sanityFetch({
query: NAV_QUERY,
tags: ['aboutPages', 'navigation', 'thema', 'simpleThema', 'euLaw', 'siteConfig', 'navigation'],
});
const hasLocalConsentCookie = hasCookie('localConsent', { cookies });
const hotjarCookie = getCookie('localConsent', { cookies });

Expand Down
21 changes: 6 additions & 15 deletions app/nieuws/[slug]/page.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import NewsDetailPageHeader from '@/components/news-page/news-detail-page-header';
import { client } from '@/lib/sanity';
import { client, sanityFetch } from '@/lib/sanity';
import NewsDetailPageBody from '@/components/news-page/news-detail-page-body';
import { NEWS_SLUGS_QUERY, NEWS_DETAIL_PAGE_QUERY, NEWS_METADATA_QUERY } from '@/lib/queries';

Expand Down Expand Up @@ -39,21 +39,12 @@ export async function generateStaticParams() {

export const dynamicParams = false;

async function getNewsPageData(params) {
const slug = params;
const newsPageData = await client.fetch(
NEWS_DETAIL_PAGE_QUERY,
{ slug },
{ next: { tags: ['newsItem'] } },
);
if (!newsPageData) {
throw new Error('could not get news detail data');
}
return newsPageData;
}

export default async function NewsDetailPage({ params }) {
const newsPageContent = await getNewsPageData(params.slug);
const newsPageContent = await sanityFetch({
query: NEWS_DETAIL_PAGE_QUERY,
qParams: params,
tags: ['newsItem'],
});
return (
<>
<NewsDetailPageHeader data={newsPageContent} />
Expand Down
Loading

0 comments on commit cab5ebd

Please sign in to comment.