Skip to content

Commit

Permalink
Fix: Older versions of webkit iOS incorrectly cache range requests (#118
Browse files Browse the repository at this point in the history
)

- See: https://bugs.webkit.org/show_bug.cgi?id=82672
- This fix adds a cache-busting header if on iOS
  • Loading branch information
tonyjin authored May 16, 2017
1 parent 0b7d8de commit 0424e16
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ export function replacePlaceholders(string, placeholderValues) {
/* eslint-enable no-plusplus */
});
}

/**
* Check to see if a file requires a Box3D viewer to be viewed
*
Expand Down
21 changes: 15 additions & 6 deletions src/lib/viewers/doc/DocBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,8 @@ class DocBaseViewer extends BaseViewer {
* @return {Promise} Promise to initialize Viewer
*/
initViewer(pdfUrl) {
this.bindDOMListeners();

// Initialize PDF.js in container
this.pdfViewer = new PDFJS.PDFViewer({
container: this.docEl,
Expand All @@ -531,15 +533,22 @@ class DocBaseViewer extends BaseViewer {
RANGE_REQUEST_CHUNK_SIZE_NON_US;
}

this.bindDOMListeners();

// Load PDF from representation URL
this.pdfLoadingTask = PDFJS.getDocument({
const docInitParams = {
url: pdfUrl,
rangeChunkSize
});
};

// Fix incorrectly cached range requests on older versions of iOS webkit browsers,
// see: https://bugs.webkit.org/show_bug.cgi?id=82672
if (this.isMobile && Browser.isIOS()) {
docInitParams.httpHeaders = {
'If-None-Match': 'webkit-no-cache'
};
}

// Set document for PDF.js
// Load PDF from representation URL and set as document for pdf.js. Cache
// the loading task so we can cancel if needed
this.pdfLoadingTask = PDFJS.getDocument(docInitParams);
return this.pdfLoadingTask.then((doc) => {
this.pdfViewer.setDocument(doc);

Expand Down
19 changes: 19 additions & 0 deletions src/lib/viewers/doc/__tests__/DocBaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,25 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {
});
});

it('should set a cache-busting header if on mobile', () => {
docBase.options.location = {
locale: 'en-US'
};
docBase.isMobile = true;
sandbox.stub(Browser, 'isIOS').returns(true);
sandbox.stub(PDFJS, 'getDocument').returns(Promise.resolve({}));

return docBase.initViewer('').then(() => {
expect(PDFJS.getDocument).to.be.calledWith({
url: '',
rangeChunkSize: 1048576,
httpHeaders: {
'If-None-Match': 'webkit-no-cache'
}
});
});
});

it('should resolve the loading task and set the document/viewer', () => {
const doc = {
url: 'url'
Expand Down

0 comments on commit 0424e16

Please sign in to comment.