3

How would I add polyfill for String.repeat method for ie11? I am not using it directly in my code, it's probably some of the imported libs.

In IE console I get this error: Object doesn't support property or method 'repeat'

I also get 'AbortController' is undefined which I am also not using my code, probably external lib again.

I am using create react app and I imported in index.js:

import 'react-app-polyfill/ie9';
import 'react-app-polyfill/ie11';
import 'react-app-polyfill/stable';

I tried adding https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat#Polyfill to my index.js but it didn't do anything.

Did anyone else have similar problem?

icelic
  • 113
  • 2
  • 11
  • 1
    Using that polyfill for IE works for me. – Keith Feb 28 '20 at 14:03
  • Have you imported the polyfill at the first line in index.js? Have you followed the steps in [this answer](https://stackoverflow.com/questions/56435589/starter-create-react-app-with-ie11-polyfill-import-still-aborts-in-ie11/56439822#56439822) to support react app in IE 11? Besides, you could use [this polyfill](https://www.npmjs.com/package/abortcontroller-polyfill) for `AbortController`. If still not working, please provide [a minimal, reproducible code sample](https://stackoverflow.com/help/minimal-reproducible-example) so that we can have a test and see how to help. – Yu Zhou Mar 02 '20 at 05:43

2 Answers2

3

To add a String.repeat polyfill for IE11, I suggest using the core-js library to polyfill missing features.

Install core-js by running the following command:

npm install --save core-js@3.6.5

Inside of src/index.js, import the appropriate polyfill at the very top:

import 'react-app-polyfill/ie11';
import 'react-app-polyfill/stable';
import 'core-js/features/string/repeat';

import React from 'react';
// ...

As for AbortController, install it by running the following command:

npm install --save abortcontroller-polyfill

Edit your src/index.js file to import it:

import 'react-app-polyfill/ie11';
import 'react-app-polyfill/stable';
import 'core-js/features/string/repeat';
import 'abortcontroller-polyfill';

import React from 'react';
// ...
goto
  • 4,336
  • 15
  • 20
0

This is for IE's ES6 incompatibility issues.

Add following polyfills with npm

promise-polyfill
unfetch
abortcontroller-polyfill

and import them like

import Promise from "promise-polyfill";
import fetch from 'unfetch';
import 'abortcontroller-polyfill';

for Abortcontroller

& For Object.repeat Copy and paste MDN's polyfill code into very first of you JS file. That will do.

EDIT For React this should look like this (you can do it in index.js)

// import polyfills
import 'react-app-polyfill/stable';
import 'react-app-polyfill/ie11';

import Promise from "promise-polyfill";
import fetch from 'unfetch';
import 'abortcontroller-polyfill';
Md. A. Apu
  • 769
  • 10
  • 30