Edit this page

getGlobalContext()

Environment: server.

Access global information about your app, such as the assets manifest.

import { getGlobalContext } from 'vike/server'
 
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)

It only provides information loaded at runtime. To get information loaded at build-time, see getVikeConfig().

The getGlobalContext() can be called at any time (independently of rendering pages). For example, it can be one of the first functions called upon starting your server (before even receving any HTTP request).

You can also access it over pageContext.globalContext.

The globalContext object also contains many internals (they are prefixed with _, e.g. globalContext._viteDevServer). You should use them only if strictly needed and, if you do, then let us know so that we can add official support for your use case (otherwise you'll expose yourself to breaking changes upon any version update).

getGlobalContextAsync()

import { getGlobalContextAsync } from 'vike/server'
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 pageContext.globalContext instead.

Same as getGlobalContext() but synchronous.

See also