diff --git a/src/lib/Preview.js b/src/lib/Preview.js index b8ab09058..fea3aac5e 100644 --- a/src/lib/Preview.js +++ b/src/lib/Preview.js @@ -1250,7 +1250,7 @@ class Preview extends EventEmitter { } // Log now that loading is finished - this.emitLoadMetrics(); + this.emitLoadMetrics(data); // Show download and print buttons if user can download if (canDownload(this.file, this.options)) { @@ -1522,9 +1522,10 @@ class Preview extends EventEmitter { * A value of 0 means that the load milestone was never reached. * * @private + * @param {string} [encoding] - Type of encoding applied to the downloaded content. ie) GZIP * @return {void} */ - emitLoadMetrics() { + emitLoadMetrics({ encoding } = {}) { if (!this.file || !this.file.id) { return; } @@ -1544,6 +1545,7 @@ class Preview extends EventEmitter { const total = times.reduce((acc, current) => acc + current); const event = { + encoding, event_name: LOAD_METRIC.previewLoadEvent, value: total, // Sum of all available load times. [LOAD_METRIC.fileInfoTime]: times[0], diff --git a/src/lib/__tests__/Preview-test.js b/src/lib/__tests__/Preview-test.js index c7bd6b1a6..6aaecae83 100644 --- a/src/lib/__tests__/Preview-test.js +++ b/src/lib/__tests__/Preview-test.js @@ -8,7 +8,7 @@ import PreviewError from '../PreviewError'; import DownloadReachability from '../DownloadReachability'; import * as file from '../file'; import * as util from '../util'; -import { API_HOST, CLASS_NAVIGATION_VISIBILITY, PERMISSION_PREVIEW } from '../constants'; +import { API_HOST, CLASS_NAVIGATION_VISIBILITY, PERMISSION_PREVIEW, ENCODING_TYPES } from '../constants'; import { VIEWER_EVENT, ERROR_CODE, LOAD_METRIC, PREVIEW_METRIC } from '../events'; import Timer from '../Timer'; @@ -2387,6 +2387,14 @@ describe('lib/Preview', () => { }); preview.emitLoadMetrics(); }); + + it('should append encoding field to load metric, when provided', (done) => { + preview.once(PREVIEW_METRIC, (metric) => { + expect(metric.encoding).to.equal(ENCODING_TYPES.GZIP); + done(); + }); + preview.emitLoadMetrics({ encoding: ENCODING_TYPES.GZIP }); + }); }); describe('getRequestHeaders()', () => { diff --git a/src/lib/viewers/doc/DocBaseViewer.js b/src/lib/viewers/doc/DocBaseViewer.js index 4ee431c8d..58d402b9f 100644 --- a/src/lib/viewers/doc/DocBaseViewer.js +++ b/src/lib/viewers/doc/DocBaseViewer.js @@ -60,6 +60,9 @@ class DocBaseViewer extends BaseViewer { // Public //-------------------------------------------------------------------------- + /** @property {string} - Tracks the type of encoding, if applicable, that was requested for the viewable content */ + encoding; + /** * @inheritdoc */ @@ -571,6 +574,9 @@ class DocBaseViewer extends BaseViewer { // Use chunk size set in viewer options if available let rangeChunkSize = this.getViewerOption('rangeChunkSize'); + // If range requests are disabled, request the gzip compressed version of the representation + this.encoding = PDFJS.disableRange ? ENCODING_TYPES.GZIP : undefined; + // Otherwise, use large chunk size if locale is en-US and the default, // smaller chunk size if not. This is using a rough assumption that // en-US users have higher bandwidth to Box. @@ -583,10 +589,10 @@ class DocBaseViewer extends BaseViewer { let url = pdfUrl; - // If range requests are disabled, request the gzip compressed version of the representation - if (PDFJS.disableRange) { + // Apply encoding request to the content request + if (this.encoding) { url = appendQueryParams(url, { - [QUERY_PARAM_ENCODING]: ENCODING_TYPES.GZIP + [QUERY_PARAM_ENCODING]: this.encoding }); } @@ -996,6 +1002,7 @@ class DocBaseViewer extends BaseViewer { if (!this.loaded) { this.loaded = true; this.emit(VIEWER_EVENT.load, { + encoding: this.encoding, numPages: pagesCount, endProgress: false, // Indicate that viewer will end progress later scale: currentScale diff --git a/src/lib/viewers/doc/__tests__/DocBaseViewer-test.js b/src/lib/viewers/doc/__tests__/DocBaseViewer-test.js index 81764fe7f..5dfa3d472 100644 --- a/src/lib/viewers/doc/__tests__/DocBaseViewer-test.js +++ b/src/lib/viewers/doc/__tests__/DocBaseViewer-test.js @@ -1569,9 +1569,11 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => { }; docBase.loaded = false; docBase.pdfViewer.pagesCount = 5; + docBase.encoding = 'gzip'; docBase.pagesinitHandler(); expect(stubs.emit).to.be.calledWith(VIEWER_EVENT.load, { + encoding: docBase.encoding, endProgress: false, numPages: 5, scale: sinon.match.any