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.

Custom Host e.g. localhost.enterprise.com (HTTPS)

See original GitHub issue

I’m using msw 0.15.7 with create-react-app using a custom HOST (via an environment variable), e.g. HOST=localhost.myenterprise.com. This causes the issue below. If I remove the host environment variable and just go with plain old localhost:3000, everything works fine. Can using a custom host be supported?

Unhandled Rejection (TypeError): Cannot read property 'addEventListener' of undefined:
(anonymous function)
<omitted>/node_modules/msw/lib/esm/index.js:1791
(anonymous function)
<omitted>/node_modules/msw/lib/esm/index.js:51
__awaiter
<omitted>/node_modules/msw/lib/esm/index.js:31
start
<omitted>/node_modules/msw/lib/esm/index.js:1789

Module../src/mocks.js
> 26 | start();

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
kettanaitocommented, May 9, 2020

Glad to help. I’ll use this ticket as an opportunity to provide a better error message when this situation happens. A guidance like “Service Workers are not supported, or you are using a custom hostname” may be helpful for the user.

1reaction
kettanaitocommented, May 9, 2020

I’ve tried to reproduce this issue and was successful. The first thing I’ve noticed, is that when running under a custom hostname, the navigator.serviceWorker is undefined. This is the technical reason behind the exception you’ve attached.

However, the actual reason for the issue is that Service Workers can only be run over a secure connection (localhost is whitelisted).

Solutions

(Option 1) Use HTTPS for development

Recommended. If you establish your local server using HTTPS, it will be considered secure by the browser, allowing you to register Service Workers.

To use HTTPs you would need to have an SSL certificate. You can generate a self-signed certificate for development, or reuse your existing certificate, if any.

(Option 2) Whitelist insecure host

Be cautious with this option. It implies whitelisting your custom host so that browser considers it secure. Do not whitelist hosts you don’t know.

  1. Open the Chrome flags, and navigate to the “Insecure origins treated as secure” (chrome://flags/#unsafely-treat-insecure-origin-as-secure) setting.
  2. Mark the setting as “Enabled”.
  3. Put the full URL of your custom hostname (http://localhost.myenterprise.com:3000) in the textarea below.
  4. Click “Relaunch” at the bottom of the page to restart the browser.

There is nothing I can do on to handle this on the library’s side, as this issue comes from the Service Worker’s behavior per specification.

Please let me know which option you’ve chosen, and whether it helped. Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Edit Your Hosts File in Windows 10 - Liquid Web
The hosts file is usually the first process in the domain name resolution procedure. Here is an example of a hosts file entry....
Read more >
Configure Virtual Host with XAMPP Running on Windows 10
This tutorial provides an overview of Windows 10 with XAMPP virtual host configuration and how it plays an important role in the modern ......
Read more >
Enabling SSL on your web server—Installation Guides (10.3 ...
In order to use SSL, you need to obtain an SSL certificate and bind it to the website that hosts the Web Adaptor....
Read more >
Connect a custom domain | Firebase Hosting - Google
@; The apex domain name (for example, example .com ); Leaving the Host field blank. Subdomain.
Read more >
Chapter 1. Setting up the Apache HTTP web server
Configuring Apache name-based virtual hosts; 1.8. ... HTTP cache using, for example, memcache backend; mod_md - an ACME protocol SSL/TLS certificate service.
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