From 8a7d9fc8b198a8aad870637d97eb7686bab874e2 Mon Sep 17 00:00:00 2001 From: Evan Kaloudis Date: Fri, 24 Jul 2026 22:00:11 -0400 Subject: [PATCH] fix: install TextEncoder/TextDecoder polyfills before protobufjs loads protobufjs v8 reads TextDecoder at import time, and Hermes does not provide it. The polyfill assignment in index.js ran after hoisted imports, so the app crashed on launch with 'Property TextDecoder doesn't exist'. Move the polyfills into their own module imported first so they are installed before any other module executes. --- index.js | 11 +---------- polyfills.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 10 deletions(-) create mode 100644 polyfills.js diff --git a/index.js b/index.js index 1844917ed..77f9c49dd 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +import './polyfills'; import 'react-native-gesture-handler'; import { enableScreens } from 'react-native-screens'; enableScreens(); @@ -11,19 +12,9 @@ enableScreens(); import 'react-native-get-random-values'; import 'react-native-url-polyfill/auto'; import 'message-port-polyfill'; -const TextEncodingPolyfill = require("text-encoding"); import Long from 'long'; import protobuf from 'protobufjs'; -const applyGlobalPolyfills = () => { - Object.assign(global, { - TextEncoder: TextEncodingPolyfill.TextEncoder, - TextDecoder: TextEncodingPolyfill.TextDecoder, - }); -}; - -applyGlobalPolyfills(); - protobuf.util.Long = Long; protobuf.configure(); diff --git a/polyfills.js b/polyfills.js new file mode 100644 index 000000000..ec48d12de --- /dev/null +++ b/polyfills.js @@ -0,0 +1,13 @@ +// Global polyfills that must be installed before any other module loads. +// Hermes has no TextEncoder/TextDecoder, and protobufjs v8+ reads +// TextDecoder at import time, so this module must be the first import +// in index.js — imports are hoisted, so assigning these globals from +// index.js's own body runs too late. +const TextEncodingPolyfill = require('text-encoding'); + +if (typeof global.TextEncoder === 'undefined') { + global.TextEncoder = TextEncodingPolyfill.TextEncoder; +} +if (typeof global.TextDecoder === 'undefined') { + global.TextDecoder = TextEncodingPolyfill.TextDecoder; +}