ホーム > Laravel > 【Laravel】How to Fix net::ERR_EMPTY_RESPONSE Error
Laravel

【Laravel】How to Fix net::ERR_EMPTY_RESPONSE Error

Thank you for your continued support.
This article contains advertisements that help fund our operations.

This article explains how to resolve the net::ERR_EMPTY_RESPONSE error when developing with Vite in Laravel.

The Error Encountered

GET http://127.0.0.1:5173/resources/js/Pages/Welcome.tsx net::ERR_EMPTY_RESPONSE
GET http://127.0.0.1:5173/@react-refresh net::ERR_EMPTY_RESPONSE
GET http://127.0.0.1:5173/resources/js/app.tsx net::ERR_EMPTY_RESPONSE
GET http://127.0.0.1:5173/@vite/client net::ERR_EMPTY_RESPONSE

Solution

Add the following configuration to vite.config.js:

export default defineConfig({
  // Added lines
  server: {
    host: true,
    hmr: {
      host: "localhost",
    },
  }, // End of added lines
  plugins: [
    laravel({
      input: "resources/js/app.tsx",
      refresh: true,
    }),
    react(),
  ],
})

Explanation of the Solution

This issue occurred because the environment was set up using Docker, requiring changes to Vite's communication settings.

Reason for the Fix

  • Setting server.host: true allows the server to be accessible even from Docker or remote environments.
  • Setting hmr.host: "localhost" ensures that HMR communicates with the browser using the correct hostname.

server.host

By default, Vite starts the server on localhost (127.0.0.1).
By setting host: true, the Vite server becomes accessible from other devices or hosts on the network.
This is particularly necessary for environments hosted on Docker or remote development servers.

hmr.host: "localhost"

HMR (Hot Module Replacement) enables real-time reflection of code changes in the browser during development.
The hmr.host setting specifies the hostname used by WebSocket connections for HMR.

This ensures that the browser connects to the HMR server using the correct URL.
For example, if the hostname is incorrect, HMR may not work, leading to errors.

I hope this information is helpful!

Please Provide Feedback
We would appreciate your feedback on this article. Feel free to leave a comment on any relevant YouTube video or reach out through the contact form. Thank you!