Skip to content

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

fix: new doc version not revalidating links

Marc Seitz91e24ef12c7a
diff --git a/jobs/convert-pdf-to-image.ts b/jobs/convert-pdf-to-image.ts
index dee8f8a0..a23b549a 100644
--- a/jobs/convert-pdf-to-image.ts
+++ b/jobs/convert-pdf-to-image.ts
@@ -138,6 +138,12 @@ client.defineJob({
},
});
});
+
+ await io.runTask("initiate-link-revalidation", async () => {
+ await fetch(
+ `${process.env.NEXTAUTH_URL}/api/revalidate?secret=${process.env.REVALIDATE_TOKEN}&documentId=${documentId}`,
+ );
+ });
}
return {
diff --git a/pages/api/revalidate.ts b/pages/api/revalidate.ts
index f96328e0..751a51e9 100644
--- a/pages/api/revalidate.ts
+++ b/pages/api/revalidate.ts
@@ -1,4 +1,5 @@
import { NextApiRequest, NextApiResponse } from "next";
+import prisma from "@/lib/prisma";
export default async function handler(
req: NextApiRequest,
@@ -13,16 +14,36 @@ export default async function handler(
return res.status(401).json({ message: "Invalid token" });
}
- const { linkId } = req.query as { linkId: string };
+ const { linkId, documentId } = req.query as {
+ linkId: string;
+ documentId: 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}`);
+ if (linkId) {
+ // revalidate this link
+ await res.revalidate(`/view/${linkId}`);
+ }
+
+ if (documentId) {
+ // revalidate all links for this document
+ const links = await prisma.link.findMany({
+ where: {
+ documentId: documentId,
+ domainId: null,
+ },
+ select: { id: true },
+ });
+ for (const link of links) {
+ await res.revalidate(`/view/${link.id}`);
+ }
+ }
+
return res.json({ revalidated: true });
} catch (err) {
// If there was an error, Next.js will continue
// to show the last successfully generated page
+ console.error("Error during revalidation:", err);
return res.status(500).send("Error revalidating");
}
}
diff --git a/pages/api/teams/[teamId]/documents/[id]/versions/index.ts b/pages/api/teams/[teamId]/documents/[id]/versions/index.ts
index b43dd5a6..d19de131 100644
--- a/pages/api/teams/[teamId]/documents/[id]/versions/index.ts
+++ b/pages/api/teams/[teamId]/documents/[id]/versions/index.ts
@@ -9,7 +9,7 @@ import { client } from "@/trigger";
export default async function handle(
req: NextApiRequest,
- res: NextApiResponse
+ res: NextApiResponse,
) {
if (req.method === "POST") {
// GET /api/teams/:teamId/documents/:id/versions
@@ -19,15 +19,21 @@ export default async function handle(
}
// get document id from query params
- const { teamId, id: documentId } = req.query as { teamId: string; id: string };
- const { url, type, numPages } = req.body as { url: string, type: string, numPages: number };
+ const { teamId, id: documentId } = req.query as {
+ teamId: string;
+ id: string;
+ };
+ const { url, type, numPages } = req.body as {
+ url: string;
+ type: string;
+ numPages: number;
+ };
- console.log("documentId", documentId)
- console.log("file", url)
+ console.log("documentId", documentId);
+ console.log("file", url);
const userId = (session.user as CustomUser).id;
-
try {
const { document } = await getTeamWithUsersAndDocument({
teamId,
@@ -83,13 +89,17 @@ export default async function handle(
// trigger document uploaded event to trigger convert-pdf-to-image job
await client.sendEvent({
name: "document.uploaded",
- payload: { documentVersionId: version.id, versionNumber: version.versionNumber, documentId: documentId },
+ payload: {
+ documentVersionId: version.id,
+ versionNumber: version.versionNumber,
+ documentId: documentId,
+ },
});
res.status(200).json({ id: documentId });
} catch (error) {
log(
- `Failed to create new version for document ${documentId}. Error: \n\n ${error}`
+ `Failed to create new version for document ${documentId}. Error: \n\n ${error}`,
);
return res.status(500).json({
message: "Internal Server Error",