onBeforeRender()
hook
Environment: server (configurable).
The
onBeforeRender()
hook is for expert Vike users. If you're new to Vike, we recommend using thedata()
hook instead, and/or Vike extensions for automatically integrating data fetching tools (RPC with Telefunc/tRPC, REST with Tanstack Query, GraphQL with Apollo/Relay, etc).
The most notable use case for the onBeforeRender()
hook is deep integration with data fetching tools.
For simple data fetching needs, use the data()
hook instead. As a strategy to decide which one to use, always first try to use data()
and only use onBeforeRender()
as a fallback if data()
doesn't work out.
The onBeforeRender()
hook can be used to orchestrate multiple custom hooks and settings, see:
Another common use case is to use onBeforeRender()
as a global initializing hook. (As a temporary workaround until #962 - New hook onBoot()
is implemented.)
onBeforeRender()
VS data()
The central difference between the two hooks is that the value returned by data()
always sets the value of pageContext.data
, whereas onBeforeRender()
can set multiple pageContext
values.
Another difference is that the entire pageContext.data
value is always sent to the client-side, whereas with onBeforeRender()
you can (and have to) decide which values are sent to the client-side by using passToClient
.
In a nutshell: while onBeforeRender()
requires more manual work, it also gives you more control.
Environment
Like data()
, the onBeforeRender()
hook always runs on the server-side by default. By using .shared.js
or .client.js
you can tell Vike to run onBeforeRender()
on the client-side instead, see API > data()
hook > Environment.
onBeforeRender()
+ meta
Using onBeforeRender()
together with custom hooks and settings (see meta
) is a powerful technique enabling you to implement your own tailored DX.
For example.
A custom setting +sql.js
(created with meta
):
A single global onBeforeRender()
hook orchestrating the customm setting:
See full implementation at API > meta
> Example: sql
.
Override
There can be only one onBeforeRender()
hook per page. For example, if you define a global onBeforeRender()
hook at /pages/+onBeforeRender.js
as well as a page-specific one at /pages/star-wars/+onBeforeRender.js
then the page-specific one overrides the global one. See API > Config Files > Inheritance.
If you want multiple
onBeforeRender()
hooks, then consider:
- Creating custom hooks instead: you can then use one global
onBeforeRender()
hook that orchestrates many custom hooks.- Using
data()
hooks: you can then use one globalonBeforeRender()
while using page-specificdata()
hooks.
You can also suppress globally defined onBeforeRender()
hooks on a per-page basis:
Advanced example
The following is an advanced example of using onBeforeRender()
with meta
in order to integrate data fetching tools. In particular, this approach can be used for advanced integration with GraphQL tools.
If you use a custom renderer instead of
vike-react
/vike-vue
/vike-solid
, then you can modify youronRenderHtml()
/onRenderClient()
hooks instead of doing the following.
See:
TypeScript
Don't omit
ReturnType<OnBeforeRenderAsync>
otherwise TypeScript won't strictly check the return type.
See API >
pageContext
> Typescript for more information on how to extendpageContext
with your own extra properties.