remix: [Bug]: Failing to load documents with react-pdf (or pdf.js)

Which Remix packages are impacted?

  • remix (Remix core)

What version of Remix are you using?

1.1.3

What version of Node are you using? Minimum supported version is 14.

16.13.0

Steps to Reproduce

I wrote a very simple component to load a PDF.

import { Paper } from "@prisma/client";
import React from "react";
import { LoaderFunction, useLoaderData } from "remix";
import { db } from "../../utils/db.sever";
import { Document, Page } from "react-pdf";

type LoaderData = { paper: Paper };

export const loader: LoaderFunction = async ({ params }) => {
  console.log("params!!!", params);
  const paper: LoaderData = {
    paper: await db.paper.findUnique({ where: { id: Number(params.paperId) }, rejectOnNotFound: true }),
  };
  return paper;
};

export default function PaperRender() {
  const { paper } = useLoaderData<LoaderData>();
  return (
    <div className="relative w-full">
      <Document file={paper.link}>
        <Page pageNumber={0} />
      </Document>
    </div>
  );
}

Expected Behavior

It should only load paperId: 1.

It works with next.js if using dynamic loading (I’m not familiar with an equivalent solution for remix). See this: https://codesandbox.io/s/react-pdf-next-js-y4ev2?file=/pdf-worker.js

Actual Behavior

It fetches the data from the DB but then it tries to load pdf.worker.js for some reason, and this fails of course.

params!!! { paperId: '1' }
[1] GET /papers/1 200 - - 31.677 ms
[1] params!!! { paperId: 'pdf.worker.js' }
[1] There was an error running the data loader for route routes/papers/$paperId
[1] Error: 
[1] Invalid `db.paper.findUnique()` invocation in

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (1 by maintainers)

Most upvoted comments

This seems to be happening because react-pdf tries to load pdf.worker.js from /papers/pdf.worker.js.

According to react-pdf documentation, the dfjs.GlobalWorkerOptions.workerSrc attribute needs to point to the location of the worker file. https://github.com/wojtekmaj/react-pdf#standard-browserify-esbuild-and-others

Here’s a minimal reproduction https://codesandbox.io/s/stupefied-brahmagupta-yrwmq3?file=/app/routes/paper/%24id.jsx

I’ve manually copied the worker to public so the example should work if lines 5-6 in /app/routes/papre/$id.jsx are uncommented. I guess this could be done as a build step but I’m not sure if there’s a more “Remix way” of doing this…