Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve offline behaviour #14

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ function App() {
>
example_protected.pdf (Password: 123456)
</button>

<input
className="button"
rezkiy37 marked this conversation as resolved.
Show resolved Hide resolved
type="file"
onChange={(event) => {
const uploadedFile = event?.target?.files?.[0];

if (!uploadedFile) {
return;
}

setFile(URL.createObjectURL(uploadedFile));
}}
/>
</div>
</>
)}
Expand Down
7 changes: 7 additions & 0 deletions example/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ module.exports = {
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
// We are importing this worker as a string by using asset/source otherwise it will default to loading via an HTTPS request later.
// This causes issues if we have gone offline before the pdfjs web worker is set up as we won't be able to load it from the server.
{
// eslint-disable-next-line prefer-regex-literals
test: new RegExp('node_modules/pdfjs-dist/legacy/build/pdf.worker.js'),
type: 'asset/source',
},
],
},
plugins: [
Expand Down
6 changes: 4 additions & 2 deletions src/PDFPreviewer.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// @ts-expect-error - This line imports a module from 'pdfjs-dist' package which lacks TypeScript typings.
// eslint-disable-next-line import/no-extraneous-dependencies
import pdfWorkerSource from 'pdfjs-dist/legacy/build/pdf.worker';
import React, {memo, useCallback, useLayoutEffect, useRef, useState} from 'react';
import type {CSSProperties, ReactNode} from 'react';
import times from 'lodash/times';
Expand Down Expand Up @@ -57,8 +60,7 @@ const defaultProps = {
contentContainerStyle: {},
};

// @ts-expect-error - It is a recommended step for import worker - https://github.com/wojtekmaj/react-pdf/blob/main/packages/react-pdf/README.md#import-worker-recommended
pdfjs.GlobalWorkerOptions.workerSrc = new URL('pdfjs-dist/build/pdf.worker.min.js', import.meta.url).toString();
pdfjs.GlobalWorkerOptions.workerSrc = URL.createObjectURL(new Blob([pdfWorkerSource], {type: 'text/javascript'}));

function PDFPreviewer({
file,
Expand Down
Loading