Skip to content

Commit

Permalink
refactor: add revalidatePath support to revalidate endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgoff committed Sep 26, 2024
1 parent 74cdbf7 commit 0c8a24b
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 26 deletions.
37 changes: 37 additions & 0 deletions app/api/app-revalidate/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { revalidatePath } from "next/cache";
import { NextRequest, NextResponse } from "next/server";

const REVALIDATE_SECRET_TOKEN = process.env.CRAFT_REVALIDATE_SECRET_TOKEN;

export async function GET(request: NextRequest): Promise<NextResponse> {
const uri = request.nextUrl.searchParams.get("uri");
const secret = request.nextUrl.searchParams.get("secret");

if (!uri) {
return NextResponse.json({
revalidated: false,
now: Date.now(),
message: "Missing path to revalidate",
});
}

if (secret !== REVALIDATE_SECRET_TOKEN) {
return NextResponse.json({
revalidated: false,
now: Date.now(),
message: "Invalid token",
});
}

if (uri) {
revalidatePath(`/[locale]/${uri}`, "page");

return NextResponse.json({ revalidated: true, now: Date.now() });
}

return NextResponse.json({
revalidated: false,
now: Date.now(),
message: "Error revalidating",
});
}
3 changes: 2 additions & 1 deletion next-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
/// <reference types="next/navigation-types/compat/navigation" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information.
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
13 changes: 12 additions & 1 deletion pages/api/revalidate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
/* eslint-disable no-console */
import { isCraftPreview } from "@/helpers";

const REVALIDATE_SECRET_TOKEN = process.env.CRAFT_REVALIDATE_SECRET_TOKEN;
const BASE_URL = process.env.NEXT_PUBLIC_BASE_URL;

/**
* @function preview
* @param {import("next").NextApiRequest} req
* @param {import("next").NextApiResponse} res
*/
async function handler(req, res) {
const { query } = req;
const isPreview = isCraftPreview(query);
Expand All @@ -12,7 +20,7 @@ async function handler(req, res) {
});
}

if (query.secret !== process.env.CRAFT_REVALIDATE_SECRET_TOKEN) {
if (query.secret !== REVALIDATE_SECRET_TOKEN) {
console.warn("Invalid token");
return res.status(401).json({ error: "Invalid token" });
}
Expand All @@ -23,6 +31,9 @@ async function handler(req, res) {
}

try {
const searchParams = new URLSearchParams(query);

await fetch(`${BASE_URL}/api/app-revalidate?${searchParams.toString()}`);
await res.revalidate(`/${query.uri}`);
return res.status(200).json({ revalidated: true });
} catch (error) {
Expand Down
101 changes: 77 additions & 24 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"compilerOptions": {
"lib": ["dom", "dom.iterable", "esnext"],
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
Expand All @@ -20,29 +24,78 @@
"baseUrl": ".",
"strictNullChecks": true,
"paths": {
"@/lib/*": ["lib/*"],
"@/api/*": ["lib/api/*"],
"@/charts/*": ["charts/*"],
"@/components/*": ["components/*"],
"@/content-blocks": ["components/content-blocks"],
"@/dynamic/*": ["components/dynamic/*"],
"@/factories/*": ["components/factories/*"],
"@/atomic/*": ["components/atomic/*"],
"@/layout/*": ["components/layout/*"],
"@/templates/*": ["components/templates/*"],
"@/global/*": ["components/global/*"],
"@/page/*": ["components/page/*"],
"@/shapes/*": ["components/shapes/*"],
"@/hooks/*": ["hooks/*"],
"@/hooks": ["hooks"],
"@/contexts/*": ["contexts/*"],
"@/styles/*": ["theme/styles/*"],
"@/svg/*": ["components/svg/*"],
"@/helpers": ["helpers"],
"@/helpers/*": ["helpers/*"],
"@/hoc/*": ["components/hoc/*"]
"@/lib/*": [
"lib/*"
],
"@/api/*": [
"lib/api/*"
],
"@/charts/*": [
"charts/*"
],
"@/components/*": [
"components/*"
],
"@/content-blocks": [
"components/content-blocks"
],
"@/dynamic/*": [
"components/dynamic/*"
],
"@/factories/*": [
"components/factories/*"
],
"@/atomic/*": [
"components/atomic/*"
],
"@/layout/*": [
"components/layout/*"
],
"@/templates/*": [
"components/templates/*"
],
"@/global/*": [
"components/global/*"
],
"@/page/*": [
"components/page/*"
],
"@/shapes/*": [
"components/shapes/*"
],
"@/hooks/*": [
"hooks/*"
],
"@/hooks": [
"hooks"
],
"@/contexts/*": [
"contexts/*"
],
"@/styles/*": [
"theme/styles/*"
],
"@/svg/*": [
"components/svg/*"
],
"@/helpers": [
"helpers"
],
"@/helpers/*": [
"helpers/*"
],
"@/hoc/*": [
"components/hoc/*"
]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
],
"exclude": [
"node_modules"
]
}

0 comments on commit 0c8a24b

Please sign in to comment.