Test revalidation

This commit is contained in:
Henrik Larsson 2023-08-24 07:43:00 +02:00
parent a61d140ef3
commit 3f0bd3e08a
3 changed files with 95 additions and 32 deletions

View File

@ -0,0 +1,64 @@
/**
* This code is responsible for revalidating queries as the dataset is updated.
*
* It is set up to receive a validated GROQ-powered Webhook from Sanity.io:
* https://www.sanity.io/docs/webhooks
*
* 1. Go to the API section of your Sanity project on sanity.io/manage or run `npx sanity hook create`
* 2. Click "Create webhook"
* 3. Set the URL to https://YOUR_NEXTJS_SITE_URL/api/revalidate
* 4. Dataset: Choose desired dataset or leave at default "all datasets"
* 5. Trigger on: "Create", "Update", and "Delete"
* 6. Filter: Leave empty
* 7. Projection: {_type, "slug": slug.current}
* 8. Status: Enable webhook
* 9. HTTP method: POST
* 10. HTTP Headers: Leave empty
* 11. API version: v2021-03-25
* 12. Include drafts: No
* 13. Secret: Set to the same value as SANITY_REVALIDATE_SECRET (create a random secret if you haven't yet, for example by running `Math.random().toString(36).slice(2)` in your console)
* 14. Save the cofiguration
* 15. Add the secret to Vercel: `npx vercel env add SANITY_REVALIDATE_SECRET`
* 16. Redeploy with `npx vercel --prod` to apply the new environment variable
*/
import { revalidateSecret } from '@/lib/sanity/sanity.api'
import { parseBody } from 'next-sanity/webhook'
import { revalidateTag } from 'next/cache'
import { NextResponse, type NextRequest } from 'next/server'
export async function POST(req: NextRequest) {
try {
const { body, isValidSignature } = await parseBody<{
_type: string
slug?: string | undefined
language: string | undefined
}>(req, revalidateSecret)
if (!isValidSignature) {
const message = 'Invalid signature'
return new Response(message, { status: 401 })
}
if (!body?._type) {
return new Response('Bad Request', { status: 400 })
}
revalidateTag(body._type)
if (body.slug) {
revalidateTag(`${body._type}:${body.slug}`)
} else {
revalidateTag(`${body._type}`)
}
return NextResponse.json({
status: 200,
revalidated: true,
now: Date.now(),
body,
})
} catch (err: any) {
console.error(err)
return new Response(err.message, { status: 500 })
}
}

View File

@ -56,7 +56,7 @@ export function getHomePage(locale: string) {
return sanityFetch<HomePagePayload | null>({ return sanityFetch<HomePagePayload | null>({
query: homePageQuery, query: homePageQuery,
params: { locale }, params: { locale },
tags: ['home', 'products', 'categories', 'page', 'menu'], tags: ['home', 'product', 'category', 'page', 'menu'],
}) })
} }

View File

@ -1,35 +1,34 @@
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
const withBundleAnalyzer = require('@next/bundle-analyzer')({ const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.BUNDLE_ANALYZE === 'true', enabled: process.env.BUNDLE_ANALYZE === 'true'
}) });
module.exports = withBundleAnalyzer( module.exports = withBundleAnalyzer({
{ async rewrites() {
async rewrites() { return [
return [ {
{ source: '/en/search',
source: '/en/search', destination: '/en/sok',
destination: '/en/sok', locale: false
locale: false }
}, ];
] },
}, eslint: {
eslint: { // Disabling on production builds because we're running checks on PRs via GitHub Actions.
// Disabling on production builds because we're running checks on PRs via GitHub Actions. ignoreDuringBuilds: true
ignoreDuringBuilds: true },
}, experimental: {
experimental: { scrollRestoration: true,
scrollRestoration: true, serverActions: true,
serverActions: true, logging: 'verbose'
}, },
images: { images: {
formats: ['image/avif', 'image/webp'], formats: ['image/avif', 'image/webp'],
remotePatterns: [ remotePatterns: [
{ {
protocol: 'https', protocol: 'https',
hostname: 'cdn.sanity.io', hostname: 'cdn.sanity.io'
}, }
], ]
},
} }
); });