wails
wails copied to clipboard
[macOS] HMR not working inside Wails frame
Description
I am running into an issue where changes inside the frontend are not being updated in the Wails window. Navigating to both the Vite frontened (5173) and Wails dev server (34115) results in HMR working just fine but not inside the Wails window.
Terminal log shows HMR being triggered but nothing is updated in Wails browser.
7:35:48 AM [vite] hmr update /src/routes/+page.svelte
Opening developer tools, I get the following errors:
WebSocket connection to 'ws://wails.localhost:34117/' failed: A server with the specified hostname could not be found.
Invalid url for WebSocket ws://localhost:undefined/
Unhandled Promise Rejection: SyntaxError: The string did not match the expected pattern.
Note: I changed to 34117 above based on another recommendation. Same behavior as leaving the port as auto and using 34115.
To Reproduce
- wails init -n new-app -t svelte
- cd new-app
- wails dev
- Modify App.svelte
Expected behaviour
HMR would result in the browser inside Wails to be refreshed
Screenshots
No response
Attempted Fixes
Attempted to change ports in wails.json
System Details
# Wails
Version | v2.6.0
# System
┌─────────────────────────┐
| OS | MacOS |
| Version | 14.1 |
| ID | 23B74 |
| Go Version | go1.21.4 |
| Platform | darwin |
| Architecture | arm64 |
└─────────────────────────┘
# Dependencies
┌────────────────────────────────────────────────────────────────┐
| Dependency | Package Name | Status | Version |
| Xcode command line tools | N/A | Installed | 2403 |
| Nodejs | N/A | Installed | 21.2.0 |
| npm | N/A | Installed | 10.2.3 |
| *Xcode | N/A | Available | |
| *upx | N/A | Available | |
| *nsis | N/A | Installed | v3.09 |
└─────────────────── * - Optional Dependency ────────────────────┘
# Diagnosis
Optional package(s) installation details:
- Xcode: Available at https://apps.apple.com/us/app/xcode/id497799835
- upx : Available at https://upx.github.io/
SUCCESS Your system is ready for Wails development!
♥ If Wails is useful to you or your company, please consider sponsoring the project:
https://github.com/sponsors/leaanthony
### Additional context
_No response_
Forgot that I swapped out Svelte for SvelteKit using these directions which is what broke HMR. Trying to figure out why HMR is no longer working with SK.
Ok, so the problem with HMR not working is based on the vite
and @vitejs/plugin-react
version installed. Removing the default frontend
directory and initializing a new vite project, regardless of framework breaks HMR with Wails.
However, removing the latest version of these two dependencies and installing the ones from the original frontend [email protected]
and @vitejs/[email protected]
restores the HMR.
Taking a look through the vite-setup-catalogue and configured Vite to be behind a reverse proxy.
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
server: {
host: true,
port: 5173,
strictPort: true,
},
});
This gets HMR working again but I still get the following error in console:
WebSocket connection to 'ws://wails.localhost:34115/' failed: A server with the specified hostname could not be found.
Maybe later I'll play around with Vite proxy settings to hopefully get rid of that.
Thanks for reporting this. This needs investigating. Something must have changed in the version update.
So it changed with Vite 4. If I used any version prior to that it worked. This was a breaking change for Vite because of breaking changes in Rollup.
Ah right. I guess we can only support one version for the templates. Do you know specifically what the change was? Perhaps we can accommodate these changes better in v3
WebSocket connection to 'ws://wails.localhost:34117/' failed: A server with the specified hostname could not be found.
Seems like newer MacOS releases don't resolve wails.localhost
anymore to 127.0.0.1
. I'm sure I've tested that previously and it worked. So we need to change the hostname to localhost
to resolve this issue.
I'm just noting that I've temporarily worked around this by adding this to my /etc/hosts file:
127.0.0.1 wails.localhost
Obviously this is only going to help during my local development, but it's fine for the time being until a fix arrives (or I can downgrade vite if need be).
p.s. I ❤️ Wails
Fixes?
🟢 cd frontend && pnpm install [email protected]
. Fixes this for me as well. But it's a work-around.
I don't know for sure what I need from the newer Vite 5 yet but we'll see.
🟢
cd frontend && pnpm install [email protected]
. Fixes this for me as well. But it's a work-around.I don't know for sure what I need from the newer Vite 5 yet but we'll see.
Not using vite, just vanilla js
Coming back around to this I did some more tinkering and here is my vite.config.ts
that allows HMR to work and removes the aforementioned error. I've also included the package.json
to show this is running [email protected]
.
Spent some time understanding vite.hmr
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import tsconfigPaths from 'vite-tsconfig-paths';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react(), tsconfigPaths()],
server: {
hmr: {
host: 'localhost',
protocol: 'ws',
},
},
});
{
"name": "frontend",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.2.66",
"@types/react-dom": "^18.2.22",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@typescript-eslint/parser": "^7.2.0",
"@vitejs/plugin-react": "^4.2.1",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.6",
"prettier": "^3.2.5",
"typescript": "^5.2.2",
"vite": "^5.2.0",
"vite-tsconfig-paths": "^4.3.2"
}
}
Spent some time understanding vite.hmr
@j-bullard Godsent solution this one. Thank you! I'll try it out now.
EDIT: ✅ Confirmed, the solution works! Great job, man!
127.0.0.1 wails.localhost
old six!