Skip to content

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

feat: add error revalidation

Marc Seitz4dcfc734d092
diff --git a/pages/view/[linkId]/index.tsx b/pages/view/[linkId]/index.tsx
index 18c88125..17e2f05a 100644
--- a/pages/view/[linkId]/index.tsx
+++ b/pages/view/[linkId]/index.tsx
@@ -10,11 +10,6 @@ import { useSession } from "next-auth/react";
import { ExtendedRecordMap } from "notion-types";
import { parsePageId } from "notion-utils";
-import LoadingSpinner from "@/components/ui/loading-spinner";
-import CustomMetaTag from "@/components/view/custom-metatag";
-import DataroomView from "@/components/view/dataroom/dataroom-view";
-import DocumentView from "@/components/view/document-view";
-
import notion from "@/lib/notion";
import { addSignedUrls } from "@/lib/notion/utils";
import {
@@ -24,6 +19,11 @@ import {
NotionTheme,
} from "@/lib/types";
+import LoadingSpinner from "@/components/ui/loading-spinner";
+import CustomMetaTag from "@/components/view/custom-metatag";
+import DataroomView from "@/components/view/dataroom/dataroom-view";
+import DocumentView from "@/components/view/document-view";
+
type DocumentLinkData = {
linkType: "DOCUMENT_LINK";
link: LinkWithDocument;
@@ -65,6 +65,7 @@ export const getStaticProps = async (context: GetStaticPropsContext) => {
if (!res.ok) {
throw new Error(`Failed to fetch: ${res.status}`);
}
+
const { linkType, link, brand } = (await res.json()) as
| DocumentLinkData
| DataroomLinkData;
@@ -85,18 +86,24 @@ export const getStaticProps = async (context: GetStaticPropsContext) => {
link.document.versions[0];
if (type === "notion") {
- theme = new URL(file).searchParams.get("mode");
- const notionPageId = parsePageId(file, { uuid: false });
- if (!notionPageId) {
+ try {
+ theme = new URL(file).searchParams.get("mode");
+ const notionPageId = parsePageId(file, { uuid: false });
+ if (!notionPageId) {
+ return { notFound: true };
+ }
+
+ pageId = notionPageId;
+ recordMap = await notion.getPage(pageId, { signFileUrls: false });
+ await addSignedUrls({ recordMap });
+ } catch (notionError) {
+ console.error("Notion API error:", notionError);
+ // Return a temporary error page instead of 404
return {
- notFound: true,
+ props: { notionError: true },
+ revalidate: 30,
};
}
-
- pageId = notionPageId;
- recordMap = await notion.getPage(pageId, { signFileUrls: false });
- // TODO: separately sign the file urls until PR merged and published; ref: https://github.com/NotionX/react-notion-x/issues/580#issuecomment-2542823817
- await addSignedUrls({ recordMap });
}
const { team, teamId, advancedExcelEnabled, ...linkDocument } =
@@ -191,14 +198,15 @@ export const getStaticProps = async (context: GetStaticPropsContext) => {
useAdvancedExcelViewer: false, // INFO: this is managed in the API route
useCustomAccessForm:
teamId === "cm0154tiv0000lr2t6nr5c6kp" ||
- teamId === "clup33by90000oewh4rfvp2eg",
+ teamId === "clup33by90000oewh4rfvp2eg" ||
+ teamId === "cm76hfyvy0002q623hmen99pf",
},
revalidate: 10,
};
}
} catch (error) {
console.error("Fetching error:", error);
- return { notFound: true };
+ return { props: { error: true }, revalidate: 30 };
}
};
@@ -217,7 +225,9 @@ export default function ViewPage({
showAccountCreationSlide,
useAdvancedExcelViewer,
useCustomAccessForm,
-}: ViewPageProps) {
+ error,
+ notionError,
+}: ViewPageProps & { error?: boolean; notionError?: boolean }) {
const router = useRouter();
const { data: session, status } = useSession();
const [storedToken, setStoredToken] = useState<string | undefined>(undefined);
@@ -245,6 +255,18 @@ export default function ViewPage({
);
}
+ if (error) {
+ return (
+ <NotFound message="Sorry, we had trouble loading this link. Please try refreshing." />
+ );
+ }
+
+ if (notionError) {
+ return (
+ <NotFound message="Sorry, we had trouble loading this link. Please try again in a moment." />
+ );
+ }
+
const {
email: verifiedEmail,
d: disableEditEmail,
diff --git a/pages/view/domains/[domain]/[slug]/index.tsx b/pages/view/domains/[domain]/[slug]/index.tsx
index 7f922956..3dc753f4 100644
--- a/pages/view/domains/[domain]/[slug]/index.tsx
+++ b/pages/view/domains/[domain]/[slug]/index.tsx
@@ -171,14 +171,15 @@ export const getStaticProps = async (context: GetStaticPropsContext) => {
useAdvancedExcelViewer: false, // INFO: this is managed in the API route
useCustomAccessForm:
teamId === "cm0154tiv0000lr2t6nr5c6kp" ||
- teamId === "clup33by90000oewh4rfvp2eg",
+ teamId === "clup33by90000oewh4rfvp2eg" ||
+ teamId === "cm76hfyvy0002q623hmen99pf",
},
revalidate: 10,
};
}
} catch (error) {
console.error("Fetching error:", error);
- return { notFound: true };
+ return { props: { error: true }, revalidate: 30 };
}
};
@@ -196,6 +197,7 @@ export default function ViewPage({
showAccountCreationSlide,
useAdvancedExcelViewer,
useCustomAccessForm,
+ error,
}: {
linkData: DocumentLinkData | DataroomLinkData;
notionData: {
@@ -214,6 +216,7 @@ export default function ViewPage({
showAccountCreationSlide: boolean;
useAdvancedExcelViewer: boolean;
useCustomAccessForm: boolean;
+ error?: boolean;
}) {
const router = useRouter();
const { data: session, status } = useSession();
@@ -241,6 +244,12 @@ export default function ViewPage({
);
}
+ if (error) {
+ return (
+ <NotFound message="Sorry, we had trouble loading this link. Please try again in a moment." />
+ );
+ }
+
const {
email: verifiedEmail,
d: disableEditEmail,