fix(init): resolve localhost IP family for devServer allowedHosts#4715
Conversation
…config - Windows 10/11 can prioritize IPv6, making localhost resolve to ::1 instead of 127.0.0.1 - This causes HMR WebSocket connections to silently fail when the bound address doesn't match - Templates now resolve the OS-preferred IP family at generation time using dns.lookup() - Resolved value is baked into allowedHosts (127.0.0.1 or ::1) - If resolution fails, allowedHosts is omitted and webpack-dev-server falls back to its own 'auto' behavior - Updated templates: default, vue, react, svelte - Updated snapshots
|
Codecov Report✅ All modified and coverable lines are covered by tests.
Additional details and impacted files@@ Coverage Diff @@
## main #4715 +/- ##
===========================================
- Coverage 91.43% 25.80% -65.63%
===========================================
Files 14 14
Lines 4716 4716
Branches 679 122 -557
===========================================
- Hits 4312 1217 -3095
- Misses 402 3497 +3095
Partials 2 2 see 13 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
🚀 New features to boost your workflow:
|
|
We don't need to do it, it is out of scope our basic generation |
|
@alexander-akait okay, Just out of curiosity, isn't it counter-intuitive that the server runs on |
|
@ThierryRakotomanana Sorry I don't understand you, we are not blocking 127.0.0.1/::1/localhost |
|
@alexander-akait My PR description might have been unclear, sorry for that, so to simplify: currently, when running a fresh project via the cli a loop who alway tray do reconnect to the server, as shown in the attached image, I've put on the PR description. I have confirmed this behavior across different OS : Windows 10, WSL, and native Ubuntu, and use different version of Node. |
Summary
When
host: "localhost"is set in the generatedwebpack.config.js, webpack-dev-server lets the OS decide at bind time whetherlocalhostresolves to127.0.0.1(IPv4) or::1(IPv6).On Windows 10/11 with modern network stacks, IPv6 is often prioritized, meaning
localhost→::1. IfallowedHostsis not explicitly set, webpack-dev-server's security checks can block WebSocket connections. This is precisely why you might see those persistent disconnection loops in the console as shown on the image bellowWhat kind of change does this PR introduce?
This PR esolve the OS-preferred IP family. By asking the system's native DNS what it actually prefers in the moment, we can grab the
primary IP familyand include it directly in theallowedHostslist.This makes the template adaptable to your specific environment. Whether you have
IPv6at the top of your priority list or you’ve switched back toIPv4, the server will recognize the address it's running on and allow the traffic through smoothly.Did you add tests for your changes?
I've update the snapshot test in
initDoes this PR introduce a breaking change?
not really, just a fix and features to detect the local IP adress bound by the OS
If relevant, what needs to be documented once your changes are merged or what have you already documented?
Not necesserary
Use of AI
Partly, in order to understand the real issue.