Skip to content

Commit

Permalink
Live for everything. Fix sitemap.
Browse files Browse the repository at this point in the history
  • Loading branch information
dagstuan committed Nov 10, 2024
1 parent f725a39 commit 2836d48
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 42 deletions.
23 changes: 10 additions & 13 deletions src/app/(site)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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<Metadata> {
const homeSeo = await getSeoData();
const { data: homeSeo } = await sanityFetch({
query: homeSeoQuery,
stega: false,
});

const metaDescription = homeSeo?.seo?.metaDescription ?? "";

Expand Down Expand Up @@ -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<WebSite> = {
"@context": "https://schema.org",
Expand Down
2 changes: 1 addition & 1 deletion src/app/(site)/oppskrifter/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
2 changes: 1 addition & 1 deletion src/app/robots.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
File renamed without changes.
13 changes: 7 additions & 6 deletions src/app/(site)/sitemap.ts → src/app/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MetadataRoute.Sitemap> {
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<ArrayElement<MetadataRoute.Sitemap> | null>((recipe) => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/pages/RecipePage/RecipePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
22 changes: 2 additions & 20 deletions src/sanity/lib/client.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
import { createClient, QueryParams } from "next-sanity";
import { createClient } from "next-sanity";
import { apiVersion, dataset, projectId, studioUrl } from "../env";

export const client = createClient({
projectId,
dataset,
apiVersion,
useCdn: true,
perspective: "published",
stega: { studioUrl },
});

export async function sanityFetchNonLive<const QueryString extends string>({
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
},
});
}

0 comments on commit 2836d48

Please sign in to comment.