I am using pdf.js to render some pdf in browser, this is how I defined the option parameters:
export const pdfJsOptions: Options = {
cMapUrl: `/pdfjs-dist/${pdfjs.version}/cmaps/`,
httpHeaders: {
Authorization: "Bearer " + getAccessToken(),
},
// open the range request
// the default value was false
// if want to load the whole pdf by default
// set this value to true
disableRange: false,
// just fetch the needed slice
disableAutoFetch: true,
rangeChunkSize: 65536 * 5,
}
then pass the options to Document like this:
<Document
options={pdfJsOptions}
file={curPdfUrl!}
onLoadSuccess={onDocumentLoadSuccess}
></Document>
this will cache the access token into memory and never refresh even though the access token is expired. But I need the pdf.js get the newest access token every time send the pdf request. Then I tweak the const to function like this:
/**
* here we should use the function not a const value
* because the const value will cached the expired access token
* that will make the pdf request failed in the future
* @returns
*/
export const getPdfjsOptions = (): Options => {
return {
cMapUrl: `/pdfjs-dist/${pdfjs.version}/cmaps/`,
httpHeaders: {
Authorization: "Bearer " + getAccessToken(),
},
// open the range request
// the default value was false
// if want to load the whole pdf by default
// set this value to true
disableRange: false,
// just fetch the needed slice
disableAutoFetch: true,
rangeChunkSize: 65536 * 5,
};
};
and invoke this function in Document that make sure everytime load access token from localStorage.but when I refresh the page, the Document will fetch pdf again and again forever. How to define the option to get the newest access token every time and avoid the dead loop to fetch the pdf at the same time?
2