Files
wotra/ui.go
T
aksdb 73359d3f39 ♻️ Answer unknown API routes with JSON and fix stale comments
The SPA catch-all answered unknown /api paths with the index page and
status 200, so the client SDK failed to parse the body. A catch-all per
HTTP method answers them with a JSON 404 in the error shape of
PocketBase; the routes registered before keep precedence. The comment
on DataFields no longer claims the fields of the read-only collections
were client writable.
2026-12-01 16:59:19 +01:00

48 lines
1.4 KiB
Go

package main
import (
"embed"
"io/fs"
"mime"
"net/http"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
)
//go:embed all:web/build
var webBuild embed.FS
// apiMethods are the methods the catch-all for unknown API routes covers. HEAD is
// left out because it would conflict with every GET route of the router; GET
// patterns answer HEAD requests anyway.
var apiMethods = []string{
http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete, http.MethodOptions,
}
func registerUI(app core.App) {
_ = mime.AddExtensionType(".webmanifest", "application/manifest+json")
sub, err := fs.Sub(webBuild, "web/build")
if err != nil {
panic(err)
}
notFound := func(e *core.RequestEvent) error {
return e.NotFoundError("Not found.", nil)
}
app.OnServe().BindFunc(func(se *core.ServeEvent) error {
// Without this the SPA catch-all below answers unknown API routes with
// the index page and status 200, which the client SDK cannot parse.
// The routes registered before keep precedence because they are more
// specific than this catch-all.
for _, method := range apiMethods {
if !se.Router.HasRoute(method, "/api/{path...}") {
se.Router.Route(method, "/api/{path...}", notFound)
}
}
if !se.Router.HasRoute("GET", "/{path...}") {
se.Router.GET("/{path...}", apis.Static(sub, true))
}
return se.Next()
})
}