Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

add revalidation to link update

Marc Seitzde37ef799f02
diff --git a/pages/api/links/[id]/index.ts b/pages/api/links/[id]/index.ts
index 96d061f5..15d5caaa 100644
--- a/pages/api/links/[id]/index.ts
+++ b/pages/api/links/[id]/index.ts
@@ -146,6 +146,10 @@ export default async function handle(
return res.status(404).json({ error: "Link not found" });
}
+ await fetch(
+ `${process.env.NEXTAUTH_URL}/api/revalidate?secret=${process.env.REVALIDATE_TOKEN}&linkId=${id}`,
+ );
+
return res.status(200).json(updatedLink);
} else if (req.method == "DELETE") {
// DELETE /api/links/:id
diff --git a/pages/api/revalidate.ts b/pages/api/revalidate.ts
new file mode 100644
index 00000000..f96328e0
--- /dev/null
+++ b/pages/api/revalidate.ts
@@ -0,0 +1,28 @@
+import { NextApiRequest, NextApiResponse } from "next";
+
+export default async function handler(
+ req: NextApiRequest,
+ res: NextApiResponse,
+) {
+ if (req.method !== "GET") {
+ return res.status(405).json({ message: "Method not allowed" });
+ }
+
+ // Check for secret to confirm this is a valid request
+ if (req.query.secret !== process.env.REVALIDATE_TOKEN) {
+ return res.status(401).json({ message: "Invalid token" });
+ }
+
+ const { linkId } = req.query as { linkId: string };
+
+ try {
+ // this should be the actual path not a rewritten path
+ // e.g. for "/blog/[slug]" this should be "/blog/post-1"
+ await res.revalidate(`/view/${linkId}`);
+ return res.json({ revalidated: true });
+ } catch (err) {
+ // If there was an error, Next.js will continue
+ // to show the last successfully generated page
+ return res.status(500).send("Error revalidating");
+ }
+}