Merge pull request #3 from kodamera/feature/on-demand-revalidation

Feature/on demand revalidation
This commit is contained in:
Henrik Larsson 2023-07-03 14:41:01 +02:00 committed by GitHub
commit 619eddffc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 926 additions and 669 deletions

View File

@ -51,9 +51,6 @@ export default async function Page({ params }: { params: { slug: string[]; local
);
}
// Background revalidate once every day.
export const revalidate = 86400;
/**
* Get paths for each page.
*/

View File

@ -0,0 +1,43 @@
import { isValidSignature, SIGNATURE_HEADER_NAME } from '@sanity/webhook';
import { revalidatePath } from 'next/cache';
import { headers } from 'next/headers';
import { NextRequest, NextResponse } from 'next/server';
const SANITY_WEBHOOK_SECRET = `${process.env.SANITY_WEBHOOK_SECRET}`;
export async function POST(request: NextRequest) {
// Await the response from our request.
const requestData = await request.json();
// Get headers.
const headersList = headers();
// Get Sanity webhook signature header name.
const signature = `${headersList.get(SIGNATURE_HEADER_NAME)}`;
const isValid = isValidSignature(JSON.stringify(requestData), signature, SANITY_WEBHOOK_SECRET);
// Log out validity of request.
console.log(`===== Is the webhook request valid? ${isValid}`);
// If not valid, return.
if (!isValid) {
NextResponse.json({ success: false, message: 'Invalid signature' });
return;
}
const slug = requestData.slug;
const locale = requestData.locale;
const type = requestData.type;
let pathToRevalidate = "";
if (type === "home") {
pathToRevalidate = `${slug}`
} else {
pathToRevalidate = `/${locale}${slug}`
}
revalidatePath(pathToRevalidate);
console.log(`===== Revalidated path: ${pathToRevalidate}`);
return NextResponse.json({ revalidated: true, now: Date.now() });
}

View File

@ -9,7 +9,7 @@ export const client = createClient({
projectId,
dataset,
apiVersion,
useCdn: true,
useCdn: false,
});
export const clientFetch = cache(client.fetch.bind(client));

View File

@ -23,53 +23,55 @@
"*": "prettier --write --ignore-unknown"
},
"dependencies": {
"@headlessui/react": "^1.7.10",
"@next/bundle-analyzer": "^13.3.4",
"@headlessui/react": "^1.7.14",
"@next/bundle-analyzer": "^13.4.3",
"@portabletext/react": "^3.0.0",
"@radix-ui/react-dropdown-menu": "^2.0.4",
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/react-navigation-menu": "^1.1.2",
"@sanity/icons": "2",
"@sanity/icons": "^2.3.1",
"@sanity/image-url": "^1.0.2",
"@sanity/types": "3",
"@sanity/ui": "1",
"@types/styled-components": "^5.1",
"@sanity/types": "^3.11.1",
"@sanity/ui": "^1.3.3",
"@sanity/webhook": "^2.0.0",
"@types/styled-components": "^5.1.26",
"@vercel/og": "^0.1.0",
"class-variance-authority": "^0.6.0",
"clsx": "^1.2.1",
"framer-motion": "^8.4.0",
"framer-motion": "^8.5.5",
"is-empty-iterable": "^3.0.0",
"lucide-react": "^0.194.0",
"next": "13.4.3",
"next-intl": "^2.13.1",
"next-sanity": "^4.2.0",
"next": "13.4.7",
"next-intl": "^2.14.6",
"next-sanity": "^4.3.2",
"react": "18.2.0",
"react-cookie": "^4.1.1",
"react-dom": "18.2.0",
"react-glider": "^4.0.2",
"sanity": "3",
"styled-components": "^5.2",
"sanity": "^3.11.1",
"sharp": "^0.32.1",
"styled-components": "^5.3.10",
"tailwind-merge": "^1.12.0",
"tailwindcss-animate": "^1.0.5"
},
"devDependencies": {
"@playwright/test": "^1.31.2",
"@playwright/test": "^1.34.1",
"@tailwindcss/typography": "^0.5.9",
"@types/negotiator": "^0.6.1",
"@types/node": "18.13.0",
"@types/react": "18.0.27",
"@types/react-dom": "18.0.10",
"@vercel/git-hooks": "^1.0.0",
"autoprefixer": "^10.4.13",
"eslint": "^8.35.0",
"eslint-config-next": "^13.4.0",
"eslint-config-prettier": "^8.6.0",
"autoprefixer": "^10.4.14",
"eslint": "^8.41.0",
"eslint-config-next": "^13.4.3",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-unicorn": "^45.0.2",
"lint-staged": "^13.1.1",
"postcss": "^8.4.21",
"prettier": "^2.8.7",
"prettier-plugin-tailwindcss": "^0.2.7",
"tailwindcss": "^3.2.6",
"lint-staged": "^13.2.2",
"postcss": "^8.4.23",
"prettier": "^2.8.8",
"prettier-plugin-tailwindcss": "^0.2.8",
"tailwindcss": "^3.3.2",
"typescript": "4.9.5"
}
}

1501
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff