Routing
Routes map URL paths to the component that renders them, and navigation switches between them without reloading the page.
This page covers the
app
package's router: registering routes, moving between them from code and from links, reading and changing the current page, and hosting the app under a path prefix such as a GitHub Pages project site.
Components themselves, and the
OnNav
hook among their lifecycle events, are on the
Components
page.
app.Shell
, which switches screens by route inside one root component, is on the
Pages, layouts and state
page.
ยง1 Registering routes
func
Route
(
path
string
,
newComponent
func
(
)
Composer
)
Route
maps a path to a function that returns a new component. Register every route before
app.RunWhenOnBrowser
, in the
main
shared by the server and the browser: the server needs the routes to prerender a page, the browser to mount it.
package
main
import
(
"net/http"
"github.com/Toms223/WebGo/app"
)
type
home
struct
{
app
.
Compo
}
func
(
h
*
home
)
Render
(
)
app
.
UI
{
return
app
.
Div
(
)
.
Body
(
app
.
H1
(
)
.
Text
(
"Home"
)
,
app
.
A
(
)
.
Href
(
"/about"
)
.
Text
(
"About"
)
,
)
}
type
about
struct
{
app
.
Compo
}
func
(
a
*
about
)
Render
(
)
app
.
UI
{
return
app
.
H1
(
)
.
Text
(
"About"
)
}
func
main
(
)
{
app
.
Route
(
"/"
,
func
(
)
app
.
Composer
{
return
&
home
{
}
}
)
app
.
Route
(
"/about"
,
func
(
)
app
.
Composer
{
return
&
about
{
}
}
)
app
.
RunWhenOnBrowser
(
)
http
.
Handle
(
"/"
,
&
app
.
Handler
{
Name
:
"Routes"
}
)
http
.
ListenAndServe
(
":8080"
,
nil
)
}
-
The path is matched exactly against the URL path, without query or fragment. In the browser, navigation drops a trailing slash first, so
/about/matches/about. - Registering the same path again replaces the earlier function.
- Exact routes are tried first, then regular-expression routes in the order they were registered.
What the function is for
The function is called on every navigation to a new path, and its component is compared with the root that is mounted, by the rules in
Writing a component
. If the types differ, the old root is dismounted and the new one mounted. If they are the same type, the mounted root stays: differing exported fields are copied from the fresh value, unexported state is kept, and
OnMount
does not run again. For
app.Route
, return a fresh value every time, never a shared one.
app.Serve
is the exception: it registers every screen's route to one
app.Shell
that is built once in the browser and kept. On each navigation the engine selects the screen for the new path before it loads the shell as the root, and
OnNav
runs after that.
Unmatched paths
A path with no route shows
app.NotFound
, a built-in "Not Found" component by default. On the server,
app.Handler
prerenders the page with it and answers
404
; in the browser, it is mounted like any root.
type
missing
struct
{
app
.
Compo
}
func
(
m
*
missing
)
Render
(
)
app
.
UI
{
return
app
.
Main
(
)
.
Body
(
app
.
H1
(
)
.
Text
(
"Page not found"
)
)
}
func
main
(
)
{
app
.
NotFound
=
&
missing
{
}
app
.
Route
(
"/"
,
app
.
NewZeroComponentFactory
(
&
home
{
}
)
)
app
.
RunWhenOnBrowser
(
)
}
Assign a pointer to your own component type before
RunWhenOnBrowser
. Each miss mounts a new zero value of that type, so fields set on the assigned value are not kept.