enable-spa-fallback

Enable SPA Fallback

SPA fallback serves your index.html when a request doesn’t match a static file and isn’t under an excluded API prefix. This supports client-side routing in SPAs.

Basic usage

1
2
3
4
5
handler, err := servejs.NewDir(
	"./dist",
	servejs.WithSPAFallback(true), // enable fallback
)
if err != nil { /* handle err */ }

Excluded prefixes

Requests starting with certain prefixes are excluded from fallback and will 404 if there’s no static match. By default these are /api and /doc.

Change or add prefixes:

1
2
3
4
5
handler, err := servejs.NewDir(
	"./dist",
	servejs.WithSPAFallback(true),
	servejs.WithAPIPrefixes("/api", "/auth", "/docs"),
)

When fallback is off

If WithSPAFallback(false) (or defaulted off in future configs), unknown non-asset routes return 404.