From 0424e163a73504c0d51b5ee04b582d0b9402b557 Mon Sep 17 00:00:00 2001 From: Tony Jin Date: Tue, 16 May 2017 15:29:26 -0700 Subject: [PATCH] Fix: Older versions of webkit iOS incorrectly cache range requests (#118) - See: https://bugs.webkit.org/show_bug.cgi?id=82672 - This fix adds a cache-busting header if on iOS --- src/lib/util.js | 1 + src/lib/viewers/doc/DocBaseViewer.js | 21 +++++++++++++------ .../doc/__tests__/DocBaseViewer-test.js | 19 +++++++++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/lib/util.js b/src/lib/util.js index cbd132354..a70007f57 100644 --- a/src/lib/util.js +++ b/src/lib/util.js @@ -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 * diff --git a/src/lib/viewers/doc/DocBaseViewer.js b/src/lib/viewers/doc/DocBaseViewer.js index 5e3acc1db..6d7a8fa95 100644 --- a/src/lib/viewers/doc/DocBaseViewer.js +++ b/src/lib/viewers/doc/DocBaseViewer.js @@ -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, @@ -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); diff --git a/src/lib/viewers/doc/__tests__/DocBaseViewer-test.js b/src/lib/viewers/doc/__tests__/DocBaseViewer-test.js index 932a49716..b4db85d10 100644 --- a/src/lib/viewers/doc/__tests__/DocBaseViewer-test.js +++ b/src/lib/viewers/doc/__tests__/DocBaseViewer-test.js @@ -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'