Skip to content

Commit

Permalink
feat: add prefetchMethod option
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael.marques committed Oct 14, 2021
1 parent fc604da commit 54d9219
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,23 @@ MyCustomPNGRenderer.fileLoader = ({
<br />
<br />

### Custom pre-fetch HTTP Verb

Some services (such as AWS) provide URLs that works only for one pre-configured verb.
By default, `react-doc-viewer` fetches document metadata through a `HEAD` request in order to guess its `Content-Type`.
If you need to have a specific verb for the pre-fetching, use the `prefetchMethod` option on the DocViewer:

```tsx
import DocViewer, { DocViewerRenderers } from "react-doc-viewer";

<DocViewer
prefetchMethod="GET"
/>;
```

<br />
<br />

### Themed

You can provide a theme object with one or all of the available properties.
Expand Down Expand Up @@ -368,6 +385,7 @@ const myHeader: IHeaderOverride = (state, previousDocument, nextDocument) => {
| config? | [`IConfig`](#iconfig) |
| theme? | [`ITheme`](#itheme) |
| pluginRenderers? | [`DocRenderer[]`](#docrenderer) |
| prefetchMethod? | `string` |

---

Expand Down
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface DocViewerProps {
config?: IConfig;
theme?: ITheme;
pluginRenderers?: DocRenderer[];
prefetchMethod?: string;
}

const DocViewer: FC<DocViewerProps> = (props) => {
Expand Down
3 changes: 2 additions & 1 deletion src/state/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ const DocViewerContext = createContext<{
}>({ state: initialState, dispatch: () => null });

const AppProvider: FC<DocViewerProps> = (props) => {
const { children, documents, config, pluginRenderers } = props;
const { children, documents, config, pluginRenderers, prefetchMethod } = props;

const [state, dispatch] = useReducer<MainStateReducer>(mainStateReducer, {
...initialState,
documents: documents || [],
currentDocument: documents && documents.length ? documents[0] : undefined,
config,
pluginRenderers,
prefetchMethod,
});

// On inital load, and whenever they change,
Expand Down
1 change: 1 addition & 0 deletions src/state/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type IMainState = {
rendererRect?: DOMRect;
config?: IConfig;
pluginRenderers?: DocRenderer[];
prefetchMethod?: string;
};

export const initialState: IMainState = {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/useDocumentLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const useDocumentLoader = (): {
CurrentRenderer: DocRenderer | null | undefined;
} => {
const { state, dispatch } = useContext(DocViewerContext);
const { currentFileNo, currentDocument } = state;
const { currentFileNo, currentDocument, prefetchMethod } = state;

const { CurrentRenderer } = useRendererSelector();

Expand All @@ -33,7 +33,7 @@ export const useDocumentLoader = (): {
const controller = new AbortController();
const { signal } = controller;

fetch(documentURI, { method: "HEAD", signal }).then((response) => {
fetch(documentURI, { method: prefetchMethod || "HEAD", signal }).then((response) => {
const contentTypeRaw = response.headers.get("content-type");
const contentTypes = contentTypeRaw?.split(";") || [];
const contentType = contentTypes.length ? contentTypes[0] : undefined;
Expand Down

0 comments on commit 54d9219

Please sign in to comment.