Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hmr): #3093 add config.server.hmr.runtimePort option #3115

Closed
wants to merge 4 commits into from

Conversation

Sociosarbis
Copy link
Contributor

@Sociosarbis Sociosarbis commented Apr 23, 2021

Description

fix #3093
add an option named runtimePort in config.server.hmr . The value of runtimePort will replace __HMR_PORT__ literal.

Additional context

Is this a good option design ? Is a test case needed?


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@Shinigami92 Shinigami92 added the p2-nice-to-have Not breaking anything but nice to have (priority) label Apr 23, 2021
Shinigami92
Shinigami92 previously approved these changes Apr 24, 2021
@@ -415,12 +415,14 @@ export default async ({ command, mode }) => {

### server.hmr

- **Type:** `boolean | { protocol?: string, host?: string, port?: number, path?: string, timeout?: number, overlay?: boolean }`
- **Type:** `boolean | { protocol?: string, host?: string, port?: number, path?: string, timeout?: number, overlay?: boolean, runtimePort?: string }`
Copy link
Member

Choose a reason for hiding this comment

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

The port is a number, so we should stick to that:

Suggested change
- **Type:** `boolean | { protocol?: string, host?: string, port?: number, path?: string, timeout?: number, overlay?: boolean, runtimePort?: string }`
- **Type:** `boolean | { protocol?: string, host?: string, port?: number, path?: string, timeout?: number, overlay?: boolean, runtimePort?: number }`

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because runtimePort is resolved on the browser. runtimePort is meaningless when it's a number.

Comment on lines 26 to 44
let port
if (config.server.middlewareMode) {
port = String(
(typeof config.server.hmr === 'object' && config.server.hmr.port) ||
24678
)
} else {
port = String(options.port || config.server.port!)
}
let hmrBase = config.base
if (options.path) {
hmrBase = path.posix.join(hmrBase, options.path)
}
if (hmrBase !== '/') {
port = path.posix.normalize(`${port}${hmrBase}`)
if (!options.runtimePort) {
if (config.server.middlewareMode) {
port = String(
(typeof config.server.hmr === 'object' &&
config.server.hmr.port) ||
24678
)
} else {
port = String(options.port || config.server.port!)
}
let hmrBase = config.base
if (options.path) {
hmrBase = path.posix.join(hmrBase, options.path)
}
if (hmrBase !== '/') {
port = path.posix.normalize(`${port}${hmrBase}`)
}
}
Copy link
Member

Choose a reason for hiding this comment

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

With this logic we're losing appending the hmrBase Was that intended?

It's also less readable to move the runtimePort check until later.

This would retain the behaviour:

let port
if (options.runtimePort != null) {
  port = runtimePort
} else if (config.server.middlewareMode) {
  port = String(
    (typeof config.server.hmr === 'object' && config.server.hmr.port) || 24678,
  )
} else {
  port = String(options.port || config.server.port!)
}

let hmrBase = config.base
if (options.path) {
  hmrBase = path.posix.join(hmrBase, options.path)
}
if (hmrBase !== '/') {
  port = path.posix.normalize(`${port}${hmrBase}`)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing this out. I will append hmrBase in my next commit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

runtimePort = options.runtimePort
if (hmrBase !== '/') {
runtimePort += ` + ${JSON.stringify(path.posix.normalize(hmrBase))}`
}

I have gotten hmrBase back.

@@ -47,7 +50,7 @@ export function clientInjectionsPlugin(config: ResolvedConfig): Plugin {
.replace(`__DEFINES__`, serializeDefine(config.define || {}))
.replace(`__HMR_PROTOCOL__`, JSON.stringify(protocol))
.replace(`__HMR_HOSTNAME__`, JSON.stringify(host))
.replace(`__HMR_PORT__`, JSON.stringify(port))
.replace(`__HMR_PORT__`, options.runtimePort ?? JSON.stringify(port))
Copy link
Member

Choose a reason for hiding this comment

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

With the changes above, this no longer needs to be changed:

Suggested change
.replace(`__HMR_PORT__`, options.runtimePort ?? JSON.stringify(port))
.replace(`__HMR_PORT__`, JSON.stringify(port))


Disable or configure HMR connection (in cases where the HMR websocket must use a different address from the http server).

Set `server.hmr.overlay` to `false` to disable the server error overlay.

Set `server.hmr.runtimePort` to any runtime code which interpreted as a port. e.g. `'window.location.port'` and `(() => window.location.port)()`. If this is set, `server.hmr.port` will be ignored.
Copy link
Member

Choose a reason for hiding this comment

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

This would not work, would it? The config is being resolved on the server/in the plugin, not on the browser.

Suggested change
Set `server.hmr.runtimePort` to any runtime code which interpreted as a port. e.g. `'window.location.port'` and `(() => window.location.port)()`. If this is set, `server.hmr.port` will be ignored.
Set `server.hmr.runtimePort` to the port that the client should connect to. This is useful when a connection is proxied and the external port differs from the `server.hmr.port`.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

runtimePort is useful when port can't be determined in advance. As mentioned in [#3093]#3093, the port of proxy is generated randomly. runtimePort is port resolved in runtime.

Copy link
Member

Choose a reason for hiding this comment

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

Right, I misunderstood. Edited my comment here to continue the discussion: #3115 (comment)

@nihalgonsalves
Copy link
Member

nihalgonsalves commented Apr 24, 2021

If the goal is to resolve #3093, you'd have to define something directly here:

https://github.com/vitejs/vite/blob/c0a0a8427e61dc0f5774056aebd038c69492294a/packages/vite/src/client/client.ts

I'm wondering whether a static port is sufficient though.


Edit: I now understand that you proposed to add an inline code snippet that's replaced as a string. I'm not sure if this is the right approach, as it's a bit of a surprising configuration method. Thoughts @patak-js @Shinigami92?

@Shinigami92
Copy link
Member

Yeah, I'm not sure about this, but also not really biased 🤔
If it helps it helps, but I would also prefer a real callback or something like that instead of a stringyfied version of a callback/variable
Anyways, this is kinda a niche feature

@jinghm318
Copy link

jinghm318 commented May 7, 2021

@nihalgonsalves @Sociosarbis So how's it now? I'm eager to get this feature.

#3011

@antfu
Copy link
Member

antfu commented May 7, 2021

Related #2921

@antfu antfu mentioned this pull request May 28, 2021
9 tasks
@Shinigami92
Copy link
Member

We decided that #3578 is the successor of this PR

@Shinigami92 Shinigami92 added the duplicate This issue or pull request already exists label May 28, 2021
@johnnysprinkles
Copy link
Contributor

Apologies, I did a bad job of searching for existing PRs! I'm making suggested changes in that #3578 today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists feat: hmr p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Vite HMR is unusable behind reverse proxies with random port numbers for client
6 participants