Skip to content

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

feat: load external js async client-only when needed

Marc Seitzc1bfd52d7a0e
diff --git a/components/view/view-data.tsx b/components/view/view-data.tsx
index e0d65e03..bb84db0a 100644
--- a/components/view/view-data.tsx
+++ b/components/view/view-data.tsx
@@ -1,3 +1,5 @@
+import dynamic from "next/dynamic";
+
import { Brand } from "@prisma/client";
import { ExtendedRecordMap } from "notion-types";
@@ -6,10 +8,14 @@ import PDFViewer from "@/components/view/PDFViewer";
import PagesViewerNew from "@/components/view/PagesViewerNew";
import { DEFAULT_DOCUMENT_VIEW_TYPE } from "@/components/view/document-view";
import Nav from "@/components/view/nav";
-import { ExcelViewer } from "@/components/view/viewer/excel-viewer";
import { LinkWithDocument } from "@/lib/types";
+const ExcelViewer = dynamic(
+ () => import("@/components/view/viewer/excel-viewer"),
+ { ssr: false },
+);
+
export default function ViewData({
viewData,
link,
diff --git a/components/view/viewer/excel-viewer.tsx b/components/view/viewer/excel-viewer.tsx
index e330da57..3ce94e36 100644
--- a/components/view/viewer/excel-viewer.tsx
+++ b/components/view/viewer/excel-viewer.tsx
@@ -1,24 +1,43 @@
-import React, { useEffect, useRef, useState } from "react";
+import { useEffect, useRef, useState } from "react";
import "@/public/vendor/handsontable/handsontable.full.min.css";
// Define the type for the JSON data
type SheetData = { [key: string]: any };
-export const ExcelViewer = ({
+export default function ExcelViewer({
columns,
data,
}: {
columns: string[];
data: SheetData[];
-}) => {
+}) {
const [availableWidth, setAvailableWidth] = useState<number>(200);
const [availableHeight, setAvailableHeight] = useState<number>(200);
- const [handsontableLoaded, setHandsontableLoaded] = useState<boolean>(true);
+ const [handsontableLoaded, setHandsontableLoaded] = useState<boolean>(false);
+
+ useEffect(() => {
+ const script = document.createElement("script");
+ script.src =
+ "https://cdnjs.cloudflare.com/ajax/libs/handsontable/6.2.2/handsontable.full.min.js";
+ script.async = true;
+ script.onload = () => {
+ setHandsontableLoaded(true);
+ };
+ script.onerror = () => {
+ console.error("Failed to load Handsontable script.");
+ };
+
+ document.body.appendChild(script);
+
+ return () => {
+ document.body.removeChild(script);
+ };
+ }, []);
const hotRef = useRef<HTMLDivElement>(null);
const containerRef = useRef<HTMLDivElement>(null);
- // @ts-ignore
+ // @ts-ignore - Handsontable import has not types
const hotInstanceRef = useRef<Handsontable | null>(null);
const calculateSize = () => {
@@ -46,10 +65,7 @@ export const ExcelViewer = ({
hotInstanceRef.current.destroy();
}
- console.log("data", data);
- console.log("columns", columns);
-
- // @ts-ignore
+ // @ts-ignore - Handsontable import has not types
hotInstanceRef.current = new Handsontable(hotRef.current!, {
data: data,
readOnly: true,
@@ -76,14 +92,12 @@ export const ExcelViewer = ({
}, [handsontableLoaded, data, columns, availableHeight, availableWidth]);
return (
- <>
- <div
- style={{ height: "calc(100vh - 64px)" }}
- className="flex h-screen items-center justify-center"
- ref={containerRef}
- >
- <div ref={hotRef}></div>
- </div>
- </>
+ <div
+ style={{ height: "calc(100vh - 64px)" }}
+ className="flex h-screen items-center justify-center"
+ ref={containerRef}
+ >
+ <div ref={hotRef}></div>
+ </div>
);
-};
+}
diff --git a/pages/_document.tsx b/pages/_document.tsx
index ac84e98d..47faab55 100644
--- a/pages/_document.tsx
+++ b/pages/_document.tsx
@@ -1,15 +1,9 @@
import { Head, Html, Main, NextScript } from "next/document";
-import Script from "next/script";
export default function Document() {
return (
<Html lang="en" className="h-full bg-background" suppressHydrationWarning>
- <Head>
- <Script
- src="https://cdnjs.cloudflare.com/ajax/libs/handsontable/6.2.2/handsontable.full.min.js"
- strategy="beforeInteractive"
- />
- </Head>
+ <Head />
<body className="h-full">
<Main />
<NextScript />