Skip to content

Commit

Permalink
Merge branch 'adrael-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
cyntler committed Apr 15, 2022
2 parents a30d853 + 1c516f8 commit 00ce4ea
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
29 changes: 22 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ You can provide a theme object with one or all of the available properties.
/>
```

## 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" />;
```

## Styling

Any styling applied to the `<DocViewer>` component, is directly applied to the main `div` container.
Expand Down Expand Up @@ -202,13 +214,16 @@ const MyDocViewer = styled(DocViewer)`
You can provide a config object, which configures parts of the component as required.

```tsx
<DocViewer documents={docs} config={{
header: {
disableHeader: false,
disableFileName: false,
retainURLParams: false
}
}} />
<DocViewer
documents={docs}
config={{
header: {
disableHeader: false,
disableFileName: false,
retainURLParams: false,
},
}}
/>
```

## Overriding Header Component
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 00ce4ea

Please sign in to comment.