Skip to content

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

fix: errors in revalidation and logging

Marc Seitz0c63be9e1057
diff --git a/lib/utils.ts b/lib/utils.ts
index 1fe12885..a7a939de 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -85,6 +85,30 @@ export const logStore = async ({ object }: { object: any }) => {
}
};
+const LOG_TIMEOUT_MS = 2500;
+
+const postJsonWithTimeout = async (
+ url: string,
+ body: unknown,
+ timeoutMs: number,
+) => {
+ const controller = new AbortController();
+ const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
+ timeoutId.unref?.();
+ try {
+ return await fetch(url, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify(body),
+ signal: controller.signal,
+ });
+ } finally {
+ clearTimeout(timeoutId);
+ }
+};
+
export const log = async ({
message,
type,
@@ -105,45 +129,32 @@ export const log = async ({
/* Log a message to channel */
try {
- if (type === "trial" && process.env.PPMK_TRIAL_SLACK_WEBHOOK_URL) {
- return await fetch(`${process.env.PPMK_TRIAL_SLACK_WEBHOOK_URL}`, {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
+ const payload = {
+ blocks: [
+ {
+ type: "section",
+ text: {
+ type: "mrkdwn",
+ // prettier-ignore
+ text: `${mention ? "<@U05BTDUKPLZ> " : ""}${type === "error" ? ":rotating_light: " : ""}${message}`,
+ },
},
- body: JSON.stringify({
- blocks: [
- {
- type: "section",
- text: {
- type: "mrkdwn",
- // prettier-ignore
- text: `${mention ? "<@U05BTDUKPLZ> " : ""}${message}`,
- },
- },
- ],
- }),
- });
+ ],
+ };
+
+ if (type === "trial" && process.env.PPMK_TRIAL_SLACK_WEBHOOK_URL) {
+ return await postJsonWithTimeout(
+ process.env.PPMK_TRIAL_SLACK_WEBHOOK_URL,
+ payload,
+ LOG_TIMEOUT_MS,
+ );
}
- return await fetch(`${process.env.PPMK_SLACK_WEBHOOK_URL}`, {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({
- blocks: [
- {
- type: "section",
- text: {
- type: "mrkdwn",
- // prettier-ignore
- text: `${mention ? "<@U05BTDUKPLZ> " : ""}${type === "error" ? ":rotating_light: " : ""}${message}`,
- },
- },
- ],
- }),
- });
+ return await postJsonWithTimeout(
+ `${process.env.PPMK_SLACK_WEBHOOK_URL}`,
+ payload,
+ LOG_TIMEOUT_MS,
+ );
} catch (e) {}
};
diff --git a/pages/view/[linkId]/d/[documentId].tsx b/pages/view/[linkId]/d/[documentId].tsx
index 09a27c13..1fa30172 100644
--- a/pages/view/[linkId]/d/[documentId].tsx
+++ b/pages/view/[linkId]/d/[documentId].tsx
@@ -12,10 +12,7 @@ import { parsePageId } from "notion-utils";
import z from "zod";
import notion from "@/lib/notion";
-import {
- addSignedUrls,
- fetchMissingPageReferences,
-} from "@/lib/notion/utils";
+import { addSignedUrls, fetchMissingPageReferences } from "@/lib/notion/utils";
import { CustomUser, LinkWithDataroomDocument, NotionTheme } from "@/lib/types";
import LoadingSpinner from "@/components/ui/loading-spinner";
@@ -48,6 +45,7 @@ type DataroomDocumentProps = {
useAdvancedExcelViewer: boolean;
useCustomAccessForm: boolean;
logoOnAccessForm: boolean;
+ error?: boolean;
};
export default function DataroomDocumentViewPage({
@@ -59,6 +57,7 @@ export default function DataroomDocumentViewPage({
useAdvancedExcelViewer,
useCustomAccessForm,
logoOnAccessForm,
+ error,
}: DataroomDocumentProps) {
const router = useRouter();
const { data: session, status } = useSession();
@@ -85,6 +84,12 @@ export default function DataroomDocumentViewPage({
);
}
+ if (error) {
+ return (
+ <NotFound message="Sorry, we had trouble loading this link. Please try again in a moment." />
+ );
+ }
+
const {
email: verifiedEmail,
d: disableEditEmail,
@@ -191,6 +196,13 @@ export async function getStaticProps(context: GetStaticPropsContext) {
const res = await fetch(
`${process.env.NEXTAUTH_URL}/api/links/${linkId}/documents/${documentId}`,
);
+ if (!res.ok) {
+ if (res.status === 404) {
+ return { notFound: true };
+ }
+
+ return { props: { error: true }, revalidate: 30 };
+ }
const { linkType, link, brand } =
(await res.json()) as DataroomDocumentLinkData;
@@ -273,7 +285,8 @@ export async function getStaticProps(context: GetStaticPropsContext) {
revalidate: brand || recordMap ? 10 : 60,
};
} catch (error) {
- console.error("Fetching error:", error);
+ const message = error instanceof Error ? error.message : String(error);
+ console.error("Fetching error:", message);
return { props: { error: true }, revalidate: 30 };
}
}
diff --git a/pages/view/[linkId]/index.tsx b/pages/view/[linkId]/index.tsx
index 7ff9f1e3..ba5087b7 100644
--- a/pages/view/[linkId]/index.tsx
+++ b/pages/view/[linkId]/index.tsx
@@ -14,10 +14,7 @@ import z from "zod";
import { getFeatureFlags } from "@/lib/featureFlags";
import notion from "@/lib/notion";
-import {
- addSignedUrls,
- fetchMissingPageReferences,
-} from "@/lib/notion/utils";
+import { addSignedUrls, fetchMissingPageReferences } from "@/lib/notion/utils";
import {
CustomUser,
LinkWithDataroom,
@@ -151,7 +148,11 @@ export const getStaticProps = async (context: GetStaticPropsContext) => {
await fetchMissingPageReferences(recordMap);
await addSignedUrls({ recordMap });
} catch (notionError) {
- console.error("Notion API error:", notionError);
+ const message =
+ notionError instanceof Error
+ ? notionError.message
+ : String(notionError);
+ console.error("Notion API error:", message);
// Return a temporary error page instead of 404
return {
props: { notionError: true },
@@ -296,7 +297,8 @@ export const getStaticProps = async (context: GetStaticPropsContext) => {
};
}
} catch (error) {
- console.error("Fetching error:", error);
+ const message = error instanceof Error ? error.message : String(error);
+ console.error("Fetching error:", message);
return { props: { error: true }, revalidate: 30 };
}
};
diff --git a/pages/view/domains/[domain]/[slug]/d/[documentId].tsx b/pages/view/domains/[domain]/[slug]/d/[documentId].tsx
index 76d21782..a7f46ffd 100644
--- a/pages/view/domains/[domain]/[slug]/d/[documentId].tsx
+++ b/pages/view/domains/[domain]/[slug]/d/[documentId].tsx
@@ -12,10 +12,7 @@ import { parsePageId } from "notion-utils";
import z from "zod";
import notion from "@/lib/notion";
-import {
- addSignedUrls,
- fetchMissingPageReferences,
-} from "@/lib/notion/utils";
+import { addSignedUrls, fetchMissingPageReferences } from "@/lib/notion/utils";
import { CustomUser, LinkWithDataroomDocument, NotionTheme } from "@/lib/types";
import LoadingSpinner from "@/components/ui/loading-spinner";
@@ -48,6 +45,7 @@ type DataroomDocumentProps = {
useAdvancedExcelViewer: boolean;
useCustomAccessForm: boolean;
logoOnAccessForm: boolean;
+ error?: boolean;
};
export default function DataroomDocumentViewPage({
@@ -59,6 +57,7 @@ export default function DataroomDocumentViewPage({
useAdvancedExcelViewer,
useCustomAccessForm,
logoOnAccessForm,
+ error,
}: DataroomDocumentProps) {
const router = useRouter();
const { data: session, status } = useSession();
@@ -86,6 +85,12 @@ export default function DataroomDocumentViewPage({
);
}
+ if (error) {
+ return (
+ <NotFound message="Sorry, we had trouble loading this link. Please try again in a moment." />
+ );
+ }
+
const {
email: verifiedEmail,
d: disableEditEmail,
@@ -207,7 +212,11 @@ export async function getStaticProps(context: GetStaticPropsContext) {
)}/${encodeURIComponent(slug)}/documents/${documentId}`,
);
if (!res.ok) {
- throw new Error(`Failed to fetch: ${res.status}`);
+ if (res.status === 404) {
+ return { notFound: true };
+ }
+
+ return { props: { error: true }, revalidate: 30 };
}
const { linkType, link, brand } =
@@ -292,7 +301,8 @@ export async function getStaticProps(context: GetStaticPropsContext) {
revalidate: brand || recordMap ? 10 : 60,
};
} catch (error) {
- console.error("Fetching error:", error);
+ const message = error instanceof Error ? error.message : String(error);
+ console.error("Fetching error:", message);
return { props: { error: true }, revalidate: 30 };
}
}
diff --git a/pages/view/domains/[domain]/[slug]/index.tsx b/pages/view/domains/[domain]/[slug]/index.tsx
index ea5296a8..285a5154 100644
--- a/pages/view/domains/[domain]/[slug]/index.tsx
+++ b/pages/view/domains/[domain]/[slug]/index.tsx
@@ -14,10 +14,7 @@ import z from "zod";
import { getFeatureFlags } from "@/lib/featureFlags";
import notion from "@/lib/notion";
-import {
- addSignedUrls,
- fetchMissingPageReferences,
-} from "@/lib/notion/utils";
+import { addSignedUrls, fetchMissingPageReferences } from "@/lib/notion/utils";
import {
CustomUser,
LinkWithDataroom,
@@ -276,7 +273,8 @@ export const getStaticProps = async (context: GetStaticPropsContext) => {
};
}
} catch (error) {
- console.error("Fetching error:", error);
+ const message = error instanceof Error ? error.message : String(error);
+ console.error("Fetching error:", message);
return { props: { error: true }, revalidate: 30 };
}
};