Skip to content

Commit

Permalink
Calling revalidation directly as a server action and changing reload …
Browse files Browse the repository at this point in the history
…action
  • Loading branch information
xaviemirmon committed Apr 29, 2024
1 parent af48b78 commit 086665c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 34 deletions.
2 changes: 1 addition & 1 deletion frontend/starters/development/app/api/revalidate/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function handler(request: NextRequest) {
}

try {
revalidatePath(path, "layout")
revalidatePath(path)

return new Response("Revalidated.")
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import config from "../../../puck.config"
import { drupal } from "@/lib/drupal"
import { drupalFieldPrefix } from "@powerstack/utils"
import { toast } from "sonner"
import { useRouter } from "next/navigation"
import { triggerRevalidation } from "@/lib/trigger-revalidation"

export function Client({ path, data }: { path: string; data: Data }) {
const router = useRouter()

return (
<Puck
Expand Down Expand Up @@ -113,13 +111,13 @@ export function Client({ path, data }: { path: string; data: Data }) {
},
}
)
triggerRevalidation(path)
await triggerRevalidation(path)
toast.success(`Published ${data.root?.props?.title}`, {
action: {
label: "View",
onClick: () => router.push(path),
onClick: () => window.location.href = path,
},
duration: 5000,
duration: 10000,
})
} catch (error) {
console.error("Error processing page:", error)
Expand Down
42 changes: 14 additions & 28 deletions frontend/starters/development/lib/trigger-revalidation.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,21 @@
"use server"

import { revalidatePath } from 'next/cache';

export const triggerRevalidation = async (path) => {
const secret = process.env.NEXT_REVALIDATE_SECRET
const baseUrl = process.env.NEXT_HOST // This should ideally be kept secure
const revalidateUrl = `${baseUrl}/api/revalidate?path=${path}&secret=${secret}`
const revalidateUrl = `/${path}`;
const revalidateEditUrl = `/edit/[...puckPath]`;

try {
const response = await fetch(revalidateUrl, {
method: "GET",
})
const result = await response
if (result.ok) {
console.log("Page revalidated successfully")
} else {
console.error("Failed to revalidate")
}
} catch (error) {
console.error("Error triggering revalidation:", error)
}
const revalidateEditUrl = `${baseUrl}/api/revalidate?path=/edit/[...puckPath]/page&secret=${secret}`
try {
const response = await fetch(revalidateEditUrl, {
method: "GET",
})
const result = await response
if (result.ok) {
console.log("Edit page revalidated successfully")
} else {
console.error("Failed to revalidate")
}
await revalidatePath(revalidateUrl, 'page'); // Assuming revalidatePath is an async operation
console.log("Page revalidated");

await revalidatePath(revalidateEditUrl, 'page');
console.log("Edit page revalidated");

return "Both pages revalidated successfully"; // Resolving the promise with a success message
} catch (error) {
console.error("Error triggering revalidation:", error)
console.error("Error triggering revalidation:", error);
throw error; // Rejecting the promise with the caught error
}
}
};

0 comments on commit 086665c

Please sign in to comment.