Skip to content

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

fix: load only required pages and reduce expiration window

Marc Seitze121961e6035
diff --git a/app/api/views-dataroom/route.ts b/app/api/views-dataroom/route.ts
index 1168cbb4..b1bc2187 100644
--- a/app/api/views-dataroom/route.ts
+++ b/app/api/views-dataroom/route.ts
@@ -52,6 +52,7 @@ export async function POST(request: NextRequest) {
dataroomViewId,
viewType,
groupId,
+ startPage,
...data
} = body as {
linkId: string;
@@ -66,6 +67,7 @@ export async function POST(request: NextRequest) {
dataroomViewId?: string;
viewType: "DATAROOM_VIEW" | "DOCUMENT_VIEW";
groupId?: string;
+ startPage?: number;
};
const { email, password, name, hasConfirmedAgreement } = data as {
@@ -889,15 +891,20 @@ export async function POST(request: NextRequest) {
},
});
- // Only sign URLs for the first batch of pages to avoid timeouts on large documents.
+ // Sign URLs for pages around the requested start page (or page 1 by default).
// Remaining page URLs are fetched on-demand by the client via /api/views/pages.
+ const centerIndex = Math.max(0, (startPage ?? 1) - 1);
+ const halfWindow = Math.floor(INITIAL_PAGES_TO_LOAD / 2);
+ const signStart = Math.max(0, centerIndex - halfWindow);
+ const signEnd = Math.min(documentPages.length, signStart + INITIAL_PAGES_TO_LOAD);
+
documentPages = await Promise.all(
documentPages.map(async (page, index) => {
const { storageType, ...otherPage } = page;
return {
...otherPage,
file:
- index < INITIAL_PAGES_TO_LOAD
+ index >= signStart && index < signEnd
? await getFile({ data: page.file, type: storageType })
: null,
};
diff --git a/app/api/views/route.ts b/app/api/views/route.ts
index 01d98706..a157bce3 100644
--- a/app/api/views/route.ts
+++ b/app/api/views/route.ts
@@ -40,6 +40,7 @@ export async function POST(request: NextRequest) {
documentName,
hasPages,
ownerId,
+ startPage,
...data
} = body as {
linkId: string;
@@ -49,6 +50,7 @@ export async function POST(request: NextRequest) {
documentName: string;
hasPages: boolean;
ownerId: string;
+ startPage?: number;
};
const { email, password, name, hasConfirmedAgreement } = data as {
@@ -608,15 +610,20 @@ export async function POST(request: NextRequest) {
},
});
- // Only sign URLs for the first batch of pages to avoid timeouts on large documents.
+ // Sign URLs for pages around the requested start page (or page 1 by default).
// Remaining page URLs are fetched on-demand by the client via /api/views/pages.
+ const centerIndex = Math.max(0, (startPage ?? 1) - 1);
+ const halfWindow = Math.floor(INITIAL_PAGES_TO_LOAD / 2);
+ const signStart = Math.max(0, centerIndex - halfWindow);
+ const signEnd = Math.min(documentPages.length, signStart + INITIAL_PAGES_TO_LOAD);
+
documentPages = await Promise.all(
documentPages.map(async (page, index) => {
const { storageType, ...otherPage } = page;
return {
...otherPage,
file:
- index < INITIAL_PAGES_TO_LOAD
+ index >= signStart && index < signEnd
? await getFile({ data: page.file, type: storageType })
: null,
};
diff --git a/components/view/dataroom/dataroom-document-view.tsx b/components/view/dataroom/dataroom-document-view.tsx
index 5705fee4..5d564860 100644
--- a/components/view/dataroom/dataroom-document-view.tsx
+++ b/components/view/dataroom/dataroom-document-view.tsx
@@ -152,6 +152,7 @@ export default function DataroomDocumentView({
userId: userId ?? null,
documentVersionId: link.dataroomDocument.document.versions[0].id,
hasPages: link.dataroomDocument.document.versions[0].hasPages,
+ startPage: router.query.p ? Number(router.query.p) : undefined,
dataroomId: link.dataroomId,
linkType: "DATAROOM_LINK",
dataroomViewId: viewData.dataroomViewId ?? null,
diff --git a/components/view/document-view.tsx b/components/view/document-view.tsx
index 90a413ff..57122286 100644
--- a/components/view/document-view.tsx
+++ b/components/view/document-view.tsx
@@ -142,6 +142,7 @@ export default function DocumentView({
userId: userId ?? null,
documentVersionId: document.versions[0].id,
hasPages: document.versions[0].hasPages,
+ startPage: router.query.p ? Number(router.query.p) : undefined,
useAdvancedExcelViewer,
previewToken,
code: code ?? undefined,
diff --git a/components/view/viewer/pages-horizontal-viewer.tsx b/components/view/viewer/pages-horizontal-viewer.tsx
index 25b2ed0d..34ff3711 100644
--- a/components/view/viewer/pages-horizontal-viewer.tsx
+++ b/components/view/viewer/pages-horizontal-viewer.tsx
@@ -351,18 +351,36 @@ export default function PagesHorizontalViewer({
}, [screenshotProtectionEnabled]);
useEffect(() => {
- setLoadedImages((prev) =>
- prev.map((loaded, index) =>
- index < DEFAULT_PRELOADED_IMAGES_NUM ? true : loaded,
- ),
- );
- ensurePagesLoaded?.(1);
+ const start = Math.max(0, pageNumber - 1 - DEFAULT_PRELOADED_IMAGES_NUM);
+ const end = Math.min(numPages - 1, pageNumber - 1 + DEFAULT_PRELOADED_IMAGES_NUM);
+ setLoadedImages((prev) => {
+ const next = [...prev];
+ for (let i = start; i <= end; i++) {
+ next[i] = true;
+ }
+ return next;
+ });
+ ensurePagesLoaded?.(pageNumber);
}, []); // Run once on mount
useEffect(() => {
ensurePagesLoaded?.(pageNumber);
}, [pageNumber, ensurePagesLoaded]);
+ useEffect(() => {
+ setLoadedImages((prev) => {
+ let changed = false;
+ const next = [...prev];
+ pages.forEach((page, index) => {
+ if (page.file && !next[index]) {
+ next[index] = true;
+ changed = true;
+ }
+ });
+ return changed ? next : prev;
+ });
+ }, [pages]);
+
useEffect(() => {
// Remove token and email query parameters on component mount
const removeQueryParams = (queries: string[]) => {
diff --git a/components/view/viewer/pages-vertical-viewer.tsx b/components/view/viewer/pages-vertical-viewer.tsx
index adadb3f3..195c852b 100644
--- a/components/view/viewer/pages-vertical-viewer.tsx
+++ b/components/view/viewer/pages-vertical-viewer.tsx
@@ -371,18 +371,36 @@ export default function PagesVerticalViewer({
}, [screenshotProtectionEnabled]);
useEffect(() => {
- setLoadedImages((prev) =>
- prev.map((loaded, index) =>
- index < DEFAULT_PRELOADED_IMAGES_NUM ? true : loaded,
- ),
- );
- ensurePagesLoaded?.(1);
+ const start = Math.max(0, pageNumber - 1 - DEFAULT_PRELOADED_IMAGES_NUM);
+ const end = Math.min(numPages - 1, pageNumber - 1 + DEFAULT_PRELOADED_IMAGES_NUM);
+ setLoadedImages((prev) => {
+ const next = [...prev];
+ for (let i = start; i <= end; i++) {
+ next[i] = true;
+ }
+ return next;
+ });
+ ensurePagesLoaded?.(pageNumber);
}, []); // Run once on mount
useEffect(() => {
ensurePagesLoaded?.(pageNumber);
}, [pageNumber, ensurePagesLoaded]);
+ useEffect(() => {
+ setLoadedImages((prev) => {
+ let changed = false;
+ const next = [...prev];
+ pages.forEach((page, index) => {
+ if (page.file && !next[index]) {
+ next[index] = true;
+ changed = true;
+ }
+ });
+ return changed ? next : prev;
+ });
+ }, [pages]);
+
useEffect(() => {
// Remove token and email query parameters on component mount
const removeQueryParams = (queries: string[]) => {
diff --git a/lib/constants.ts b/lib/constants.ts
index f7a41e55..953e065c 100644
--- a/lib/constants.ts
+++ b/lib/constants.ts
@@ -43,6 +43,7 @@ export const REACTIONS = [
// time in milliseconds
export const ONE_SECOND = 1000;
export const ONE_MINUTE = ONE_SECOND * 60;
+export const TWO_MINUTES = ONE_MINUTE * 2;
export const ONE_HOUR = ONE_MINUTE * 60;
export const ONE_DAY = ONE_HOUR * 24;
export const ONE_WEEK = ONE_DAY * 7;
diff --git a/pages/api/file/s3/get-presigned-get-url.ts b/pages/api/file/s3/get-presigned-get-url.ts
index aeb88457..aa1cf491 100644
--- a/pages/api/file/s3/get-presigned-get-url.ts
+++ b/pages/api/file/s3/get-presigned-get-url.ts
@@ -4,7 +4,7 @@ import { GetObjectCommand } from "@aws-sdk/client-s3";
import { getSignedUrl as getCloudfrontSignedUrl } from "@aws-sdk/cloudfront-signer";
import { getSignedUrl as getS3SignedUrl } from "@aws-sdk/s3-request-presigner";
-import { ONE_HOUR, ONE_SECOND } from "@/lib/constants";
+import { ONE_SECOND, TWO_MINUTES } from "@/lib/constants";
import { getTeamS3ClientAndConfig } from "@/lib/files/aws-client";
import { log } from "@/lib/utils";
@@ -64,7 +64,7 @@ export default async function handler(
url: distributionUrl.toString(),
keyPairId: `${config.distributionKeyId}`,
privateKey: `${config.distributionKeyContents}`,
- dateLessThan: new Date(Date.now() + ONE_HOUR).toISOString(),
+ dateLessThan: new Date(Date.now() + TWO_MINUTES).toISOString(),
});
return res.status(200).json({ url });
@@ -76,7 +76,7 @@ export default async function handler(
});
const url = await getS3SignedUrl(client, getObjectCommand, {
- expiresIn: ONE_HOUR / ONE_SECOND,
+ expiresIn: TWO_MINUTES / ONE_SECOND,
});
return res.status(200).json({ url });