-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
fix(hmr): respect server https options when running as middleware #1992
Conversation
28e8057
to
fee6481
Compare
Congrats on 2.0 going GA! Would be real great to get this in -- anything more I should do? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moves the https config into a top level key of the resolved config, so its only resolved once (and only one set of certs is generated) and can be shared between different consumers
I don't think this is necessary. resolveConfig
is called for all commands including build and preview, so resolving https (which is dev server only) in resolveConfig
is definitely not ideal.
The https options should be resolved in server/index.ts
and then passed to resolveHttpServer
and createWebSocketServer
.
Ah, that totally makes sense, my bad. The reason I did it this way is because both those inner functions need access to the resolved config anyways, and |
I think we can extract a |
Ok, updated! |
Would love to get this in! |
c15ac3d
to
1a96ed3
Compare
Rebased this on top of latest |
ping @yyx990803 anything else I can do to get this in? |
Referencing this issue cytopia/devilbox#797 and hope it will be sorted with this merge. |
@airhorns How can add your commit 1a96ed3 to When I run I assume this is because Vite is compiled so that the folder structure with the changed files in it is not there - meaning I cannot just copy and paste the changes in locally installed Vite. Kinda wanted to give this a spin and see if that sorts things for the setup I use. |
Could |
@Shinigami92 I am not sure, from that info, but what I understand is, I should clone the entire monorepo and thus have the correct folders and files locally and then also add the changes locally by checking out the commit above in a branch named how I like, is that what you mean? |
Oh maybe I misunderstood something
assuming you currently working on a PR or so and just want to test something out But maybe you can try https://www.npmjs.com/package/patch-package for now |
Anyone available to help get this merged? There's another fix on top I would like to add that applies the same find-a-free-port logic that the main vite server has to the HMR port as well so you can start multiple instances if need be. |
Any chance on having this merged soon, to fix the infinite redirect on mobile Safari? |
@airhorns would you merge main to this branch? We completed the migration to GitHub CI and tests should be green after you update the PR |
If you are running vite over https, HMR works great in the standalone mode, but when in middleware mode, the middleware might be served over https and the websocket server wouldn't listen on https. This updates the websocket server to respect the config.server.https option when in middleware mode too.
Ok, updated to be on top of |
SvelteKit now works with Devilbox, secure local dev domain and HMR! svelte.config.js import { readFileSync } from 'fs';
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte',
vite: () => ({
server: {
host: '0.0.0.0',
port: 3000,
https: {
key: readFileSync('/shared/httpd/devilbox/ca/certs/mass/skdevkit.loc.key'),
cert: readFileSync('/shared/httpd/devilbox/ca/certs/mass/skdevkit.loc.crt')
},
hmr: {
host: 'skdevkit.loc',
protocol: 'wss',
port: 24678
}
}
})
}
};
export default config;
Thank you! |
@airhorns -- I've been researching SvelteKit HMR issues over HTTPS. SvelteKit operates in Vite's middleware mode. Even with your change, we're still experiencing constant refreshing when using self-signed certificates. There's kind of an ugly workaround explained here. It also works with a proposed fix to SvelteKit, but requires using an undocumented option (server.hmr.server), passing the reference of our HTTPS server. Although this works like a charm, there's hesitancy about using an option that isn't in the documentation. Could you share your configuration for using HTTPS and HMR for unsigned certificates in middleware mode? Thanks in advance. |
I use signed certificates generated with {
server: {
https: {
key: fs.readFileSync(path.join(...)),
cert: fs.readFileSync(path.join(...)),
}
}
} I think the browser might be aborting your connection to the self signed cert unless you have convinced it to accept it? |
Yes, that is the issue with self-signed certificates -- The wss:// connection automatically uses https:// to authorize, but this process isn't associated with a browser window in order to accept it. It works perfectly when I pass a reference to SvelteKit's created server via the vite.server.hmr.server option, but this parameter isn't documented on Vite's website. The initial https:// request (for the web socket) is sent to that server, the "Your connection is not private" dialog is presented once, and, after acceptance, allows the both the web app and hmr to proceed. I think I'll create an issue for updating the documentation, and see what the "powers-that-be" decide. I'm somewhat new to Github and open-source projects, so I'm a bit unfamiliar with the process. In any event, thanks for your reply -- I wanted to confirm whether your original issue addressed self signed certificates for middlewareMode setups. |
@svelte/kit@next: 1.0.0-next.178. vite: {
server: {
host: '0.0.0.0',
port: 3000,
// ??
// https: {
// key: readFileSync('app.loc.key'),
// cert: readFileSync('app.loc.crt')
// }
},
hmr: {
protocol: 'wss',
host: 'app.loc',
port: 443,
server: {
host: '??',
port: '??',
// ??
https: {
key: readFileSync('app.loc.key'),
cert: readFileSync('app.loc.crt')
}
}
}
} |
Using Devilbox - Docker remote environment, it works with this simple config, running /** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte',
vite: {
server: {
host: '0.0.0.0'
}
}
}
};
export default config; What confused me was that SvelteKit did work just fine with HMR under https://app.loc/ without having a port number in the URL in the past. Being able to tell Vite to use the cert from https://app.loc instead of making a new one for localhost on start could possibly fix this issue. There is an option for vite.server.hmr.server where I assume this could be solved however the option does not seem to work with a self signed cert so far. import { readFileSync } from 'fs';
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte',
vite: {
server: {
host: '0.0.0.0'
},
hmr: {
server: {
https: {
key: readFileSync('app.loc.key'),
cert: readFileSync('app.loc.crt')
}
}
}
}
}
};
export default config; |
If you are running vite over https, HMR works great in the standalone mode, but when in middleware mode, the middleware might be served over https and the websocket server wouldn't listen on https. This updates the websocket server to respect the config.server.https option when in middleware mode too.
config.server
https option is on