question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Uncaught ReferenceError: regeneratorRuntime is not defined

See original GitHub issue

Library Affected: workbox-webpack-plugin

Browser & Platform: Was found on Chrome v51

Bug: I am using this plugin like so:

new GenerateSW({
    babelPresetEnvTargets: ["last 4 versions", "safari >= 7"],
    cacheId: 'sw',
    cleanupOutdatedCaches: true,
    clientsClaim: true,
    exclude: [/\.map$/, /stats\.json$/],
    inlineWorkboxRuntime: true,
    runtimeCaching: [
        {
            urlPattern: /^https:\/\/fonts\.googleapis\.com.*/,
            handler: 'StaleWhileRevalidate',
        },
    ],
    skipWaiting: true,
    sourcemap: false,
    swDest: 'sw.js',
}),

This error is coming through into the browser console: Uncaught ReferenceError: regeneratorRuntime is not defined

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:17 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
jeffposnickcommented, May 15, 2020

When you set babelPresetEnvTargets, there’s a single transpilation performed, and a single service worker file output. You can think of it as meeting the requirements of the “lowest common denominator” based on your babelPresetEnvTargets configuration.

If your babelPresetEnvTargets configuration includes a browser that doesn’t have support for generators, then the single service worker file that’s output will include code that references regeneratorRuntime. If regeneratorRuntime isn’t available (which it wouldn’t be if you’re using a transpilation configured by Workbox), then the code will fail to run on any browser—it’s failing on modern browsers that have generator support because there’s now a dependency on the missing regeneratorRuntime.

As suggested, you can either perform your own transpilation after Workbox produces a service worker file, and in that transpilation, add in support for the regeneratorRuntime (check the babel documentation for exactly how you’d do that). Alternatively, you could ensure that you set Workbox’s babelPresetEnvTargets to only include browsers that have generator support, and the service worker file that Workbox creates should work across all of those browsers.

I’m leaving this issue open because it’s definitely something that we could help prevent from happening—I think Workbox could conditionally add the regeneratorRuntime to the service workers it transpiles without incurring a larger bundle size when it’s not needed. But what I’m offering in the meantime is some workarounds that should address your issue.

0reactions
jeffposnickcommented, Mar 11, 2021

Hey all. Sorry for not updating this for a while.

@phil-w, the babelPresetEnvTargets setting is available when you’re using the GenerateSW plugin.

If you’re using the InjectManifest plugin, then any Babel plugins from the parent compilation is applied to the child compilation that outputs your service worker.

@mycolaos, if you could point me to a GitHub project or some other code archive that includes a full setup which reproduces the problem you’re seeing, I’ll dive into it and tell you what needs to be configured where, and potentially see how those changes could be upstreamed to the source projects.

In general, one option that wasn’t mentioned yet is that if you want to pull in the regenerator runtime into your service worker, it can be explicitly loaded from, e.g., the unpkg CDN (or using a local copy that you maintain). The mechanism for loading it varies depending on which webpack plugin you’re using:

  • GenerateSW: add importScripts: ['https://unpkg.com/regenerator-runtime@0.13.7/runtime.js'] to your Workbox configuration.
  • InjectManifest: add importScripts('https://unpkg.com/regenerator-runtime@0.13.7/runtime.js'); to the top of your swSrc file.

Again, you can replace those URLs with a copy of the regenerator-runtime code served from a local URL if you’d rather not rely on a CDN.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix regeneratorRuntime is not defined?
I have ran into a problem, the error is regeneratorRuntime is not defined while working with React and Parcel bundler.
Read more >
Babel 6 regeneratorRuntime is not defined
I did not need babel-runtime to get async await working. Is that correct? Edit: I'm running the code server side. :) – GijsjanB....
Read more >
ReferenceError regeneratorRuntime is not defined #9849
I'm building an SSR template, and when I use @babebl/register and then execute webpack (config), the system reported an error.
Read more >
Parcel, how to fix the `regeneratorRuntime is not defined` ...
I run into this problem in a project using Babel as soon as I added an async function, but the problem is the...
Read more >
Blank Admin Page: regeneratorRuntime is not defined ...
Uncaught ReferenceError : regeneratorRuntime is not defined ... Still experiencing the issue while deploying with Heroku, no idea how to fix ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found