pageContext
The pageContext
object provides contextual information about the current page.
The
+data
hook is explained at Guides > Data Fetching.
It includes:
- Built-in properties such as
pageContext.urlParsed
andpageContext.routeParams
. - Custom properties that you can add, for example
pagecontext.user
.
It's accessible to all:
- Vike hooks, such as
+data
. - UI components, by using
usePageContext()
.
You can access pageContext
from the client-side by using passToClient
.
Built-in
Built-in properties:
-
pageContext.Page
: theexport { Page }
orexport default
of the+Page.js
file. -
pageContext.data
: the value returned by thedata()
hook, see also API >useData()
. -
pageContext.routeParams
: the route parameters. (E.g.pageContext.routeParams.movieId
for a page with a Route String/movie/@movieId
.) -
pageContext.urlOriginal
: the current URL.On the server-side,
pageContext.urlOriginal
is the value you passed at the server middleware:On the client-side:
- When using Client Routing, the value of
pageContext.urlOriginal
is the browser's current URL (window.location.href
). - When using Server Routing, the value of
pageContext.urlOriginal
isundefined
(unless you usepassToClient
).
- When using Client Routing, the value of
-
pageContext.urlPathname
: alias forpageContext.urlParsed.pathname
. -
pageContext.urlParsed
: URL information:For example:
https://example.com/some-base-url/hello/s%C3%A9bastien?fruit=%C3%A2pple&fruit=orânge#%C3%A2ge
-
pageContext.headers
: The headers of the HTTP Request. As a string object (Record<string, string>
) normalized by Vike, see HTTP Headers. -
pageContext.headersOriginal
: The headers of the HTTP Request. The original object provided by the server, see HTTP Headers. -
pageContext.config
: See API >meta
. -
pageContext.isHydration
: Whether the page is rendered to HTML. When using Client Routing, the value istrue
for the first page the user navigates to, andfalse
for any subsequent navigation. (When using Server Routing, the value is alwaystrue
.) (If the page doesn't throw an error then it's equivalent to!pageContext.isClientSideNavigation
, otherwise the error page is rendered and thuspageContext.isHydration
isfalse
whereas!pageContext.isClientSideNavigation
istrue
.) -
pageContext.isBackwardNavigation
: Whether the user is navigating back in history. The value istrue
when the user clicks on his browser's backward navigation button, or when invokinghistory.back()
. TheisBackwardNavigation
property only works with Client Routing. (The value is alwaysnull
when using Server Routing.) -
pageContext.is404
: If an error occurs, whether the error is a404 Page Not Found
or a500 Internal Error
, see API > Error Page. -
pageContext.isClientSideNavigation
: Whether the page was navigated by the client-side router. In other words, when using Client Routing, the value isfalse
for the first page the user visits, andtrue
for any subsequent navigation. (When using Server Routing, the value is alwaysfalse
.) -
pageContext.abortReason
: Set bythrow render()
and used by the error page. -
pageContext.abortStatusCode
: Set bythrow render()
and used by the error page. -
pageContext.errorWhileRendering
: The first error (if there is any) that occurred while rendering the page, see Tool Integration > Error Tracking.
Custom
You can define custom pageContext
properties. (See TypeScript for how to define their types.)
-
At
renderPage()
:Setting
pageContext.user
is a common use case for integrating authentication tools, see Tool Integration > Authentication > Integration. -
At any Vike hook.
-
At any UI component, by using
usePageContext()
.
FAQ
Can I mutate pageContext
?
Yes, it's a common practice to change/add pageContext
properties at any time.
Can I use pageContext
to store UI state?
No, see Lifecycle.
Can I check whether SSR is enabled?
On the server-side, you can tell whether SSR is enabled by checking whether pageContext.Page
is set:
TypeScript
To extend and/or refine Vike's types PageContext
/PageContextServer
/PageContextClient
, use the global interface Vike.PageContext
:
If you use Server Routing:
Lifecycle
Server-side
On the server-side, upon an HTTP request, a new pageContext
object is created. It's used for rendering the HTML included in the HTTP response. The pageContext
object is discarded after the HTTP response is sent.
Client-side
On the client-side, upon client-side page navigation, the pageContext
object of the previous page is discarded and a new pageContext
object is created.
Vike adds information to
pageContext
only while rendering the page. We recommend to treatpageContext
as immutable after the page rendering has finished. For example, don't usepageContext
as a UI store (use a proper state management tool instead).
Pre-rendering
If a page is pre-rendered, its initial pageContext
value is determined at build-time and saved at dist/client/${url}/index.pageContext.json
.
Consequently, the initial
pageContext
value of a pre-rendered page is set in stone at build-time.