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)