maxence-charriere / go-app

A package to build progressive web apps with Go programming language and WebAssembly.

Home Page:https://go-app.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Custom error page

muffins261 opened this issue · comments

I wanted to create a custom 404 page, but I didn't find this option in the documentation. Is it possible to do this? If so, please give me an example.

It depends on what you mean with an 404 page:

As you define the routes in the backend and any route that is not picked up will throw a normal 404 error as with any other Go standard HTTP Server you could handle that from the backend, sending some special standard HTML page (instead of loading the PWA). See for example: https://stackoverflow.com/questions/9996767/showing-custom-404-error-page-with-standard-http-package.

One thing you easily could do, when using the "normal" way how Go-App, defines the routes: Set up all the real routes and as the last one you set up a wildcard route and make that load a "Page not found" component.

If you only have a wildcard route to the front-end and use some root component for all the logic, you need to deal with navigation there, then you need to evaluate your path and create something that you want to be the error 404 page.

@oderwat has excellent points on handling 404s and routing them correctly in go-app.

On a side note, looking at how the code handles routing, I came across the following:

go-app/pkg/app/app.go

Lines 353 to 356 in ae6abf8

compo, ok := routes.createComponent(path)
if !ok {
compo = &notFound{}
}

A possible enhancement would instead call an app.NotFoundComposer function:

compo, ok := routes.createComponent(path)
if !ok {
	compo = NotFoundComposer()
}

And changing:

var (
// NotFound is the ui element that is displayed when a request is not
// routed.
NotFound UI = &notFound{}
)

To a function:

var (
	// NotFoundComposer returns a default not found component.
	NotFoundComposer = func() Composer { return &notFound{} }
)

Then users could provide a default component when routing to an unknown route.

// while setting up app.Route() calls
app.NotFoundComposer = func() app.Composer { return &notFoundCustom{} }