Skip to content

[Feature]: Add ability to override fetchOptions #20040

Open
@wojtekmaj

Description

@wojtekmaj

Is the feature relevant to the Firefox PDF Viewer?

No

Feature description

Currently, PDFs are fetched using the options provided by createFetchOptions. However, this function offers limited configurability.

One of the main reasons developers avoid using PDF.js's built-in loading logic is the lack of support for more advanced fetch options. While it's possible to pass custom headers, it's not currently possible to set a custom method or include a body in the request.

Many users of react-pdf (myself included) rely on endpoints that return binary PDF data in response to a POST request with a request body. Due to current limitations, we must manually fetch the PDF, store the binary data, and then hand it off to PDF.js, adding unnecessary complexity to an otherwise straightforward task.

Proposal for DocumentInitParameters extension

To solve this problem while ensuring full backwards compatibility, I propose extending the DocumentInitParameters type with a new optional field:

/**
 * - Custom fetch configuration, used when `url` is specified.
 * Overrides default fetch options returned by `createFetchOptions`.
 */
fetchOptions?: RequestInit | undefined;

This field would be used only when url is defined, and would allow developers to specify a POST request with a body, custom caching behavior, credentials, and other advanced configurations supported by the Fetch API.

The existing fields such as httpHeaders and withCredentials could continue to work as-is and take precedence unless explicitly overridden in fetchOptions, ensuring backward compatibility and gradual adoption.

In the long term, it may make sense to deprecate httpHeaders and withCredentials in favor of the more versatile and standardized fetchOptions field, reducing API surface complexity and aligning with native fetch behavior.

Other PDF viewers

n/a

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions