Route Function
Route Functions provide full programmatic flexibility to define routes.
The parameter
id
is available atpageContext.routeParams.id
.
If you merely want to guard your page, then you can use a Route String with a guard()
hook instead of a Route Function.
You can use any routing tool you want, such as:
- Vike's Route String resolver
resolveRoute()
- partRegex
- path-to-regexp (the router used by Express.js)
- etc.
Precedence
The precedence
number can be used to resolve route conflicts, see API > Routing Precedence.
resolveRoute()
You can use Route Strings inside Route Functions:
TypeScript
Lightweight & fast
Route Functions should be lightweight and fast.
Vike executes all Route Functions every time the user navigates to a new page. This means that a slow Route Function slows down all pages.
Vike always has to run all Route Functions because it cannot magically predict the outcome of Route Functions. Consider the following example:
This authentication routing trick is further explained at Tool Integration > Authentication > Login flow.
Vike cannot know whether another Route Function will return a higher precedence number, therefore Vike has to execute all Route Functions.
If you use Client Routing, then all your Route Functions are loaded in the browser. This means that if a Route Function imports a lot of code, then all that code is loaded on the client-side of every page. A heavy Route Function slows down your whole app.
Route Functions should be lightweight and fast.
Async
Asynchronous Route Functions are forbidden.
The motivation for having an asynchronous Route Function is usually to redirect/protect a private
page. You can achieve that by using throw render()
/ throw redirect()
with an async guard()
, async data()
, or
async onBeforeRender()
hook.
An asynchronous Route Function would slow down your entire app: as explained in Lightweight & fast, every time the user navigates to a new page all your Route Functions are called. This means that a single slow Route Function slows down all your pages.
Redirection
Cannot provide pageContext
Using Route Functions to provide pageContext
values is forbidden.
In principle, Vike could support providing pageContext
values but it deliberately doesn't support it in order to foster lightweight Route Functions.
As explained in Lightweight & fast, you should keep Route Functions simple and you shouldn't implement complex logic in
+route.js
files.
That said, you can work around it by misusing pageContext.routeParams
to provide data.
But it isn't recommended: pageContext.routeParams
is supposed to hold only a minimal amount of information. Instead,
we recommend to implement complex logic in data()
,
onBeforeRender()
, guard()
,
or in a custom hook.