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

Endless reloading by start svelte-kit it with VS Code Remote-SSH terminal #1134

Closed
alexander-mart opened this issue Apr 19, 2021 · 47 comments
Closed
Labels
bug Something isn't working vite

Comments

@alexander-mart
Copy link
Contributor

alexander-mart commented Apr 19, 2021

Describe the bug

Endless reloading by start it from VS Code Remote-SSH terminal:
npm run dev -- --open
(open localhost:3000 on local computer and svelte is running on remote)

Termorary solution:
With --host it works great:
npm run dev -- --host --open
(open 111.222.333.444:3000 (server ip) on local computer and svelte is running on remote)

Other projects works fine on the same server with same VS Code Remote-SSH, sapper-template, for example. Problem (for me) only with SvelteKit now.

Logs
Please include browser console and server logs around the time this bug occurred.

To Reproduce

  1. Open VS Code
  2. Install Remote-SSH (if it doesn't exist)
  3. Connect to you remote server with Remote-SSH
  4. pnpm init svelte@next my-app
  5. pnpm i
  6. npm run dev -- --open

Expected behavior
Work on VS Code Remote-SSH with port forwarding without endless reloading.

Stacktraces
If you have a stack trace to include, we recommend putting inside a <details> block for the sake of the thread's readability:

Stack trace

image

[vite] connecting...
content.js:1 Uncaught (in promise) Error: Unexpected token u in JSON at position 0
    at A (content.js:1)
A @ content.js:1
Promise.then (async)
(anonymous) @ content.js:1
(anonymous) @ content.js:1
t @ content.js:1
(anonymous) @ content.js:1
(anonymous) @ content.js:1
client:184 WebSocket connection to 'ws://localhost:24678/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
(anonymous) @ client:184
client:340 [vite] server connection lost. polling for restart...
(index):1 Uncaught (in promise) TypeError: Failed to fetch dynamically imported module: http://localhost:3001/src/routes/index.svelte?import
start.js:1024 Uncaught (in promise) TypeError: Failed to fetch dynamically imported module: http://localhost:3001/src/routes/$layout.svelte?import
async function (async)
start @ start.js:1018
(anonymous) @ (index):222

Information about your SvelteKit Installation:

Diagnostics
  • The output of npx envinfo --system --npmPackages svelte,@sveltejs/kit,vite --binaries --browsers

    System:
    OS: Linux 4.15 Ubuntu 18.04.4 LTS (Bionic Beaver)
    CPU: (2) x64 Intel Xeon E3-12xx v2 (Ivy Bridge, IBRS)
    Memory: 98.10 MB / 984.89 MB
    Container: Yes
    Shell: 4.4.20 - /bin/bash

    Binaries:
    Node: 14.16.1 - /usr/local/bin/node
    Yarn: 2.0.0-rc.27 - ~/.npm-global/bin/yarn
    npm: 6.13.7 - ~/.npm-global/bin/npm

    npmPackages:
    @sveltejs/kit: next => 1.0.0-next.80
    svelte: ^3.29.0 => 3.37.0

  • Your browser

    Chrome 88.0.4324.150 (Official), (64 bit)

Severity
Not critical, but it is incrase REPL from 1 second to 8 seconds :(
if i use the solution with adding --host as additional parameter.

@Conduitry
Copy link
Member

This is presumably the same thing as #844 in that an inability to access the websocket used for HMR is causing an endless reload.

@nielsvandermolen
Copy link

Opening port 24678 on the server and forwarding it in VSCode resolved the issue for me and enabled auto-reload. I use it with remote development with VSCode in Docker containers.

You want to see something like this in the Chrome console:

client.ts:43 [vite] connecting...   
client.ts:43 [vite] connected.

This error in the console says there is something wrong with the Vite connection:

WebSocket connection to 'ws://localhost:24678/' failed:    

Some background information

SvelteKit uses Vite for its hot module reload (HMR) feature. This uses a WebSocket that uses a different port than the one opened with Sveltekit (default 24678, but it is configurable, but the configuration seems to not apply with SvelteKit). When you fire up SvelteKit using npm run dev it starts the server and creates a Vite WebSocket that listens to connections.

Related topics

@vanlong441
Copy link

vanlong441 commented May 10, 2021

I just want to inform that this is still happening on @sveltejs/[email protected]

And opening port 24678 is working as a workaround

@alexander-mart
Copy link
Contributor Author

this is still happening on @sveltejs/[email protected]

@JBusillo
Copy link
Contributor

JBusillo commented May 30, 2021

@GreenRobot777 -- Are you opening up both your application port (default: 3000) and the websocket port (default 24678) on your remote server? This has been suggested as a "workaround" -- which really is a misstatement. It's absolutely necessary to manually open those ports, as the browser client (running on the local server) needs to connect to those ports to support HMR. SvelteKit can't alter your system's firewall settings. This necessity applies to Docker containers, too.
I've tested it on my local and remote servers with the default HMR port (24678) and again with an override (3334).

// svelte.config.js to override the default HMR port from 24678 to 3334
/** @type {import('@sveltejs/kit').Config} */
const config = {
	kit: {
		// hydrate the <div id="svelte"> element in src/app.html
		target: '#svelte',
		vite: {
			server: {
				hmr: {
					port: 3334,
				}
			}
		}
	}
};

export default config;

You also need to specify --host, otherwise it will only accept connections from your remote server's localhost, not from your "external" local server.

For me, I changed svelte.config.js as described above, then executed commands 1 through 5 in your Reproduction instructions, then:

sudo ufw allow 3333          # My prod server already uses 3000 for another app, so I had to use another port
sudo ufw allow 3334          # Testing Vite's HMR port override feature.
npx svelte-kit dev --host --port 3333

If you just want to use the default ports, omit changing your svelte.config.js, and run:

sudo ufw allow 3000          
sudo ufw allow 24678
npx svelte-kit dev --host 

I hope I understood your issue correctly. Please let me know. If this does resolve your issue, it probably can be closed the remedy for this issue would be clarification in the documentation. (FAQ?)

@drejohnson
Copy link

I'm having this issue using WSL2 and Nginx as a reverse proxy with https and custom domain (http://localhost:3000 -> https://svltkt.local).

Opening the ports and using --host as suggested above works for host IP but not custom domain added the C:\Windows\System32\drivers\etc\hosts. Any suggestions?

@JBusillo
Copy link
Contributor

@drejohnson Which SvelteKit version are you on? The above workaround was for http, however the latest version of SvelteKit has fixes for https. You probably want to upgrade.

@drejohnson
Copy link

@JBusillo I'm on 1.0.0-next.114. Still doesn't work for custom domains (<http:https>://svltkt.local). <http:https>://localhost:3000 works as expected.

@JBusillo
Copy link
Contributor

next.114 does have the modified code. BTW, there was a next.115 just published less than an hour ago, but that shouldn't affect this. Do you want to go over this using Discord private messaging? Then we can post our resolutions back in this issue. My Discord id is "Joe Busillo#8928".

@Kapsonfire-DE
Copy link
Contributor

working example of nginx vhost via wsl2
image

@taffit
Copy link

taffit commented Aug 9, 2021

Just to mention: I had the same problem with a custom domain. As soon as I tried to invoke the application with a custom domain, it started reloading the page every 2-5 seconds. HMR-port was open as stated above, no ports were blocked by some firewall, it even happened when I started it locally with pnpm dev (having the --host-switch as well).
What helped in my case was the plugin vite-plugin-host, which explicitly mentions to enable a custom domain in dev as well. Once imported and configured in the svelte.config.js, also the custom domain worked fine.

@scomma
Copy link

scomma commented Aug 13, 2021

Still happening on 1.0.0-next.146

@alexbruno
Copy link

alexbruno commented Sep 7, 2021

Still happens with 1.0.0-next.164, but a little different for me.
No endless reload, but HMR is not working.
Looking at DevTools, the websocket connection continues pending.

image

@generalmarty
Copy link

Specifying the forwarding ports in devcontainer.json resolves the ws connection problem for me.

"forwardPorts": [3000, 24678],

@alexbruno
Copy link

alexbruno commented Sep 10, 2021

Not for me, HMR still doesn't work even with update to 1.0.0-next.164.

None of these workarounds should be necessary, VSCode Dev Container detects and forwards all ports automatically, no setup needed:

image

And the manually opening ports solution suggested by @JBusillo also shouldn't be necessary exactally because this same reason.

I'm working on a @vue/cli project running inside a VSCode Dev Container (Docker) inside WSL2 Alpine Linux on Windows 10. And the ports are automatically opened and forwarded, server and HMR work out of the box, no extra setup.

But @sveltejs/kit can serve only the base application port but not the WS one for HMR.

@annmarie-switzer
Copy link

annmarie-switzer commented Sep 17, 2021

I think next.169 broke something in relation to this. When I upgraded to next.169 this afternoon, the websocket could not be reached anymore and the page would endlessly reload. I reverted back to next.164 and everything is working again as intended.

Here are my svelte config file and docker compose file. Again, this works with next.164 but does not work with next.169:

svelte.config.js

import preprocess from 'svelte-preprocess';

import adapter from '@sveltejs/adapter-static';

/** @type {import('@sveltejs/kit').Config} */
const config = {
    // Consult https://github.com/sveltejs/svelte-preprocess
    // for more information about preprocessors
    preprocess: preprocess(),
    kit: {
        adapter: adapter(),
        ssr: false,
        target: '#svelte',
        vite: {
            server: {
                https: {
                    key: '/certs/key.pem',
                    cert: '/certs/cert.pem'
                },
                hmr: {
                    protocol: 'wss',
                    port: 24678
                }
            }
        }
    }
};

export default config;

docker-compose.yml

  ui:
    container_name: ui
    image: ui-${UI_MODE}
    build:
      context: ../ui
      dockerfile: Dockerfile.${UI_MODE}
    restart: always
    ports:
      - 24678:24678
    volumes:
      - ../ui/src/:/app/src
      - ../ui/static:/app/static
      - ./configs/certs/mycert.pem:/certs/cert.pem
      - ./configs/certs/mycert-key.pem:/certs/key.pem
    tty: true

@moisesbites
Copy link

moisesbites commented Sep 18, 2021

I think next.169 broke something in relation to this. When I upgraded to next.169 this afternoon, the websocket could not be reached anymore and the page would endlessly reload. I reverted back to next.164 and everything is working again as intended.

I don't have this reload in next.168 using browser and svelte in a docker container. But in next.169, the browser is reloading.

I saw this document: https://github.com/sveltejs/svelte-hmr#noreload. Maybe it's a workaround, but I don't see how I could use this.

@gyurielf
Copy link
Contributor

I think next.169 broke something in relation to this. When I upgraded to next.169 this afternoon, the websocket could not be reached anymore and the page would endlessly reload. I reverted back to next.164 and everything is working again as intended.

I don't have this reload in next.168 using browser and svelte in a docker container. But in next.169, the browser is reloading.

I saw this document: https://github.com/sveltejs/svelte-hmr#noreload. Maybe it's a workaround, but I don't see how I could use this.

I ran into it, and I got the same. I tested it as well.

@moisesbites
Copy link

moisesbites commented Sep 21, 2021

I made some tests on "@sveltejs/kit": "1.0.0-next.169/170"/ "@sveltejs/adapter-node": "1.0.0-next.49".

// svelte.config.js
kit: {
    adapter: node(),
    target: '#svelte',
    ssr: false,
    hostHeader: 'X-Forwarded-Host',
    vite: {
      server: {
        hmr: {
          host: 'localhost',
          protocol: 'ws',
          port: 3001
        }
      }
....
  }
# docker-compose.yaml
  frontend:
    image: frontend-0.0.151
    user: node
    working_dir: /home/node/app
    volumes:
      - ./frontend:/home/node/app
    command: npm run dev
    ports:
      - '3001:3001'
    networks:
      - frontend

response for ws://localhost:3001/ using "@sveltejs/kit": "1.0.0-next.168"/ "@sveltejs/adapter-node": "1.0.0-next.48". It's working correctly.

<anonymous>
https://localhost/@vite/client:186:16
InnerModuleEvaluation self-hosted:2381:31
InnerModuleEvaluation self-hosted:2381:31
InnerModuleEvaluation index.svelte:80:24
InnerModuleEvaluation index.svelte:124:13
evaluation self-hosted:2332:24 

Response for ws://localhost:3001/ using "@sveltejs/kit": "1.0.0-next.170"/ "@sveltejs/adapter-node": "1.0.0-next.49". The browser reloading after this request.

<anonymous>
client.ts:28:15
InnerModuleEvaluation self-hosted:2381:31
InnerModuleEvaluation self-hosted:2381:31
InnerModuleEvaluation self-hosted:2381:31
InnerModuleEvaluation self-hosted:2381:31
evaluation self-hosted:2332:24 

The browser always reload after it try to access ws://localhost:3001/ using svelte.kt 169 or 170. Using 168 it don't reload.

My frontend container is behind haproxy with self sign certificate. Only the port 3001 is open for vite without certificate.

I'm not accessing via SSH but via normal browser.

What was changed in Vite/Svelte from 168 to 169 version?

Thank you for the help.

@alexbruno
Copy link

alexbruno commented Sep 22, 2021

lol
In my case, I'm running now with "@sveltejs/kit": "1.0.0-next.170" and it works out of the box.
The issue is fixed to me.

@taffit
Copy link

taffit commented Sep 23, 2021

Today using "svelte": "^3.43.0", "vite-plugin-host": "^1.0.2" (which in the past helped me with the reloading issue and the custom domain in the past, see above) and "@sveltejs/kit": "next" (resolving to @sveltejs/kit 1.0.0-next.171) I'm getting the following warning during startup with pnpm dev:

The value for kit.vite.server.host specified in svelte.config.js
has been ignored. This option is controlled by SvelteKit.

And now it starts reloading endlessly again. I have a custom kit.vite.server domain-name entered in the svelte.config.js:

...
      server: {
        host: '0.0.0.0',
        hmr: {
          host: 'myapp.local.host',
          port: 24678,
        },
      },
...

Edit: Removing the kit.vite.server-part as a whole from the svelte.config.js fixed the problem. I even didn't have to set the kit.host-parameter. Working for pnpm dev and a custom domain (without any adapter involved for now).

@moisesbites
Copy link

moisesbites commented Sep 23, 2021

Edit: Removing the kit.vite.server-part as a whole from the svelte.config.js fixed the problem. I even didn't have to set the kit.host-parameter. Working for pnpm dev and a custom domain (without any adapter involved for now).

This didn't work for me.

@moisesbites
Copy link

After many tests and configurations, this solved the endless reload:

// "@sveltejs/adapter-node": "^1.0.0-next.51",
// "@sveltejs/kit": "^1.0.0-next.174",
// "svelte": "^3.43.0",
// svelte.config.js
      server: {
        hmr: {
          host: 'localhost',
          protocol: 'wss',
          port: 443
        }
      }

Ref.: vitejs/vite#1653 (comment)

@robots4life
Copy link

#2510 (comment)
4 days ago I had a version that would still work, 172 I think, then updated and none of the options listed work.

@annmarie-switzer 👍

I reverted back to next.164 and everything is working again as intended.

I had a version that was working, updated that and tried so much the past few days.. nothing works.. uff.. how do you revert back to next.164 ? I think for now I just like to stick with a version that works for remote development, get going with the project and wait for 1.0. Or how does one/do you go about this ?

@taffit

What helped in my case was the plugin vite-plugin-host, which explicitly mentions to enable a custom domain in dev as well. Once imported and configured in the svelte.config.js, also the custom domain worked fine.

Did you put this in the svelte.config.js file? Don't know how to use this, care to post an example usage ?

@moisesbites

After many tests and configurations, this solved the endless reload:

// "@sveltejs/adapter-node": "^1.0.0-next.51",
// "@sveltejs/kit": "^1.0.0-next.174",
// "svelte": "^3.43.0",
// svelte.config.js
      server: {
        hmr: {
          host: 'localhost',
          protocol: 'wss',
          port: 443
        }
      }

Ref.: vitejs/vite#1653 (comment)

Trying on Svelte 3.42.6 SvelteKit v1.0.0-next.178.
Did you put anything for server?

vite: {
	server: {
		host: '0.0.0.0',
		port: 3000,
		https: {
			key: readFileSync('/app.loc.key'),
			cert: readFileSync('/app.loc.crt')
		},
		hmr: {
			host: 'localhost', // or 0.0.0.0
			protocol: 'wss',
			port: 443
		}
	}
}

@taffit
Copy link

taffit commented Oct 1, 2021

@robots4life What I currently have in a working state is the following (your mileage may vary):

  • Packages (only listing the IMO relevant packages for this issue):
    • @sveltejs/kit@next: 1.0.0-next.178
    • vite-plugin-host: 1.0.2
  • Installed via pnpm install.
  • My svelte.config.js looks like the following (removed some lines not relevant for the issue, but leaving mostly intact):
import preprocess from 'svelte-preprocess';
import host from 'vite-plugin-host';
import path from 'path';
import node from '@sveltejs/adapter-node';

/** @type {import('@sveltejs/kit').Config} */
const config = {
	// Consult https://github.com/sveltejs/svelte-preprocess
	// for more information about preprocessors
	preprocess: preprocess(),

	kit: {
		// hydrate the <div id="svelte"> element in src/app.html
		target: '#svelte',
		adapter: node({
			out: 'build',
			precompress: false,
			env: {
				host: 'HOST',
				port: 'PORT'
			}
		}),
		vite: {
			resolve: {
				alias: {
					$components: path.resolve('./src/lib/components'),
				}
			},
			envDir: '../',
			plugins: [host()]
		}
	}
};

export default config;
  • pnpm dev -> Now I can invoke the page with e. g. http://mysite.local.host:3000 or http://anothersubdomain.local.host:3000 (with anothersubdomain.local.host and mysite.local.host pointing to localhost in my hosts-file).

In my case there was no need to add anything other, neither any hmr-properties.

Edit: Updated to latest @svelte/kit@next: 1.0.0-next.178, still works.
Edit 2: You need to add the complete domain name to your DNS/hosts-file, of course.

@robots4life
Copy link

robots4life commented Oct 1, 2021

@taffit On linux, have 127.0.0.0.1 app.loc in hosts file, you are correct http://app.loc:3000 works, I can confirm, however neither https://app.loc or even https://app.loc:3000 work. The vite_ping also fails for https and HMR reloads. Using VS Code remote docker environment.

@moisesbites Nada, even with the host added to the package.json the same result. Can confirm this was working in prior versions of Kit. Is npm run dev -- --host 0.0.0.0 not the same without the changes in package.json?

ref: #1468

@taffit
Copy link

taffit commented Oct 1, 2021

@robots4life https is a different beast. I assume, you set the certificates somewhere? Do you have some proxy that will terminate TLS and forward to your app? Or do you have some traefik-container?
Did you experiment with the kit.vite.server.hmr-properties, as proposed by @moisesbites ?

@robots4life
Copy link

@taffit pinged you on Discord.. yeah I have a CA and auto-gen the certs, do a reverse proxy on port 3000, yeah, done plenty of hmr value experiments.. 😉

@robots4life
Copy link

Using Devilbox - Docker remote environment, it works with this simple config, running npm run dev -- --https --host 0.0.0.0 and going to https://app.loc:3000 instead of just https://app.loc (there I get a 403).
Also for the generated cert from Vite for localhost an exception has to be allowed once.

/** @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.
So a change in the way HRM is done must/could have been introduced in Vite or SvelteKit.

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;

@Cluster2a
Copy link

Got the same error using traefik + https with mkcert.

@gyurielf
Copy link
Contributor

Okay, finally I found the trick.

So I don't even know how it works before version next 169..
But if you use a reverse proxy and/or containers to run SvelteKit
The ports it should be same (in my case at least) with the SvelteKit running port.
In my case in the docker it's run on the port 3000, and map to 3001 host port and i have a reverse proxy in front of it, and runs on port 80 and pass to 3001.

This config solved the problem for me. (http)

        vite: {
            server: {
                hmr: {
                    // Internal port (in container same as sveltekit port).
                    port: 3000,
                    // External port (Docker host)
                    clientPort: 3001
                }
            }
        },

@Cidan
Copy link

Cidan commented Oct 20, 2021

Hi everyone,

Mostly leaving this for future readers, but I believe I have found a fix for people using sveltekit behind a TLS reverse proxy, with no local/native TLS in sveltekit it self.

If you were previously running a config setting protocol, host, etc, you must strip the config down to just this:

		vite: {
			server: {
				hmr: {
					clientPort: 443
				}
			}
		},

This fixed the issue for me, and sveltekit no longer loops infinitely. It seems like you no longer have to specify the host, local port, or protocol.

My setup:

  • sveltekit running in a container on kubernetes
  • TLS traffic handled by a reverse proxy outside of the container
  • TLS traffic forwarded to port 3000 of the sveltekit container port

I hope this helps!

@YugoCode
Copy link

YugoCode commented Nov 8, 2021

The proposed fixes unfortunately still don't work for me 🥺 I'm running Nginx as a reverse proxy on port 443. Sveltekit is running on port 3000. Both are running locally currently. I adjusted the svelte.config.js as @Cidan described, but I still get the endless reloading when I visit https://mydomain.com.

Could someone who has fixed it, please post his Nginx config?

@Kapsonfire-DE
Copy link
Contributor

Could someone who has fixed it, please post his Nginx config?

But i did a long time ago xD

@annmarie-switzer
Copy link

annmarie-switzer commented Nov 9, 2021

@YugoCode

server {
    listen 80;
    gzip on;

    location / {
        root /usr/share/nginx/html;
        index index.html;
        try_files $uri $uri/index.html /200/index.html =404;
    }
}

I'm running nginx and the svelte app in Docker.

svelte config:

import preprocess from 'svelte-preprocess';
import adapter from '@sveltejs/adapter-static';

/** @type {import('@sveltejs/kit').Config} */
const config = {
    preprocess: preprocess(),
    kit: {
        adapter: adapter(),
        ssr: false,
        target: '#svelte',
        vite: {
            server: {
                hmr: {
                    port: 80,
                    clientPort: 443
                }
            }
        }
    }
};

export default config;

Dockerfile for svelte app:

FROM node:16-alpine AS build

RUN mkdir -p /app

WORKDIR /app

COPY . .
RUN npm install

EXPOSE 80/tcp

CMD ["npm", "run", "dev", "--", "--host", "0.0.0.0", "--port", "80"]

Container config (docker-compose):

  ui:
    container_name: ui
    image: ui-${UI_MODE}
    build:
      context: ../ui
      dockerfile: Dockerfile.${UI_MODE}
    restart: always
    volumes:
      - ../ui/src/:/app/src
      - ../ui/static:/app/static
      - ./configs/certs/mycert.pem:/certs/cert.pem
      - ./configs/certs/mycert-key.pem:/certs/key.pem
    tty: true

@robots4life
Copy link

@annmarie-switzer this config then runs on https://localhost/ or https://myapp.loc or https://myapp.loc:3000 ?

@annmarie-switzer
Copy link

https://myapp.tld

@bartosjiri
Copy link

bartosjiri commented Nov 11, 2021

I have been able to get rid of this issue and keep HMR when running through Docker Compose in WSL2 behind Traefik with the following setup:

docker-compose.dev.yml

...
services:
  ...
  client:
    build:
      context: "./client"
      target: dev
    container_name: client
    volumes:
      - "./client/src/:/usr/src/app/src/"
      - "/usr/src/app/node_modules"
    expose:
      - 3000
      - 24678
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.client.entrypoints=web"
      - "traefik.http.routers.client.rule=PathPrefix(`/`)"
      - "traefik.http.services.client.loadbalancer.server.port=3000"
    networks:
      - traefik-proxy

client/Dockerfile

FROM node:14.17.0-alpine AS base

WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . /usr/src/app
EXPOSE 3000
EXPOSE 24678

FROM base AS dev
ENV CHOKIDAR_USEPOLLING=true
CMD ["npm", "run", "dev"]

client/package.json

{
...
  "scripts": {
    ...
    "dev": "svelte-kit dev --host 0.0.0.0",
  },
  "devDependencies": {
    ...
    "@sveltejs/kit": "^1.0.0-next.195",
    "svelte": "^3.44.0",
  }
}

client/svelte.config.js

const config = {
  ...
  kit: {
    ...
    vite: {
      server: {
        watch: {
          usePolling: true
        },
        hmr: {
          protocol: 'ws',
          host: 'localhost',
          port: 80
        }
      }
    }
  }
}

The site is then available on http://localhost.

@YugoCode
Copy link

YugoCode commented Nov 13, 2021

Thank you very much for your help! 😊

I got it working 🥳 I have following setup on my local machine:

  • Nginx as a gateway (Port 443)
  • SvelteKit behind the gateway (Port 3000)
  • An API server behind the gateway (Port 8080)

If you have a similar setup, here is what you need to do:

1. Adjust your nginx.conf


events {}
http {                                                                                                                               
     server {
         listen       443 ssl http2;                                                                                                  
                                                                                                  
         # Better set absolute paths to those two files                                                                                        ssl_certificate      ssl/localhost.crt;                                                                                      
         ssl/localhost.crt
         ssl/localhost.key;                                                                                                                                                                                                                                     ssl_session_cache    shared:SSL:1m;                                                                                          
         ssl_session_timeout  5m;                                                                                                                                                                                                                                           
         ssl_ciphers  HIGH:!aNULL:!MD5;                                                                                                        ssl_prefer_server_ciphers  on;                                                                                               
         
         # Important for HMR over websockets                                                                                          
         proxy_set_header Upgrade $http_upgrade;                                                                                      
         proxy_set_header Connection 'Upgrade';                                                                                       
         proxy_set_header Host $host;                                                                                                 
                                                                                                                                      
         location / {                                                                                                                 
           proxy_pass http://localhost:3000;                                                                                          
         }                                                                                                                            
                                                                                                                                      
         # Set this if you have a (node.js) server running for handling API calls                                                     
         location /api {                                                                                                              
           proxy_pass http://localhost:8080;                                                                                          
         }                                                                                                                            
     }                                                                                                                                
 }

Make sure to adjust the paths to your certificate and private key file. If you want to create a self-signed certificate for local testing, you can use this command:

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout localhost.key -out localhost.crt

Don't forget to reload your config after you have done the changes:

sudo nginx -s reload

2. Adjust your svelte.config.js

    kit: {
        ...
        vite: {
            server: {
                hmr: {
                    port: 443
                }
            }
        },
    }
  1. Enjoy HMR on https://localhost

Just restart SvelteKit and you are good to go:

npm run dev -- --open

From now on you should only use https://localhost, because http://localhost:3000 won't work with HMR.

*Thanks again everyone for your help ❤️

@bradfrosty
Copy link

Chiming in here as I develop with Remote VSCode frequently on a SvelteKit project and I have never run into this issue, including with ports automatically forwarded. While what I'm suggesting is not a solution to the issue itself, it may help people securely (and quickly) develop over SSH with VSCode session depending on your situation.

I see many people suggesting to use a reverse proxy, or to modify their UFW firewalls. This should not be necessary and may expose your personal or development machines unnecessarily. These types are things are easily solved with VPNs.

I highly recommend using something like Tailscale. It is a fantastic piece of software and is free for personal use. It takes no more than 10 minutes to get setup. All you need to do is create an account and install Tailscale on the machines in your network. With that, you now have a fancy new zero trust private network! Confirm by trying to ssh into the other machine on your network. If that works, VSCode should be good to go without any of these issue. HMR should work normally, pages should load quick, and ports should automatically forward.

PS: Here's their repo.

@trasherdk
Copy link

Ahh, finally. The endless reload has taken a break.

pnpm dev -- --open on @sveltejs/kit 1.0.0-next.301 and a svelte.config.js with:

	kit: {
		// hydrate the <div id="svelte"> element in src/app.html
		// target: '#svelte'
		vite: {
			server: {
				hmr: {
					port: 3000,
					clientPort: 3001
				}
			}
		}
	}

local: Windows 8.1 VS Code Remote client,
remote: Slackware 15 (headless), Node 16 (Remote, as in 7000 Km 😄 )

@Rich-Harris
Copy link
Member

Would anyone be up for taking a stab at adding a FAQ entry that explains what the issue is and how to resolve it (ideally in a tool-agnostic way)? https://github.com/sveltejs/kit/tree/master/documentation/faq

@benmccann
Copy link
Member

This was a bug I fixed (at least mostly) in Vite 2.9, so I'm not sure we necessarily need an FAQ as it wasn't really user confusion, but a bug

@benmccann benmccann added bug Something isn't working vite and removed documentation Improvements or additions to documentation labels Apr 8, 2022
@wallynm
Copy link

wallynm commented Mar 23, 2024

Just for future reference, i was getting stuck using vite version 5.0.3, installed latest version which is 5.2.4 and worked fine.
I've got this error initiating a project from scratch using: npx svelte-add@latest, and was getting error from beginning, with this update its working fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working vite
Projects
None yet
Development

No branches or pull requests