Edit this page

getGlobalContext()

Environment: server, client.

Access global information about your app, see API > globalContext.

For example, on the server-side, it can be used to access the assets manifest:

// Environment: server
 
import { getGlobalContext } from 'vike'
 
const globalContext = await getGlobalContext()
 
// The assets manifest
console.log(globalContext.assetsManifest)
 
// Subset of the Vite configuration passed to the runtime. (By default the
// Vite configuration is available only at build-time.)
console.log(globalContext.viteRuntimeInfo)

getGlobalContext() can be invoked at any time, independent of page rendering. For example, it can be one of the first functions called when your server starts — before it even receives any HTTP requests.

You can also access it over pageContext.globalContext.

getGlobalContextAsync()

import { getGlobalContextAsync } from 'vike'
const globalContext = await getGlobalContextAsync(process.env.NODE_ENV==='production')

Same as getGlobalContext(): for some users getGlobalContext() cannot be called early and the solution is to use getGlobalContextAsync() instead.

We recommend using getGlobalContext() instead: use getGlobalContextAsync() only if getGlobalContext() throws the following error.

# For some users, the following error is thrown when running getGlobalContext()
The global context isn't set yet, use getGlobalContextAsync() instead.

The returned globalContext is the same as the one returned by getGlobalContext().

getGlobalContextSync()

⚠️
Don't use this: it will be deprecated in the next major release. Use getGlobalContext() or pageContext.globalContext instead.

Same as getGlobalContext() but synchronous.

See also