Skip to content

Commit 75363af

Browse files
Feature/enhance inline documentation (#85)
* Improve inline documentation * npm add @sanity/client
1 parent 8f9945f commit 75363af

27 files changed

+19888
-50
lines changed

nextjs-app/app/[slug]/page.tsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Metadata } from "next";
22
import Head from "next/head";
3-
import { notFound } from "next/navigation";
43

54
import PageBuilderPage from "@/app/components/PageBuilder";
65
import { sanityFetch } from "@/sanity/lib/live";
@@ -12,20 +11,30 @@ type Props = {
1211
params: Promise<{ slug: string }>;
1312
};
1413

14+
/**
15+
* Generate the static params for the page.
16+
* Learn more: https://nextjs.org/docs/app/api-reference/functions/generate-static-params
17+
*/
1518
export async function generateStaticParams() {
1619
const { data } = await sanityFetch({
1720
query: pagesSlugs,
21+
// // Use the published perspective in generateStaticParams
1822
perspective: "published",
1923
stega: false,
2024
});
2125
return data;
2226
}
2327

28+
/**
29+
* Generate metadata for the page.
30+
* Learn more: https://nextjs.org/docs/app/api-reference/functions/generate-metadata#generatemetadata-function
31+
*/
2432
export async function generateMetadata(props: Props): Promise<Metadata> {
2533
const params = await props.params;
2634
const { data: page } = await sanityFetch({
2735
query: getPageQuery,
2836
params,
37+
// Metadata should never contain stega
2938
stega: false,
3039
});
3140

nextjs-app/app/api/draft-mode/enable/route.ts

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import { defineEnableDraftMode } from "next-sanity/draft-mode";
33
import { client } from "@/sanity/lib/client";
44
import { token } from "@/sanity/lib/token";
55

6+
/**
7+
* defineEnableDraftMode() is used to enable draft mode. Set the route of this file
8+
* as the previewMode.enable option for presentationTool in your sanity.config.ts
9+
* Learn more: https://github.com/sanity-io/next-sanity?tab=readme-ov-file#5-integrating-with-sanity-presentation-tool--visual-editing
10+
*/
11+
612
export const { GET } = defineEnableDraftMode({
713
client: client.withConfig({ token }),
814
});

nextjs-app/app/client-utils.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { toast } from "sonner";
55

66
export function handleError(error: unknown) {
77
if (isCorsOriginError(error)) {
8+
// If the error is a CORS origin error, lets display that specific error.
89
const { addOriginUrl } = error;
910
toast.error(`Sanity Live couldn't connect`, {
1011
description: `Your origin is blocked by CORS policy`,

nextjs-app/app/components/BlockRenderer.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ const Blocks: BlocksType = {
2222
infoSection: Info,
2323
};
2424

25+
/**
26+
* Used by the <PageBuilder>, this component renders a the component that matches the block type.
27+
*/
2528
export default function BlockRenderer({ block, index }: BlockProps) {
2629
// Block does exist
2730
if (typeof Blocks[block._type] !== "undefined") {

nextjs-app/app/components/Onboarding.tsx

-15
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,10 @@
66
*/
77

88
import Link from "next/link";
9-
import { useSyncExternalStore } from "react";
109

1110
import { studioUrl } from "@/sanity/lib/api";
1211

13-
const emptySubscribe = () => () => {};
14-
1512
export default function Onboarding() {
16-
const target = useSyncExternalStore(
17-
emptySubscribe,
18-
() => (window.top === window ? undefined : "_blank"),
19-
() => "_blank"
20-
);
21-
2213
return (
2314
<div className="max-w-2xl mx-auto grid grid-flow-row gap-6 py-12 text-center bg-red-500 text-white rounded-lg p-8">
2415
<svg
@@ -120,12 +111,6 @@ const OnboardingMessage = ({ message, link }: OnboardingMessageProps) => {
120111
};
121112

122113
export function PageOnboarding() {
123-
const target = useSyncExternalStore(
124-
emptySubscribe,
125-
() => (window.top === window ? undefined : "_blank"),
126-
() => "_blank"
127-
);
128-
129114
return (
130115
<div className="max-w-2xl mx-auto grid grid-flow-row gap-6 py-12 text-center bg-red-500 text-white rounded-lg p-8">
131116
<svg

nextjs-app/app/components/PageBuilder.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ type PageBuilderPageProps = {
88
page: Page;
99
};
1010

11+
/**
12+
* The PageBuilder component is used to render the blocks from the `pageBuilder` field in the Page type in your Sanity Studio.
13+
*/
1114
export default function PageBuilder({ page }: PageBuilderPageProps) {
1215
if (page?.pageBuilder && page.pageBuilder.length > 0) {
1316
return (

nextjs-app/app/components/ResolvedLink.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default function ResolvedLink({
1313
children,
1414
className,
1515
}: ResolvedLinkProps) {
16+
// resolveLink() is used to determine the type of link and return the appropriate URL.
1617
const resolvedLink = linkResolver(link);
1718

1819
if (typeof resolvedLink === "string") {

nextjs-app/app/layout.tsx

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import { settingsQuery } from "@/sanity/lib/queries";
1616
import { resolveOpenGraphImage } from "@/sanity/lib/utils";
1717
import { handleError } from "./client-utils";
1818

19+
/**
20+
* Generate metadata for the page.
21+
* Learn more: https://nextjs.org/docs/app/api-reference/functions/generate-metadata#generatemetadata-function
22+
*/
1923
export async function generateMetadata(): Promise<Metadata> {
2024
const { data: settings } = await sanityFetch({
2125
query: settingsQuery,
@@ -64,13 +68,16 @@ export default async function RootLayout({
6468
<html lang="en" className={`${inter.variable} bg-white text-black`}>
6569
<body>
6670
<section className="min-h-screen pt-24">
71+
{/* The <Toaster> component is responsible for rendering toast notifications used in /app/client-utils.ts and /app/components/DraftModeToast.tsx */}
6772
<Toaster />
6873
{isDraftMode && (
6974
<>
7075
<DraftModeToast />
76+
{/* Enable Visual Editing, only to be rendered when Draft Mode is enabled */}
7177
<VisualEditing />
7278
</>
7379
)}
80+
{/* The <SanityLive> component is responsible for making all sanityFetch calls in your application live, so should always be rendered. */}
7481
<SanityLive onError={handleError} />
7582
<Header />
7683
<main className="">{children}</main>

nextjs-app/app/posts/[slug]/page.tsx

+10
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,24 @@ type Props = {
1515
params: Promise<{ slug: string }>;
1616
};
1717

18+
/**
19+
* Generate the static params for the page.
20+
* Learn more: https://nextjs.org/docs/app/api-reference/functions/generate-static-params
21+
*/
1822
export async function generateStaticParams() {
1923
const { data } = await sanityFetch({
2024
query: postPagesSlugs,
25+
// Use the published perspective in generateStaticParams
2126
perspective: "published",
2227
stega: false,
2328
});
2429
return data;
2530
}
2631

32+
/**
33+
* Generate metadata for the page.
34+
* Learn more: https://nextjs.org/docs/app/api-reference/functions/generate-metadata#generatemetadata-function
35+
*/
2736
export async function generateMetadata(
2837
props: Props,
2938
parent: ResolvingMetadata
@@ -32,6 +41,7 @@ export async function generateMetadata(
3241
const { data: post } = await sanityFetch({
3342
query: postQuery,
3443
params,
44+
// Metadata should never contain stega
3545
stega: false,
3646
});
3747
const previousImages = (await parent).openGraph?.images || [];

nextjs-app/package-lock.json

+4-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nextjs-app/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"typegen": "sanity typegen generate"
1111
},
1212
"dependencies": {
13+
"@sanity/client": "^6.22.5",
1314
"@sanity/image-url": "^1.1.0",
1415
"@tailwindcss/typography": "^0.5.15",
1516
"@types/node": "^20.14.13",

0 commit comments

Comments
 (0)