Sanity Webhooks and On-demand Revalidation in Nextjs #75
Replies: 10 comments 7 replies
-
Thanks for sharing this! 🙌 I have a question. Does revalidating the tag purge all caches related to that tag? For instance, if I have I'm facing an issue where the projects page isn't updating, whereas the project page updates immediately. Any insights? |
Beta Was this translation helpful? Give feedback.
-
It doesn't update at all unless I rebuild. Let me provide more context: // Sanity fetch function
const DEFAULT_PARAMS = {} as QueryParams;
const DEFAULT_TAGS = [] as string[];
export const sanityFetch = async <QueryResponse>({
query,
params = DEFAULT_PARAMS,
tags = DEFAULT_TAGS,
}: {
query: string;
params?: QueryParams;
tags: string[];
}): Promise<QueryResponse> => {
return client.fetch<QueryResponse>(query, params, {
cache: process.env.NODE_ENV === "development" ? "no-store" : "force-cache",
next: { tags },
});
}; // Get all projects
export const getProjects = () => {
return sanityFetch<ProjectType[] | null>({
query: projectsGroq,
tags: ["project"],
});
}; // Projects page.tsx
const Project = async () => {
const projects = await getProjects();
return (...component)
} |
Beta Was this translation helpful? Give feedback.
-
I followed your suggested approach, and it seems to be working now. However, I have noticed that the setup behaves somewhat unpredictably. It works on some occasions but doesn't when it decides not to – almost like it has a mind of its own lol. Thank you for helping 🙌 |
Beta Was this translation helpful? Give feedback.
-
I have a question, how do you present the code in your Portabletext, I am trying to display it but it's not appearing on the front-end. import Image from "next/image";
import Link from "next/link";
import urlFor from "@/sanity/lib/urlFor";
import SyntaxHighlighter from 'react-syntax-highlighter';
import { docco } from 'react-syntax-highlighter/dist/esm/styles/hljs';
// Modified Code component to include syntax highlighting
const Code = ({ children, className }: any) => {
return (
<pre className={className}>
<SyntaxHighlighter language="javascript" style={docco}>
{children}
</SyntaxHighlighter>
</pre>
);
};
export const RichTextComponents = {
types: {
code: Code,
image: ({ value }: any) => {
return (
<div className="relative min-h-[16rem] md:h-96 w-full my-5 mx-auto">
<Image src={urlFor(value).url()} alt="Blog post image." fill className="object-cover lg:object-center rounded-sm md:rounded-md" />
</div>
);
},
list: {
// Ex. 1: customizing common list types
bullet: ({ children }: any) => <ul className="list-disc text-lg space-y-4 mb-2 ml-5">{children}</ul>,
number: ({ children }: any) => <ol className="mt-lg">{children}</ol>,
// Ex. 2: rendering custom lists
checkmarks: ({ children }: any) => <ol className="m-auto text-lg">{children}</ol>,
},
block: {
h1: ({ children }: any) => (
<h1 className="text-5xl py-10 font-bold">{children}</h1>
),
h2: ({ children }: any) => (
<h2 className="text-4xl py-2 font-bold text-white">{children}</h2>
),
h3: ({ children }: any) => (
<h3 className="text-3xl py-2 font-bold text-white">{children}</h3>
),
h4: ({ children }: any) => (
<h4 className="text-2xl py-2 font-bold text-white">{children}</h4>
),
h5: ({ children }: any) => (
<h5 className="text-xl py-2 font-bold text-white">{children}</h5>
),
h6: ({ children }: any) => (
<h6 className="text-lg py-2 font-bold text-white">{children}</h6>
),
normal: ({ children }: any) => <p className="text-base py-2">{children}</p>,
blockquote: ({ children }: any) => (
<blockquote className=" border-l-blue-600 text-xl border-l-4 pl-5 py-5 my-5">
{children}
</blockquote>
),
},
marks: {
link: ({ children, value }: any) => {
const rel = !value.href.startsWith("/")
? "noreferrer noopener"
: undefined;
return (
<Link
href={value.href}
rel={rel}
className="underline decoration-blue-600 hover:decoration-blackF"
>
{children}
</Link>
);
},
},
},
}; |
Beta Was this translation helpful? Give feedback.
-
Thank you so much. Let me have a look.
…On Mon, 25 Mar 2024 at 18:21, Victor Eke ***@***.***> wrote:
Check out my CodeBlock
<https://github.com/Evavic44/victoreke.com/blob/main/app/components/shared/CodeBlock.tsx>
component to see how I did it.
I used Refractor <https://www.npmjs.com/package/react-refractor> for
syntax highlighting, it uses Prism under the hood which provides a lot of
themes <https://github.com/PrismJS/prism/tree/master/themes> you can
choose from. I customized my own theme with CSS. You can find the styles in
the prism.css
<https://github.com/Evavic44/victoreke.com/blob/main/app/styles/prism.css>
file
—
Reply to this email directly, view it on GitHub
<#75 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AX7A5BL2HKFXYTFFRWBRGRTY2A6IJAVCNFSM6AAAAABAZXLIXKVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4DSMBUGE2TC>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Hello! Thank you so much for this write up. It saved me a lot of banging my head against the wall. :-) However... Everything is working as expected and all content refreshes almost immediately, except on a nested dynamic route: Content updates on the same document are reflected in the E.g., If a document is titled "A Jade Praying Mantis Pendant" and I change it in the sanity studio to "A Jade Praying Mantis Pendant EDITED" and publish. The new title shows up on the link at the The
I'm stumped on why it's revalidating properly on one route but not the other. Any ideas? |
Beta Was this translation helpful? Give feedback.
-
Hi @victorekea, how you identify if is an update / delete or a new publish? |
Beta Was this translation helpful? Give feedback.
-
Thank you so much for this! |
Beta Was this translation helpful? Give feedback.
-
Hello! Great tutorial, i have gotten it to work on one of my apps. But i have a problem when using a catchall route, has someone got any ideas why this happens. im using app/[...slug]/page.tsx structure for my pages, which fetch the page itself which is of type "page" and some other stuff for nested pages. But when updating something the catchall routes become 404 routes, and i get 404 fetch errors when trying to access them in the console. |
Beta Was this translation helpful? Give feedback.
-
Sanity Webhooks and On-demand Revalidation in Nextjs
Learn how to set up and trigger content updates in your Sanity Headless CMS site built in Nextjs using on-demand revalidation and GROQ-powered webhooks for delivering fast and non-cached content updates
https://victoreke.com/blog/sanity-webhooks-and-on-demand-revalidation-in-nextjs
Beta Was this translation helpful? Give feedback.
All reactions