Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting "ReferenceError: 'TextEncoder' is undefined" in IE11 after importing the text-encoding polyfill?

I am trying to pollyfill a library fetch-readablestream (https://github.com/jonnyreeves/fetch-readablestream). I have added the polyfills and most things do work but I keep getting an error about TextEncoder ERROR ReferenceError: 'TextEncoder' is undefined

I've added the required pollyfills: web-streams-polyfill, text-encoding, and babel-polyfill. I tried other equivalents of these polyfills but I get the same issue.

My polyfills.ts file after the required IE imports that I uncommented.

import 'web-streams-polyfill'; // Run `npm install --save web-streams-polyfill`.
import 'text-encoding'; // Run `npm install --save text-encoding`.
import 'babel-polyfill'; // Run `npm install --save babel-polyfill`.

I also tried adding scripts to index.html

<script src="node_modules/text-encoding/lib/encoding-indexes.js"></script>
<script src="node_modules/text-encoding/lib/encoding.js"></script>

I don't expect any errors but I get this:

ERROR ReferenceError: 'TextEncoder' is undefined
   "ERROR"
   {
      [functions]: ,
      __proto__: { },
      description: "'TextEncoder' is undefined",
      message: "'TextEncoder' is undefined",
      name: "ReferenceError",
      number: -2146823279,
      stack: "ReferenceError: 'TextEncoder' is undefined
   at responseParserFactory (http://localhost:4200/vendor.js:139703:3)
   at xhrTransport (http://localhost:4200/vendor.js:139960:1)
   at fetchStream (http://localhost:4200/vendor.js:139795:4)
   at DevicewiseService.prototype.getNotifications (http://localhost:4200/main.js:6577:9)
   at Anonymous function (http://localhost:4200/main.js:159:17)
   at SafeSubscriber.prototype.__tryOrUnsub (http://localhost:4200/vendor.js:145834:9)
   at SafeSubscriber.prototype.next (http://localhost:4200/vendor.js:145772:13)
   at Subscriber.prototype._next (http://localhost:4200/vendor.js:145715:5)
   at Subscriber.prototype.next (http://localhost:4200/vendor.js:145692:9)
   at MapSubscriber.prototype._next (http://localhost:4200/vendor.js:150984:5)",
      Symbol([[Cancel]])_g.r6fxkqwxet3: undefined,
      Symbol([[Pull]])_h.r6fxkqwxet3: undefined,
      Symbol(INITIAL_VALUE)_p.r6fxkqwxet3: undefined,
      Symbol(rxSubscriber)_o.r6fxkqwxet3: undefined
   }

We can see this happens in the fetchStream call.

like image 770
Austen Stone Avatar asked Feb 13 '19 17:02

Austen Stone


People also ask

How to write textencoder to a file in Firefox?

You need to import TextEncoder explicitly from some other module, as SDK modules lack the class. .flush () is only supported in Firefox 27+ (and a bad idea anyway). Use .writeAtomic if you need that. You write: true to write to a file. Here is a full, working example I tested in Firefox 25 ( main.js)

Do I need to choose an encoding standard for my text?

However, if you share text files with people who work in other languages, download text files across the Internet, or share text files with other computer systems, you may need to choose an encoding standard when you open or save a file.

What is Unicode encoding in Microsoft Word?

To avoid problems with encoding and decoding text files, you can save files with Unicode encoding. Unicode accommodates most characters sets across all the languages that are commonly used among computer users today. Because Word is based on Unicode, Word automatically saves files encoded as Unicode.

What happens if I open a file with a different encoding?

However, if you open the same file on a computer that uses a different encoding, the computer displays whatever character corresponds to the 201 numeric value in the encoding standard that the computer uses by default.


1 Answers

Use npm package: https://www.npmjs.com/package/text-encoding (now deprecated, but works like a charm).

Then add to polyfills.ts code:

if (typeof window['TextEncoder'] !== 'function') {
  const TextEncodingPolyfill = require('text-encoding');
  window['TextEncoder'] = TextEncodingPolyfill.TextEncoder;
  window['TextDecoder'] = TextEncodingPolyfill.TextDecoder;
}

Do not use import 'text-encoding'; !

like image 52
Lukáš Jurygáček Avatar answered Oct 08 '22 04:10

Lukáš Jurygáček