Giter VIP home page Giter VIP logo

Comments (25)

stffabi avatar stffabi commented on June 12, 2024 4

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,
	},

from wails.

stffabi avatar stffabi commented on June 12, 2024 2

Just pushed the above proposal to v3-alpha branch.

from wails.

stffabi avatar stffabi commented on June 12, 2024 1

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.

from wails.

leaanthony avatar leaanthony commented on June 12, 2024 1

@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.

from wails.

BenoitBotton avatar BenoitBotton commented on June 12, 2024
  • reverting to vite v4.5.2 and vite/plugin-vue v4.6.2 solved the issue

I tried the following while keeping vite 5.0.0+:

I am puzzled as to why the above config changes do not work with vite 5.0.0+

from wails.

leaanthony avatar leaanthony commented on June 12, 2024

I believe the answer is somewhere in here: vitejs/vite#8452

from wails.

leaanthony avatar leaanthony commented on June 12, 2024

Setting appType: 'custom' should work

from wails.

BenoitBotton avatar BenoitBotton commented on June 12, 2024

thanks @leaanthony
indeed, it does, to some extent...
with vite.config.ts:

export default defineConfig({
  plugins: [vue()],
  appType: 'custom',
})

I get the expected behaviour in the console:

let response = await fetch('go.mod')
undefined
await response.text()
'module changeme\n\ngo 1.18\n\nrequire github.com/wailsapp/wails/v2 v2.7.1\n\nrequire (\n\tgithub.com/bep/debounce v1.2.1 // indirect\n\tgithub.com/go-ole/go-ole v1.2.6 // indirect\n\tgithub.com/godbus/dbus/v5 v5.1.0 // indirect\n\tgithub.com/google/uuid v1.3.0 // indirect\n\tgithub.com/jchv/go-winloader v0.0.0-20210711035445-715c2860da7e // indirect\n\tgithub.com/labstack/echo/v4 v4.10.2 // indirect\n\tgithub.com/labstack/gommon v0.4.0 // indirect\n\tgithub.com/leaanthony/go-ansi-parser v1.6.0 // indirect\n\tgithub.com/leaanthony/gosod v1.0.3 // indirect\n\tgithub.com/leaanthony/slicer v1.6.0 // indirect\n\tgithub.com/leaanthony/u v1.1.0 // indirect\n\tgithub.com/mattn/go-colorable v0.1.13 // indirect\n\tgithub.com/mattn/go-isatty v0.0.19 // indirect\n\tgithub.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect\n\tgithub.com/pkg/errors v0.9.1 // indirect\n\tgithub.com/rivo/uniseg v0.4.4 // indirect\n\tgithub.com/samber/lo v1.38.1 // indirect\n\tgithub.com/tkrajina/go-reflector v0.5.6 // indirect\n\tgithub.com/valyala/bytebufferpool v1.0.0 // indirect\n\tgithub.com/valyala/fasttemplate v1.2.2 // indirect\n\tgithub.com/wailsapp/go-webview2 v1.0.10 // indirect\n\tgithub.com/wailsapp/mimetype v1.4.1 // indirect\n\tgolang.org/x/crypto v0.14.0 // indirect\n\tgolang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect\n\tgolang.org/x/net v0.17.0 // indirect\n\tgolang.org/x/sys v0.13.0 // indirect\n\tgolang.org/x/text v0.13.0 // indirect\n)\n\n// replace github.com/wailsapp/wails/v2 v2.7.1 => C:\\Users\\Benoit\\go\\pkg\\mod\n'
let response = await fetch('I dont exist')
VM43:1 
        
        
        GET http://wails.localhost:34115/I%20dont%20exist 400 (Bad Request)
(anonymous) @ VM43:1
undefined
await response.text()
'Could not load file I dont exist'

But the app (working from the template) looks like:
image

with the console reporting errors:

:34115/favicon.ico:1 
        
        
        Failed to load resource: the server responded with a status of 400 (Bad Request)
wails.localhost/:1 
        
        
        Failed to load resource: the server responded with a status of 400 (Bad Request)

so the app will probably need some changes as well. I will investigate later, other work issues to deal with first

from wails.

BenoitBotton avatar BenoitBotton commented on June 12, 2024

that was when using wails dev
if building the app with wails build -devtools, everything is fine:
image

from wails.

BenoitBotton avatar BenoitBotton commented on June 12, 2024

with appType: 'cutom' set, I notice some errors in the terminal log:

