From 3f0bd3e08aa687c5a1e919039ae6e7e9b7a70c29 Mon Sep 17 00:00:00 2001 From: Henrik Larsson Date: Thu, 24 Aug 2023 07:43:00 +0200 Subject: [PATCH] Test revalidation --- app/api/revalidate/sanity/route.ts | 64 ++++++++++++++++++++++++++++++ lib/sanity/sanity.fetch.ts | 2 +- next.config.js | 61 ++++++++++++++-------------- 3 files changed, 95 insertions(+), 32 deletions(-) create mode 100644 app/api/revalidate/sanity/route.ts diff --git a/app/api/revalidate/sanity/route.ts b/app/api/revalidate/sanity/route.ts new file mode 100644 index 000000000..91743a2d6 --- /dev/null +++ b/app/api/revalidate/sanity/route.ts @@ -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 }) + } +} \ No newline at end of file diff --git a/lib/sanity/sanity.fetch.ts b/lib/sanity/sanity.fetch.ts index 8365263e5..ce49c8a9f 100644 --- a/lib/sanity/sanity.fetch.ts +++ b/lib/sanity/sanity.fetch.ts @@ -56,7 +56,7 @@ export function getHomePage(locale: string) { return sanityFetch({ query: homePageQuery, params: { locale }, - tags: ['home', 'products', 'categories', 'page', 'menu'], + tags: ['home', 'product', 'category', 'page', 'menu'], }) } diff --git a/next.config.js b/next.config.js index 41dddf6e0..a90e07509 100644 --- a/next.config.js +++ b/next.config.js @@ -1,35 +1,34 @@ /** @type {import('next').NextConfig} */ const withBundleAnalyzer = require('@next/bundle-analyzer')({ - enabled: process.env.BUNDLE_ANALYZE === 'true', -}) + enabled: process.env.BUNDLE_ANALYZE === 'true' +}); -module.exports = withBundleAnalyzer( - { - async rewrites() { - return [ - { - source: '/en/search', - destination: '/en/sok', - locale: false - }, - ] - }, - eslint: { - // Disabling on production builds because we're running checks on PRs via GitHub Actions. - ignoreDuringBuilds: true - }, - experimental: { - scrollRestoration: true, - serverActions: true, - }, - images: { - formats: ['image/avif', 'image/webp'], - remotePatterns: [ - { - protocol: 'https', - hostname: 'cdn.sanity.io', - }, - ], - }, +module.exports = withBundleAnalyzer({ + async rewrites() { + return [ + { + source: '/en/search', + destination: '/en/sok', + locale: false + } + ]; + }, + eslint: { + // Disabling on production builds because we're running checks on PRs via GitHub Actions. + ignoreDuringBuilds: true + }, + experimental: { + scrollRestoration: true, + serverActions: true, + logging: 'verbose' + }, + images: { + formats: ['image/avif', 'image/webp'], + remotePatterns: [ + { + protocol: 'https', + hostname: 'cdn.sanity.io' + } + ] } -); +});