From 2836d4801d1737b9cad4752e2906ad70804bc5dd Mon Sep 17 00:00:00 2001 From: Dag Stuan Date: Sun, 10 Nov 2024 20:10:08 +0100 Subject: [PATCH] Live for everything. Fix sitemap. --- src/app/(site)/layout.tsx | 23 ++++++++----------- src/app/(site)/oppskrifter/[slug]/page.tsx | 2 +- src/app/robots.ts | 2 +- src/app/{(site) => }/shared-metadata.ts | 0 src/app/{(site) => }/sitemap.ts | 13 ++++++----- .../pages/RecipePage/RecipePage.tsx | 2 +- src/sanity/lib/client.ts | 22 ++---------------- 7 files changed, 22 insertions(+), 42 deletions(-) rename src/app/{(site) => }/shared-metadata.ts (100%) rename src/app/{(site) => }/sitemap.ts (77%) diff --git a/src/app/(site)/layout.tsx b/src/app/(site)/layout.tsx index e0842ff..e15635d 100644 --- a/src/app/(site)/layout.tsx +++ b/src/app/(site)/layout.tsx @@ -13,12 +13,11 @@ import { siteName, siteUrl, twitterMetadata, -} from "./shared-metadata"; -import { cache } from "react"; -import { sanityFetchNonLive } from "@/sanity/lib/client"; +} from "../shared-metadata"; import Script from "next/script"; import { DynamicDisableDraftMode } from "./DynamicDisableDraftMode"; import dynamic from "next/dynamic"; +import { sanityFetch } from "@/sanity/lib/live"; const SanityLive = dynamic(() => import("@/sanity/lib/live").then((mod) => mod.SanityLive), @@ -39,16 +38,11 @@ const geistMono = localFont({ weight: "100 900", }); -const getSeoData = cache( - async () => - await sanityFetchNonLive({ - query: homeSeoQuery, - revalidate: 60 * 60, - }), -); - export async function generateMetadata(): Promise { - const homeSeo = await getSeoData(); + const { data: homeSeo } = await sanityFetch({ + query: homeSeoQuery, + stega: false, + }); const metaDescription = homeSeo?.seo?.metaDescription ?? ""; @@ -134,7 +128,10 @@ export default async function RootLayout({ }: Readonly<{ children: React.ReactNode; }>) { - const homeSeo = await getSeoData(); + const { data: homeSeo } = await sanityFetch({ + query: homeSeoQuery, + stega: false, + }); const jsonLd: WithContext = { "@context": "https://schema.org", diff --git a/src/app/(site)/oppskrifter/[slug]/page.tsx b/src/app/(site)/oppskrifter/[slug]/page.tsx index 58f55ae..b280a5d 100644 --- a/src/app/(site)/oppskrifter/[slug]/page.tsx +++ b/src/app/(site)/oppskrifter/[slug]/page.tsx @@ -5,7 +5,7 @@ import { openGraphMetadata, siteUrl, twitterMetadata, -} from "../../shared-metadata"; +} from "../../../shared-metadata"; import { sanityFetch } from "@/sanity/lib/live"; import { RecipePage } from "@/components/pages/RecipePage/RecipePage"; diff --git a/src/app/robots.ts b/src/app/robots.ts index 4d3fd5e..0d1e20c 100644 --- a/src/app/robots.ts +++ b/src/app/robots.ts @@ -1,5 +1,5 @@ import { MetadataRoute } from "next"; -import { siteUrl } from "./(site)/shared-metadata"; +import { siteUrl } from "./shared-metadata"; export default function robots(): MetadataRoute.Robots { return { diff --git a/src/app/(site)/shared-metadata.ts b/src/app/shared-metadata.ts similarity index 100% rename from src/app/(site)/shared-metadata.ts rename to src/app/shared-metadata.ts diff --git a/src/app/(site)/sitemap.ts b/src/app/sitemap.ts similarity index 77% rename from src/app/(site)/sitemap.ts rename to src/app/sitemap.ts index 0354291..cb9b428 100644 --- a/src/app/(site)/sitemap.ts +++ b/src/app/sitemap.ts @@ -6,14 +6,15 @@ import { import { ArrayElement } from "@/utils/types"; import { MetadataRoute } from "next"; import { siteUrl } from "./shared-metadata"; -import { sanityFetchNonLive } from "@/sanity/lib/client"; +import { sanityFetch } from "@/sanity/lib/live"; export default async function sitemap(): Promise { - const [recipes, home, about] = await Promise.all([ - sanityFetchNonLive({ query: recipesSitemapQuery, revalidate: 60 * 60 }), - sanityFetchNonLive({ query: homeSitemapQuery, revalidate: 60 * 60 }), - sanityFetchNonLive({ query: aboutSitemapQuery, revalidate: 60 * 60 }), - ]); + const [{ data: recipes }, { data: home }, { data: about }] = + await Promise.all([ + sanityFetch({ query: recipesSitemapQuery, stega: false }), + sanityFetch({ query: homeSitemapQuery, stega: false }), + sanityFetch({ query: aboutSitemapQuery, stega: false }), + ]); const mappedRecipes = recipes .map | null>((recipe) => { diff --git a/src/components/pages/RecipePage/RecipePage.tsx b/src/components/pages/RecipePage/RecipePage.tsx index cc7ba7d..81541c5 100644 --- a/src/components/pages/RecipePage/RecipePage.tsx +++ b/src/components/pages/RecipePage/RecipePage.tsx @@ -6,7 +6,7 @@ import type { Recipe as RecipeSchema, WithContext } from "schema-dts"; import { JsonLd } from "@/components/JsonLd/JsonLd"; import { formatDurationISO } from "@/utils/recipeUtils"; import { RecipeQueryResult } from "../../../../sanity.types"; -import { creator, siteUrl } from "@/app/(site)/shared-metadata"; +import { creator, siteUrl } from "@/app/shared-metadata"; type RecipePageProps = { params: { slug: string }; diff --git a/src/sanity/lib/client.ts b/src/sanity/lib/client.ts index 539e988..59a24b9 100644 --- a/src/sanity/lib/client.ts +++ b/src/sanity/lib/client.ts @@ -1,4 +1,4 @@ -import { createClient, QueryParams } from "next-sanity"; +import { createClient } from "next-sanity"; import { apiVersion, dataset, projectId, studioUrl } from "../env"; export const client = createClient({ @@ -6,24 +6,6 @@ export const client = createClient({ dataset, apiVersion, useCdn: true, + perspective: "published", stega: { studioUrl }, }); - -export async function sanityFetchNonLive({ - query, - params = {}, - revalidate = 60, // default revalidation time in seconds - tags = [], -}: { - query: QueryString; - params?: QueryParams; - revalidate?: number | false; - tags?: string[]; -}) { - return client.fetch(query, params, { - next: { - revalidate: tags.length ? false : revalidate, // for simple, time-based revalidation - tags, // for tag-based revalidation - }, - }); -}