DEB | [DevWebServer] Serving DevServer at http://localhost:34115
Watching (sub)/directory: D:\source\assethandler
DEB | [AssetHandler] Handling request '/' (file='.')
DEB | [AssetHandler] File '.' not found, serving '/' by AssetHandler
DEB | [ExternalAssetHandler] Loading 'http://localhost:5173/'
DEB | [ExternalAssetHandler] 'http://localhost:5173/' returned not found, using AssetHandler
Requesting file: 
DEB | [AssetHandler] Handling request '/favicon.ico' (file='favicon.ico')
DEB | [AssetHandler] File 'favicon.ico' not found, serving '/favicon.ico' by AssetHandler
DEB | [ExternalAssetHandler] Loading 'http://localhost:5173/favicon.ico'
DEB | [ExternalAssetHandler] 'http://localhost:5173/favicon.ico' returned not found, using AssetHandler
Requesting file: favicon.ico
DEB | [AssetHandler] Handling request '/' (file='.')
DEB | [AssetHandler] File '.' not found, serving '/' by AssetHandler
DEB | [ExternalAssetHandler] Loading 'http://localhost:5173/'
DEB | [ExternalAssetHandler] 'http://localhost:5173/' returned not found, using AssetHandler

so is the issue shifted away from vite, to the ExternalAssetHandler?
I admit I am out of my depth as I don't know all that much about serving files.

from wails.

leaanthony avatar leaanthony commented on June 12, 2024

Where is the request for "." coming from?

from wails.

BenoitBotton avatar BenoitBotton commented on June 12, 2024

from wails.

stffabi avatar stffabi commented on June 12, 2024

so is the issue shifted away from vite, to the ExternalAssetHandler?

No that is perfectly fine. Because now Vite returns a 404 error, the request is forwarded to the assethandler. The problem is that now Vite does not work anymore for SPA or MPA, due to using appType: 'custom'. So now with custom one needs to add all the Vite middlewares to handle everything on it's own, therefore it newer serves the index.html anymore. That's why you see the 'could not find file' errors. You can check the Vite configuration by directly navigating to http://localhost:5173/ which is Vite it self. If you don't see your frontend there, then Vite is not configured correctly for SPA.

from wails.

leaanthony avatar leaanthony commented on June 12, 2024

Does bun have a Dev server/bundler? 😉

from wails.

leaanthony avatar leaanthony commented on June 12, 2024

Spitballing here.... What if you registered a path prefix with your middleware so a matching request isn't forwarded to the external server at all?

from wails.

BenoitBotton avatar BenoitBotton commented on June 12, 2024

so far I have tried a few things(separately):

none worked.
I've spent some time trying to setup Vite middlewares, but I am really out of my depth. I will not be quick solving this

from wails.

leaanthony avatar leaanthony commented on June 12, 2024

@stffabi - what if we tried the custom handlers first and forwarded to vite only if that failed?

from wails.

stffabi avatar stffabi commented on June 12, 2024

@stffabi - what if we tried the custom handlers first and forwarded to vite only if that failed?

That would unfortunately change the current behaviour completely. Currently the AssetHandler is a fallback for everything not found. If now people just use that handler to serve content for all paths, that would mean nothing would be served from vite anymore.

I will try to take an in-depth look this week, to see what we could do.

from wails.

leaanthony avatar leaanthony commented on June 12, 2024

Understood that it would change the current behaviour and we don't need to address this until v3 as vite 4 still works fine. If we needed to change behaviour, then we could have that as an option and leave the default. I was just speculating that a workflow of request->asset handler->external server might be an option rather than relying on external server 404s.

from wails.

stffabi avatar stffabi commented on June 12, 2024

I personally would leave the behaviour as it is for v2 and for v3 I would completely drop the AssetHandler and just leave the AssetMiddleware option. Since that is handled in the request chain before going to the assets fs.FS or going to the external dev server.
Only having the AssetMiddleware is also what people are used to when coming from a Web-Framework like standard go http package or gin/echo and so on.
AssetHandler was the first step in v2 at a time when we didn't have an almost full blown http.Server mock.

from wails.

leaanthony avatar leaanthony commented on June 12, 2024

Love it! Sounds like a plan!

from wails.

BenoitBotton avatar BenoitBotton commented on June 12, 2024

from wails.

stffabi avatar stffabi commented on June 12, 2024

Love it! Sounds like a plan!

After taking a look into v3, I think we should keep the AssetHandler but change the logic to use it just for non GET-requests.

  • GET requests are always served from the fs.FS or the external DevServer.
  • AssetHandler is used for all non GET requests
  • If the fs.FS is nil and no external DevServer is used, all requests are being handled by the AssetHandler

This allows people that don't want to use the fs.FS to serve their content completely from an http.Handler. This is interesting for people that use templ.

@leaanthony thoughts?

from wails.

leaanthony avatar leaanthony commented on June 12, 2024

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?

from wails.

stffabi avatar stffabi commented on June 12, 2024

We can see this issue as closed. There's currently nothing we can do regarding Vite v5+ and for Wails v3 we have changed the implementation so this is not a problem anymore.

from wails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.