WebSocket connection to 'ws:app_url/_next/webpack-hmr' failed: WebSocket is closed before the connection is established
Matthew C.
—After upgrading to Next.js version 12+, you may get the following error in your browser dev tools console when running your development server:
WebSocket connection to 'wss://app_url/_next/webpack-hmr' failed: Websocket is closed before connection is established
From version 12 onward, Next.js uses a WebSockets connection for Hot Module Replacement (HMR) instead of a server-sent events connection. The above error occurs when an incorrectly configured proxy server disrupts this WebSockets connection.
If you are using a proxy or a custom server to serve a Next.js app over HTTPS during development (for example, so that you can use an API that requires HTTPS), you need to configure your server to handle WebSockets traffic without connection disruptions.
This solution demonstrates the correct configuration for three common proxy servers: NGINX, Apache, and custom servers.
If you’re using NGINX, add the following configuration:
location /_next/webpack-hmr { proxy_pass http://localhost:3000/_next/webpack-hmr; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; }
This informs the NGINX proxy server that the client’s HTTP protocol will be upgraded to a WebSockets connection. The headers have to be passed explicitly.
Consult the NGINX docs to learn more about WebSocket proxying.
If you’re using Apache (2.x), you can add the following configuration to enable WebSockets to the server:
<VirtualHost *:443> # ServerName yourwebsite.local ServerName "${WEBSITE_SERVER_NAME}" ProxyPass / http://localhost:3000/ ProxyPassReverse / http://localhost:3000/ # Next.js 12 uses websocket <Location /_next/webpack-hmr> RewriteEngine On RewriteCond %{QUERY_STRING} transport=websocket [NC] RewriteCond %{HTTP:Upgrade} websocket [NC] RewriteCond %{HTTP:Connection} upgrade [NC] RewriteRule /(.*) ws://localhost:3000/_next/webpack-hmr/$1 [P,L] ProxyPass ws://localhost:3000/_next/webpack-hmr retry=0 timeout=30 ProxyPassReverse ws://localhost:3000/_next/webpack-hmr </Location> </VirtualHost>
Consult the Apache docs to learn more about upgrading an HTTP connection to a WebSockets connection.
If you’re using a custom server, you may need to ensure that the request is passed correctly. In an Express app, you can pass the request to a Next.js request handler:
app.all('/_next/webpack-hmr', (req, res) => { nextjsRequestHandler(req, res) })
Next.js version 12+ uses the WebSockets connection for HMR because it speeds up the development process by updating an app in the development server whenever its source code is changed.
For example, when you edit your app source code while the development server is running, HMR updates the app in the development server, by replacing, adding, or removing modules without needing a full reload.
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.