2018-07-11 10:27:30 +00:00
|
|
|
# Frontend browser integration
|
|
|
|
|
|
2018-08-23 07:41:13 +00:00
|
|
|
Reitit includes two browser history integrations.
|
|
|
|
|
|
2023-03-24 09:32:22 +00:00
|
|
|
Main functions are `navigate` and `set-query`. Navigate is used to navigate
|
|
|
|
|
to named routes, and the options parameter can be used to control all
|
|
|
|
|
parameters and if `pushState` or `replaceState` should be used to control
|
|
|
|
|
browser history stack. The `set-query` function can be used to change
|
|
|
|
|
or modify query parameters for the current route, it takes either map of
|
|
|
|
|
new query params or function from old params to the new params.
|
|
|
|
|
|
|
|
|
|
There are also secondary functions following HTML5 History API:
|
|
|
|
|
`push-state` to navigate to new route adding entry to the history and
|
|
|
|
|
`replace-state` to change route without leaving previous entry in browser history.
|
2018-08-23 07:41:13 +00:00
|
|
|
|
2025-01-28 13:46:37 +00:00
|
|
|
See [coercion notes](./coercion.md) to see how frontend route parameters
|
|
|
|
|
can be decoded and encoded.
|
|
|
|
|
|
2018-08-23 07:41:13 +00:00
|
|
|
## Fragment router
|
|
|
|
|
|
|
|
|
|
Fragment is simple integration which stores the current route in URL fragment,
|
|
|
|
|
i.e. after `#`. This means the route is never part of the request URI and
|
|
|
|
|
server will always know which file to return (`index.html`).
|
|
|
|
|
|
|
|
|
|
## HTML5 router
|
|
|
|
|
|
|
|
|
|
HTML5 History API can be used to modify the URL in browser without making
|
|
|
|
|
request to the server. This means the URL will look normal, but the downside is
|
|
|
|
|
that the server must respond to all routes with correct file (`index.html`).
|
|
|
|
|
Check examples for simple Ring handler example.
|
|
|
|
|
|
2019-04-15 08:40:59 +00:00
|
|
|
### Anchor click handling
|
|
|
|
|
|
|
|
|
|
HTML5 History router will handle click events on anchors where the href
|
|
|
|
|
matches the route tree (and other [rules](../../modules/reitit-frontend/src/reitit/frontend/history.cljs#L84-L98)).
|
|
|
|
|
If you have need to control this logic, for example to handle some
|
|
|
|
|
anchor clicks where the href matches route tree normally (i.e. browser load)
|
2019-05-02 08:52:13 +00:00
|
|
|
you can provide `:ignore-anchor-click?` function to add your own logic to
|
2019-04-15 08:40:59 +00:00
|
|
|
event handling:
|
|
|
|
|
|
|
|
|
|
```clj
|
|
|
|
|
(rfe/start!
|
|
|
|
|
router
|
2020-08-17 02:54:20 +00:00
|
|
|
on-navigate-fn
|
2019-04-15 08:40:59 +00:00
|
|
|
{:use-fragment false
|
2019-04-26 12:10:17 +00:00
|
|
|
:ignore-anchor-click? (fn [router e el uri]
|
|
|
|
|
;; Add additional check on top of the default checks
|
|
|
|
|
(and (rfh/ignore-anchor-click? router e el uri)
|
|
|
|
|
(not= "false" (gobj/get (.-dataset el) "reititHandleClick"))))})
|
2019-04-15 08:40:59 +00:00
|
|
|
|
|
|
|
|
;; Use data-reitit-handle-click to disable Reitit anchor handling
|
|
|
|
|
[:a
|
|
|
|
|
{:href (rfe/href ::about)
|
|
|
|
|
:data-reitit-handle-click false}
|
|
|
|
|
"About"]
|
|
|
|
|
```
|
|
|
|
|
|
2018-08-23 07:41:13 +00:00
|
|
|
## Easy
|
|
|
|
|
|
|
|
|
|
Reitit frontend routers require storing the state somewhere and passing it to
|
2018-12-04 08:51:17 +00:00
|
|
|
all the calls. Wrapper `reitit.frontend.easy` is provided which manages
|
|
|
|
|
a router instance and passes the instance to all calls. This should
|
|
|
|
|
allow easy use in most applications, as browser anyway can only have single
|
|
|
|
|
event handler for page change events.
|
2019-03-22 06:42:28 +00:00
|
|
|
|
|
|
|
|
## History manipulation
|
|
|
|
|
|
2025-01-28 13:46:37 +00:00
|
|
|
Reitit doesn't include functions to manipulate the history stack, i.e.,
|
2019-03-22 06:42:28 +00:00
|
|
|
go back or forwards, but calling History API functions directly should work:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
(.go js/window.history -1)
|
|
|
|
|
;; or
|
|
|
|
|
(.back js/window.history)
|
|
|
|
|
```
|