Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1541

How to make sveltekit SPA app manage routes with golang backend when entering urls in browser

$
0
0

With having backend built in golang, frontend built in sveltekit as an SPA, how to make sveltekit frontend manage routes make in sveltekit, while having golang handle routes for api?

For example: i would expect that:

  • localhost:3000/api/whatever is handled by golang
  • localhost:3000/project or localhost:3000/app/project is handled by sveltekit

To further explain, i have several routes in sveltekit (e.g. /home, /project, /project/1, ...) and several in golang (/api/project, /api/project/1, ...)

As mentioned above, when accessing localhost:3000/home or localhost:3000/project/1 i expect that sveltekit would properly show +page.svelte for that page.

Unfortunatelly, this doesnt work.

I have tried to use the following (having defaults for frontend being built by vite)

func (app *SharedState) ServeIndex(w http.ResponseWriter, r *http.Request) {    http.ServeFileFS(w, r, app.FrontendApp, "frontend/build/index.html")}func (app *SharedState) staticFileHandler(content embed.FS) http.Handler {    html, _ := fs.Sub(content, "frontend/build/_app")    return http.FileServerFS(html)}func (app *SharedState) Routes() *http.ServeMux {    mux := http.NewServeMux()    //    // redirect to spa    //    mux.HandleFunc("GET /{$}", app.RedirectToApp)    //    // spa / sveltekit app via embedfs    //    mux.Handle("GET /app/_app/", http.StripPrefix("/app/_app/", app.staticFileHandler(app.FrontendApp)))    mux.HandleFunc("GET /app", app.ServeIndex)    mux.HandleFunc("GET /app/", app.ServeIndex)    ...}

I i would simply use a FileServerFS on the /, this works when opening localhost:3000, then navigating with clicking to localhost:3000/project/1, but not when this is pasted in browser and just going to that url.

To simplify, how to make golang backend with svelte spa frontend work with shared links?

PS. svelte has a config that i enabled for /app to work

paths: {            base: "/app",        },

Viewing all articles
Browse latest Browse all 1541

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>