Skip to content

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

refactor: change view/[linkId] to static w/ on-demand revalidation

Marc Seitzf68e24f45b4e
diff --git a/lib/types.ts b/lib/types.ts
index 4eba661f..95ba66d6 100644
--- a/lib/types.ts
+++ b/lib/types.ts
@@ -38,7 +38,7 @@ export interface LinkWithViews extends Link {
export interface LinkWithDocument extends Link {
document: Document & {
- versions: { versionNumber: number }[];
+ versions: { id: string; versionNumber: number; hasPages: true }[];
};
}
diff --git a/pages/api/links/[id]/index.ts b/pages/api/links/[id]/index.ts
index 28f6b16e..96d061f5 100644
--- a/pages/api/links/[id]/index.ts
+++ b/pages/api/links/[id]/index.ts
@@ -14,6 +14,7 @@ export default async function handle(
const { id } = req.query as { id: string };
try {
+ console.time("get-link");
const link = await prisma.link.findUnique({
where: {
id: id,
@@ -30,7 +31,7 @@ export default async function handle(
id: true,
versions: {
where: { isPrimary: true },
- select: { versionNumber: true },
+ select: { id: true, versionNumber: true, hasPages: true },
take: 1,
},
},
@@ -38,6 +39,8 @@ export default async function handle(
},
});
+ console.timeEnd("get-link");
+
if (!link) {
return res.status(404).json({ error: "Link not found" });
}
diff --git a/pages/view/[linkId].tsx b/pages/view/[linkId].tsx
index 5ab98b1d..1f3e9c4d 100644
--- a/pages/view/[linkId].tsx
+++ b/pages/view/[linkId].tsx
@@ -1,18 +1,17 @@
import LoadingSpinner from "@/components/ui/loading-spinner";
import DocumentView from "@/components/view/document-view";
import { useLink } from "@/lib/swr/use-link";
+import { CustomUser, LinkWithDocument } from "@/lib/types";
import NotFound from "@/pages/404";
+import { GetStaticPropsContext } from "next";
import { useSession } from "next-auth/react";
+import { useRouter } from "next/router";
-export default function ViewPage() {
- const { link, error } = useLink();
+export default function ViewPage({ link }: { link: LinkWithDocument }) {
+ const router = useRouter();
const { data: session, status } = useSession();
- if (error && error.status === 404) {
- return <NotFound />;
- }
-
- if (!link || status === "loading") {
+ if (!link || status === "loading" || router.isFallback) {
return (
<div className="h-screen flex items-center justify-center">
<LoadingSpinner className="h-20 w-20" />
@@ -27,7 +26,7 @@ export default function ViewPage() {
isArchived,
} = link;
- const { email: userEmail } = session?.user || {};
+ const { email: userEmail, id: userId } = (session?.user as CustomUser) || {};
// If the link is expired, show a 404 page
if (expiresAt && new Date(expiresAt) < new Date()) {
@@ -44,9 +43,47 @@ export default function ViewPage() {
if (emailProtected || linkPassword) {
return (
- <DocumentView link={link} userEmail={userEmail} isProtected={true} />
+ <DocumentView
+ link={link}
+ userEmail={userEmail}
+ userId={userId}
+ isProtected={true}
+ />
);
}
- return <DocumentView link={link} userEmail={userEmail} isProtected={false} />;
+ return (
+ <DocumentView
+ link={link}
+ userEmail={userEmail}
+ userId={userId}
+ isProtected={false}
+ />
+ );
+}
+
+export async function getStaticProps(context: GetStaticPropsContext) {
+ const linkId = context.params?.linkId as string;
+
+ // Fetch the link
+ const res = await fetch(`${process.env.NEXTAUTH_URL}/api/links/${linkId}`);
+ if (!res.ok) {
+ return { notFound: true };
+ }
+ const link = (await res.json()) as LinkWithDocument;
+
+ console.log(link);
+
+ return {
+ props: {
+ link,
+ },
+ };
+}
+
+export async function getStaticPaths() {
+ return {
+ paths: [],
+ fallback: true,
+ };
}