clientRouting
Environment: client.
If you don't use a UI framework Vike extension vike-react
/vike-vue
/vike-solid
then Vike does Server Routing by default.
You can opt into Client Routing by using the clientRouting
setting, see Getting started.
Instead of manually integrating Client Routing yourself, you can use a UI framework Vike extension
vike-react
/vike-vue
/vike-solid
which already integrates Client Routing. And you can use Bati to scaffold an app that usesvike-react
/vike-vue
/vike-solid
.
Examples
React example:
Vue example:
- /boilerplates/boilerplate-vue-ts/renderer/+onRenderClient.ts
- /boilerplates/boilerplate-vue-ts/renderer/app.ts
Getting started
-
Set
clientRouting
totrue
: -
Adapt your
onRenderClient()
hook:Note that
pageContext
is completely discarded and created anew upon page navigation. That's why it's called pageContext (and not appContext).See Render Modes for another illustration of conditional DOM hydration.
Links intercepting
You can keep using <a href="/some-url">
links: the Client Router automatically intercepts clicks on <a>
elements.
You can skip the Client Router for individual <a>
links by adding the rel="external"
attribute, e.g. <a rel="external" href="/some/url">The Client Router won't intercept me</a>
.
Hooks
You can use the following hook to implement initialization after the hydration has finished:
And following hooks to implement page transition animations:
These hooks are only available if you use Client Routing.
Settings
Page settings:
prefetchStaticAssets
: Link prefetching settings, see API >prefetchStaticAssets
.hydrationCanBeAborted
: Whether the UI framework allows the hydration to be aborted, seehydrationCanBeAborted
.
Anchor link options:
<a href="/some/url" keep-scroll-position>
: Don't scroll to the top of the page, preserve the scroll position instead. See also:<a href="/some/url" rel="external">
: Skip Client Routing, see Links intercepting.
Programmatic navigation
You can use the function navigate('/some/url')
to programmatically navigate the user to a new page.
See API > navigate()
.
State initialization
Usually, when using tools such as Apollo GraphQL, Redux or Vuex, you determine the initial state of your UI on the server-side while rendering HTML, and then initialize the client-side with that initial state.
Depending on the tool, you do either one of the following:
- You initialize the state once.
- You re-initialize the state on every page navigation.
To initialize once:
To initialize on every page navigation: