Skip to content

Commit

Permalink
Update: Disable pdf.js streaming (#483)
Browse files Browse the repository at this point in the history
Streaming performance in Chrome is still bad compared to just using range request, so disable until more progress on performance is made.
  • Loading branch information
tonyjin committed Nov 14, 2017
1 parent 25d7840 commit 5a062c1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
18 changes: 14 additions & 4 deletions src/lib/viewers/doc/DocBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const SCROLL_EVENT_THROTTLE_INTERVAL = 200;
const SCROLL_END_TIMEOUT = this.isMobile ? 500 : 250;
const RANGE_REQUEST_CHUNK_SIZE_US = 1048576; // 1MB
const RANGE_REQUEST_CHUNK_SIZE_NON_US = 524288; // 512KB
const MINIMUM_RANGE_REQUEST_FILE_SIZE_NON_US = 26214400; // 25MB
const MOBILE_MAX_CANVAS_SIZE = 2949120; // ~3MP 1920x1536

const LOADING_ICON_MAP = {
Expand Down Expand Up @@ -605,7 +606,7 @@ class DocBaseViewer extends BaseViewer {
setupPdfjs() {
// Set PDFJS worker & character maps
const { file, location } = this.options;
const { watermark_info: watermarkInfo } = file;
const { size, watermark_info: watermarkInfo } = file;
const assetUrlCreator = createAssetUrlCreator(location);
PDFJS.workerSrc = assetUrlCreator(`third-party/doc/${DOC_STATIC_ASSETS_VERSION}/pdf.worker.min.js`);
PDFJS.imageResourcesPath = assetUrlCreator(`third-party/doc/${DOC_STATIC_ASSETS_VERSION}/images/`);
Expand All @@ -615,13 +616,22 @@ class DocBaseViewer extends BaseViewer {
// Open links in new tab
PDFJS.externalLinkTarget = PDFJS.LinkTarget.BLANK;

// Disable streaming via fetch until performance is improved
PDFJS.disableStream = true;

// Disable font faces on IOS 10.3.X
// @NOTE(JustinHoldstock) 2017-04-11: Check to remove this after next IOS release after 10.3.1
PDFJS.disableFontFace = PDFJS.disableFontFace || Browser.hasFontIssue();

// Disable range requests for watermarked files since they are streamed, otherwise override range request
// disabling by pdf.js's compatibility checking since all the browsers we support allow range requests
PDFJS.disableRange = watermarkInfo && watermarkInfo.is_watermarked;
// Disable range requests for files smaller than MINIMUM_RANGE_REQUEST_FILE_SIZE (25MB) for
// previews outside of the US since the additional latency overhead per range request can be
// more than the additional time for a continuous request. This also overrides any range request
// disabling that may be set by pdf.js's compatibility checking since the browsers we support
// should all be able to properly handle range requests.
PDFJS.disableRange = location.locale !== 'en-US' && size < MINIMUM_RANGE_REQUEST_FILE_SIZE_NON_US;

// Disable range requests for watermarked files since they are streamed
PDFJS.disableRange = PDFJS.disableRange || (watermarkInfo && watermarkInfo.is_watermarked);

// Disable text layer if user doesn't have download permissions
PDFJS.disableTextLayer =
Expand Down
24 changes: 23 additions & 1 deletion src/lib/viewers/doc/__tests__/DocBaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,29 @@ describe('src/lib/viewers/doc/DocBaseViewer', () => {
expect(PDFJS.disableFontFace).to.be.true;
});

it('should not disable range requests normally', () => {
it('should not disable streaming', () => {
docBase.setupPdfjs();
expect(PDFJS.disableStream).to.be.true;
});

it('should not disable range requests if the locale is en-US', () => {
docBase.setupPdfjs();
expect(PDFJS.disableRange).to.be.false;
});

it('should disable range requests if locale is not en-US, the file is smaller than 25MB and is not an Excel file', () => {
docBase.options.file.size = 26000000;
docBase.options.extension = 'pdf';
docBase.options.location.locale = 'ja-JP';
docBase.setupPdfjs();
expect(PDFJS.disableRange).to.be.true;
});

it('should enable range requests if locale is not en-US, the file is greater than 25MB and is not watermarked', () => {
docBase.options.location.locale = 'ja-JP';
docBase.options.file.size = 26500000;
docBase.options.extension = 'pdf';
docBase.options.file.watermark_info.is_watermarked = false;
docBase.setupPdfjs();
expect(PDFJS.disableRange).to.be.false;
});
Expand Down

0 comments on commit 5a062c1

Please sign in to comment.