devforth / spa-to-http

Lightweight zero-configuration SPA HTTP server. Serves SPA bundle on HTTP port so it plays well with Traefik out of the box. Compatible with Vue.js, React and Angular
MIT License
110 stars 8 forks source link

Support relative path prefixes (base in vite.config.js) #11

Closed tommed closed 1 year ago

tommed commented 1 year ago

Hi there, my vite.config.js file contains a base URL:

export default defineConfig({
  base: '/london/fileshare',
  //...
})

This is because I'm using a load balancer (and path prefix) for this service.

However, this does not appear to work with this docker image, as all static files no longer map correctly to the files. The paths map correctly, but literally all asset requests return with the index.html as the response (e.g., https://MY_DOMAIN/london/fileshare/assets/index-c9fdd385.css returns with the contents of index.html).

This is because the relative path you're receiving contains the path prefix, and you're probably scanning for a local directory called london/fileshare/assets rather than assets. How can I support scenario please?

If I remove the base value from vite, the assets are in the index.html as /assets which obviously wrong as it's referring to the root directory which is outside of this app. I need a way of feeding into your web server the relative path of this service.

FYI, when I use gin as my web server for my own go projects, I simply set-up my routing based on the relative path like so:

// build web server
r := gin.Default()

var base gin.IRouter = r
if globals.RelUrl = os.Getenv("REL_URL"); globals.RelUrl != "" {
    globals.RelUrl = "/" + strings.TrimLeft(globals.RelUrl, "/")
    base = base.Group(globals.RelUrl)
    log.Printf("relative url: %s", globals.RelUrl)
}

Could you not do something similar? Apologies if I've missed some documentation somewhere.

tommed commented 1 year ago

Doesn't matter. I've managed to replace this program with a few lines of express code. It has also allowed me to set CSP, Permissions Policy etc.

Here's the implementation if anyone needs is: https://gist.github.com/tommed/a9a07d4c045d1e10614a86f25cbbfda7