diff --git a/src/lib/Preview.js b/src/lib/Preview.js index d3836a971..4cb1bebfe 100644 --- a/src/lib/Preview.js +++ b/src/lib/Preview.js @@ -771,10 +771,15 @@ class Preview extends EventEmitter { this.retryCount = 0; } - // Fetch access tokens before proceeding - getTokens(this.file.id, this.previewOptions.token) - .then(this.handleTokenResponse) - .catch(this.handleFetchError); + const isPreviewOffline = typeof fileIdOrFile === 'object' && this.options.skipServerUpdate; + if (isPreviewOffline) { + this.handleTokenResponse({}); + } else { + // Fetch access tokens before proceeding + getTokens(this.file.id, this.previewOptions.token) + .then(this.handleTokenResponse) + .catch(this.handleFetchError); + } } /** @@ -798,6 +803,8 @@ class Preview extends EventEmitter { // Load from cache if the current file is valid, otherwise load file info from server if (checkFileValid(this.file)) { + // Save file in cache. This also adds the 'ORIGINAL' representation. It is required to preview files offline + cacheFile(this.cache, this.file); this.loadFromCache(); } else { this.loadFromServer(); diff --git a/src/lib/__tests__/Preview-test.js b/src/lib/__tests__/Preview-test.js index 8370b3a02..e7f5d970d 100644 --- a/src/lib/__tests__/Preview-test.js +++ b/src/lib/__tests__/Preview-test.js @@ -1002,6 +1002,14 @@ describe('lib/Preview', () => { expect(preview.retryTimeout).to.equal(undefined); }); + it('should load preview when a well-formed file object is passed and server update should be skipped', () => { + preview.options.skipServerUpdate = true; + + preview.load(stubs.file); + expect(stubs.handleTokenResponse).to.be.calledWith({}); + expect(stubs.getTokens).to.not.be.called; + }); + it('should set the retry count if we are retrying by file ID', () => { preview.retryCount = 0; preview.file.id = '0'; @@ -1114,6 +1122,7 @@ describe('lib/Preview', () => { stubs.checkFileValid.returns(true); preview.handleTokenResponse({}); + expect(stubs.cacheFile).to.be.calledWith(preview.cache, preview.file); expect(stubs.loadFromCache).to.be.called; expect(stubs.loadFromServer).to.not.be.called; });