wails: Dynamic assets not loading in dev mode since updating vite to v5.0.0+

Description

since updating to vite 5.0.0+, dynamic assets are not served anymore when running wails dev, but the index.html file is instead. things are fine once compiled

To Reproduce

in a brand new project, follow the step in the dynamic assets example whether fetching a non-exiting file or go.mod, the index.html file gets served image

I have a repo with an example and troubleshooting steps: https://github.com/BenoitBotton/assethandler

Expected behaviour

expect files to get served or an error produced as in the example

Screenshots

No response

Attempted Fixes

No response

System Details

# Wails
Version | v2.7.1

# System
┌─────────────────────────────────────────────────────────────────────────────────────────────────┐
| OS           | Windows 10 Pro                                                                   |
| Version      | 2009 (Build: 22631)                                                              |
| ID           | 23H2                                                                             |
| Go Version   | go1.20.1                                                                         |
| Platform     | windows                                                                          |
| Architecture | amd64                                                                            |
| CPU          | AMD Ryzen 9 5900HX with Radeon Graphics                                          |
| GPU 1        | AMD Radeon(TM) Graphics (Advanced Micro Devices, Inc.) - Driver: 31.0.21023.2010 |
| GPU 2        | NVIDIA GeForce RTX 3070 Laptop GPU (NVIDIA) - Driver: 31.0.15.4633               |
| Memory       | 64GB                                                                             |
└─────────────────────────────────────────────────────────────────────────────────────────────────┘

# Dependencies
┌───────────────────────────────────────────────────────┐
| Dependency | Package Name | Status    | Version       |
| WebView2   | N/A          | Installed | 121.0.2277.98 |
| Nodejs     | N/A          | Installed | 20.10.0       |
| npm        | N/A          | Installed | 9.8.1         |
| *upx       | N/A          | Available |               |
| *nsis      | N/A          | Installed | v3.08         |
└─────────────── * - Optional Dependency ───────────────┘

# Diagnosis
Optional package(s) installation details:
  - upx : Available at https://upx.github.io/

 SUCCESS  Your system is ready for Wails development!

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 26 (17 by maintainers)

Commits related to this issue

Most upvoted comments

Does that mean if you want to serve up, say, an image, you have to use POST? Could we not check the path and if it starts with /wails/assethandler/ we direct it there instead?

One can do that with the Middleware if someone wants that. I personally would not introduce another preregistered path for this, because that can be achieved with just a custom middleware. We would just introduce another “magic” thing that is specific to wails.

We can still drop AssetHandler if you want, but then people that want to use templ must add a middleware and drop the next handler. This assumes the middleware is behind all the internal wails middlewares, like the runtime, but we already discussed to lift the middleware up, so it is being called before any wails middlewares and people can add CORS handling.

I’m open to whatever you want. I personally would not introduce some magic paths like /wails/assethandler/ for assethandler, because IMHO that’s the use case for a middleware and people are used to this from web frameworks. I think we should strive for a clean solution with clean rules and try to be more like web frameworks.

My preferred solution would be probably like this: Let’s just drop the fs.FS completely from the application.AssetOptions and leave http.Handler and middleware. And middleware injects it at the beginning of all requests, even before Wails internal middlewares, so people can add CORS handling or add logging to Wails runtime requests and so on.

Using an fs.FS would then look like this and if someone uses the external devserver, the application.AssetFileServerFS would use the external server instead of using the assets.

app := application.New(application.Options{
	Assets: application.AssetOptions{
		Handler: application.AssetFileServerFS(assets),
	},

And if someone wants to have a custom path for images that get dynamically served, this would look like the following with e.g. chi:

r := chi.NewRouter()
r.Get("/images/*", func(w http.ResponseWriter, r *http.Request) {
	fmt.Println("My Images dynamically served")
	w.WriteHeader(http.StatusOK)
})
r.NotFound(application.AssetFileServerFS(assets).ServeHTTP)

app := application.New(application.Options{
	Assets: application.AssetOptions{
		Handler: r,
	},

Example with http.ServeMux in Go 1.22

mux := &http.ServeMux{}
mux.Handle("/", application.AssetFileServerFS(assets))
mux.HandleFunc("/images/", func(w http.ResponseWriter, r *http.Request) {
	fmt.Println("My Images dynamically served")
	w.WriteHeader(http.StatusOK)
})

app := application.New(application.Options{
	Assets: application.AssetOptions{
		Handler: mux,
	},

Just pushed the above proposal to v3-alpha branch.

hello i know that there is no solution until v3 comes out, but there is three ways to resolve this (this is for the new people who see the issue)

  • the current solution: stay on the old ver v4 [but now they reached v6.5 which alot of things may not work like tailwind with vite extention, react with swc etc]
  • best solution: wails team bring a partial feature from v3 for the handler
  • janky solution if you really need vite ASAP and can’t wait (the solution im using right now): eat the performance hit and base64 the data [if it’s a large file good luck]
func (a *App) GetFile(path string) (string, error) {
	data, err := os.ReadFile(path)
	if err != nil {
		slog.Error("GetFile", "err", err)
		return "", err
	}
	return base64.StdEncoding.EncodeToString(data), nil
}

@BenoitBotton I guess the answer for this ticket is that unless you can find a way to make vite 5 work with its new behaviour and defaults, we recommend staying with Vite 4.

Where is the request for “.” coming from?

That’s when requesting the ‘/’ and indicates which file would have been loaded from the assets fs.FS, so in this case the current directory, which is .. Because that is a directory, it will inernally load the index.html from the fs.FS.