Skip to content

Free & Open-Source Invoice Generator - No Sign-Up, No Ads, Instant PDF Export, 100% In-Browser, and Fully Customizable Templates.

fix: enhance error handling and re-render logic in MobileInvoicePDFViewer (#134)

Vlad Sazonaufad100b8682f
diff --git a/e2e/stripe-invoice-template/share-logic.test.ts b/e2e/stripe-invoice-template/share-logic.test.ts
index cb0a145..b5e05b7 100644
--- a/e2e/stripe-invoice-template/share-logic.test.ts
+++ b/e2e/stripe-invoice-template/share-logic.test.ts
@@ -351,7 +351,7 @@ test.describe("Stripe Invoice Sharing Logic", () => {
await shareButton.click({ force: true });
await expect(page.getByText("Unable to Share Invoice")).toBeVisible({
- timeout: 700,
+ timeout: 3000,
});
await expect(
diff --git a/e2e/stripe-invoice-template/template.test.ts b/e2e/stripe-invoice-template/template.test.ts
index db18da4..8b21241 100644
--- a/e2e/stripe-invoice-template/template.test.ts
+++ b/e2e/stripe-invoice-template/template.test.ts
@@ -340,6 +340,9 @@ test.describe("Stripe Invoice Template", () => {
.getByRole("combobox", { name: "Invoice Template" })
.selectOption("stripe");
+ // Wait for URL to be updated
+ await expect(page).toHaveURL("/?template=stripe");
+
// Upload a valid logo
await page.evaluate(uploadBase64LogoAsFile, SMALL_TEST_IMAGE_BASE64);
@@ -353,7 +356,7 @@ test.describe("Stripe Invoice Template", () => {
// Wait a moment for any debounced localStorage updates
// eslint-disable-next-line playwright/no-wait-for-timeout
- await page.waitForTimeout(500);
+ await page.waitForTimeout(600);
// Verify data is actually saved in localStorage
const storedData = (await page.evaluate((key) => {
@@ -473,6 +476,9 @@ test.describe("Stripe Invoice Template", () => {
.getByRole("combobox", { name: "Invoice Template" })
.selectOption("stripe");
+ // Wait for URL to be updated
+ await expect(page).toHaveURL("/?template=stripe");
+
const generalInfoSection = page.getByTestId("general-information-section");
// Add payment URL
diff --git a/src/app/(app)/components/invoice-pdf-preview/mobile-pdf-viewer.tsx b/src/app/(app)/components/invoice-pdf-preview/mobile-pdf-viewer.tsx
index 1f3c863..5379f9b 100644
--- a/src/app/(app)/components/invoice-pdf-preview/mobile-pdf-viewer.tsx
+++ b/src/app/(app)/components/invoice-pdf-preview/mobile-pdf-viewer.tsx
@@ -6,7 +6,7 @@ import type { InvoiceData } from "@/app/schema";
import { InvoicePdfTemplate } from "../invoice-pdf-template";
import { StripeInvoicePdfTemplate } from "../invoice-pdf-stripe-template";
import * as Sentry from "@sentry/nextjs";
-import { useMemo } from "react";
+import { useMemo, useState } from "react";
// https://github.com/wojtekmaj/react-pdf/issues/1822#issuecomment-2233334169
pdfjs.GlobalWorkerOptions.workerSrc = `//unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.mjs`;
@@ -23,6 +23,8 @@ export const MobileInvoicePDFViewer = ({
}: {
invoiceData: InvoiceData;
}) => {
+ const [key, setKey] = useState(0);
+
const memoizedInvoicePdfTemplate = useMemo(() => {
switch (invoiceData.template) {
case "stripe":
@@ -66,7 +68,9 @@ export const MobileInvoicePDFViewer = ({
// https://github.com/wojtekmaj/react-pdf
return (
<Document
- file={url}
+ // we use a key to force a re-render of the PDF viewer in case of error
+ key={key}
+ file={url || ""}
className="h-[480px] w-[650px] overflow-auto lg:h-[620px] 2xl:h-[700px]"
loading={
<div className="flex h-[480px] w-full items-center justify-center border border-gray-200 bg-gray-200 lg:h-[620px] 2xl:h-[700px]">
@@ -77,8 +81,30 @@ export const MobileInvoicePDFViewer = ({
</div>
}
onLoadError={(error) => {
- Sentry.captureException(error);
+ console.error(error);
+
+ // Force a re-render of the PDF viewer to try to recover from error
+ setKey((prev) => prev + 1);
}}
+ error={
+ <div className="flex h-[480px] w-full items-center justify-center border border-gray-200 bg-gray-200 lg:h-[620px] 2xl:h-[700px]">
+ <div className="text-center">
+ <p className="text-balance text-red-600">
+ Error generating PDF preview. Please refresh the page or use
+ a different browser. If the issue persists, please file a{" "}
+ <a
+ href="https://pdfinvoicegenerator.userjot.com/board/bugs?cursor=1&order=top&limit=10"
+ target="_blank"
+ rel="noopener noreferrer"
+ className="text-blue-600 underline hover:text-blue-800"
+ >
+ bug report
+ </a>
+ .
+ </p>
+ </div>
+ </div>
+ }
>
<Page
pageNumber={1}