Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

fix(vite): assign different hmr port for each instance #3169

Merged
merged 2 commits into from Feb 10, 2022
Merged

Conversation

antfu
Copy link
Member

@antfu antfu commented Feb 10, 2022

πŸ”— Linked issue

Fix nuxt/nuxt#13325, Fix partially nuxt/nuxt#11956

❓ Type of change

  • πŸ“– Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

Thanks a lot for @danielroe's help!

The root cause of HMR breakage is because in middleware mode of Vite, the HMR port is fixed to 24678. Which means when you have multiple Vite/Nuxt instances running, it causes the later server to connect to the wrong Websocket instance, making the HMR fail to trigger. This PR makes the port dynamic with the currently available ports.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

Co-authored-by: danielroe <danielroe@users.noreply.github.com>
@netlify
Copy link

netlify bot commented Feb 10, 2022

βœ”οΈ Deploy Preview for nuxt3-docs canceled.

πŸ”¨ Explore the source changes: 1313921

πŸ” Inspect the deploy log: https://app.netlify.com/sites/nuxt3-docs/deploys/6205200acbc79d0007f212aa

@antfu antfu changed the title fix(vite): assign different hmr port for reach instance fix(vite): assign different hmr port for each instance Feb 10, 2022
const hmrPortDefault = 24678 // Vite's default HMR port
const hmrPort = await getPort({
port: hmrPortDefault,
ports: Array.from({ length: 20 }, (_, i) => hmrPortDefault + 1 + i)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not just use a random port?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I am just not sure about how random works. Could change it to random if you want to

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OS chooses one when port is 0

@pi0 pi0 merged commit 089160a into main Feb 10, 2022
@pi0 pi0 deleted the fix/vite-hmr branch February 10, 2022 16:37
@danielroe danielroe added the 3.x label Jan 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

vite HMR Broken for <template>
3 participants