"revalidatePath" for Vercel ISR On-Demand Revalidation?
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 20.8k
- Forks
- 2.3k
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 156
Description
Describe the problem
If you check the Vercel docs about on-demand revalidation for ISR, you will see they show this example for Next.js:
import { revalidatePath } from 'next/cache';
export async function GET(request) {
const { searchParams } = new URL(request.url);
if (searchParams?.secret !== process.env.MY_SECRET_TOKEN) {
return new Response(`Invalid credentials`, {
status: 500,
});
}
revalidatePath('/blog-posts');
return new Response(
{
revalidated: true,
now: Date.now(),
},
{
status: 200,
},
);
}
Where they import { revalidatePath } from 'next/cache'; but for the SvelteKit example there is no such function:
import { BYPASS_TOKEN } from '$env/static/private';
export const config = {
isr: {
expiration: 10,
bypassToken: BYPASS_TOKEN,
},
};
I suppose you can manually create a path like src/routes/api/revalidate/+server.ts:
import {error, json, type RequestHandler} from "@sveltejs/kit";
import {isValidSignature, SIGNATURE_HEADER_NAME} from "@sanity/webhook";
import { env } from "$env/dynamic/private";
type ResponseType = {
_id?: string,
slug?: string,
_type?: string,
}
/**
* Revalidates a slug.
*
* @param slug
* The slug to revalidate.
*/
async function revalidateSlug(slug: string): Promise<boolean> {
// Set a delay to allow Sanity data to correctly propagate.
await new Promise(resolve => setTimeout(resolve, 2000));
console.log(`Revalidating: ${env.SITE_URL}/${slug}`);
const res = await fetch(`${env.SITE_URL}/${slug}`, {
cache: 'no-store',
headers: {
'x-prerender-revalidate': env.BYPASS_TOKEN,
}
});
if (!res.ok) {
console.log(`Could not revalidate ${env.SITE_URL}/${slug}`);
}
return res.ok;
}
/** @type {import('./$types').RequestHandler} */
export const POST: RequestHandler = async ({ request }) => {
const signature = request.headers.get(SIGNATURE_HEADER_NAME) ?? '';
const body = await request.clone().json() as ResponseType;
if (!(await isValidSignature(JSON.stringify(body), signature, env.BYPASS_TOKEN))) {
throw error(403, "Forbidden")
}
if (body?.slug) {
const ok = await revalidateSlug(body.slug);
if (body?._type === 'post') {
await revalidateSlug('blog');
}
return json({ok});
}
throw error(404, "Slug not found")
}
(taken from https://www.megamashmedia.com/blog/how-to-setup-on-demand-revalidation-with-sveltekit-sanity-and-vercel)
But I'm wondering if this isn't something SvelteKit already includes like seen in the NextJS example?
Describe the proposed solution
If this doesn't exist in SvelteKit, I'm wondering if it would be a nice to have worth adding?
Alternatives considered
No response
Importance
nice to have
Additional Information
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Vercel ISR documentation and the SvelteKit configuration shown in the issue, then inspect whether SvelteKit exposes a built-in path-invalidation entry point. Compare that with the proposed src/routes/api/revalidate/+server.ts endpoint and determine what tests or documentation would establish the supported behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, next.js, typescript
- Domain
- web-dev
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100