From 8841c8c874ee7e9ad9df77f24f3d5c6fd6924cbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Alby?= Date: Thu, 28 Jul 2022 11:21:46 +0200 Subject: [PATCH] Merge pull request #9008 from overleaf/jpa-disable-pdf-caching-edge-legacy [web] disable pdf caching feature in legacy Edge browser GitOrigin-RevId: f57ca3385337b5516e27a701646f2ed92325bf8d --- .../js/features/pdf-preview/util/pdf-caching-flags.js | 6 ++++++ .../frontend/js/features/pdf-preview/util/pdf-caching.js | 7 ++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/services/web/frontend/js/features/pdf-preview/util/pdf-caching-flags.js b/services/web/frontend/js/features/pdf-preview/util/pdf-caching-flags.js index 484b7e29b1..db7db3e439 100644 --- a/services/web/frontend/js/features/pdf-preview/util/pdf-caching-flags.js +++ b/services/web/frontend/js/features/pdf-preview/util/pdf-caching-flags.js @@ -1,6 +1,12 @@ import getMeta from '../../../utils/meta' +const hasTextEncoder = typeof TextEncoder !== 'undefined' +if (!hasTextEncoder) { + console.warn('TextEncoder is not available. Disabling pdf-caching.') +} + function isFlagEnabled(flag) { + if (!hasTextEncoder) return false return getMeta('ol-splitTestVariants')?.[flag] === 'enabled' } diff --git a/services/web/frontend/js/features/pdf-preview/util/pdf-caching.js b/services/web/frontend/js/features/pdf-preview/util/pdf-caching.js index 569080f46c..e28a254b9c 100644 --- a/services/web/frontend/js/features/pdf-preview/util/pdf-caching.js +++ b/services/web/frontend/js/features/pdf-preview/util/pdf-caching.js @@ -5,12 +5,12 @@ const MAX_SUB_REQUEST_COUNT = 4 const MAX_SUB_REQUEST_BYTES = 4 * PDF_JS_CHUNK_SIZE const INCREMENTAL_CACHE_SIZE = 1000 -const ENCODER = new TextEncoder() function backfillEdgeBounds(file) { if (!file.backfilledEdgeBoundsOnce) { + const encoder = new TextEncoder() for (const range of file.ranges) { if (range.objectId) { - range.objectId = ENCODER.encode(range.objectId) + range.objectId = encoder.encode(range.objectId) range.start -= range.objectId.byteLength } } @@ -221,6 +221,7 @@ function getMultipartBoundary(response, chunk) { function resolveMultiPartResponses({ file, chunks, data, boundary, metrics }) { const responses = [] let offsetStart = 0 + const encoder = new TextEncoder() for (const chunk of chunks) { const header = `\r\n--${boundary}\r\nContent-Type: application/pdf\r\nContent-Range: bytes ${ chunk.start @@ -228,7 +229,7 @@ function resolveMultiPartResponses({ file, chunks, data, boundary, metrics }) { const headerSize = header.length // Verify header content. A proxy might have tampered with it. - const headerRaw = ENCODER.encode(header) + const headerRaw = encoder.encode(header) if ( !data .subarray(offsetStart, offsetStart + headerSize)