Context

The Context object is used to provide extra information and utilities to middleware, loaders, actions and handlers.

Along with the fields below, you can also inject your own with the context prop in your firebolt.config.js. This can be used to provide database access, auth utilities etc.

cookies

A utility to read and write cookies. Any cookie changes made during middleware, loaders, actions and handlers will automatically be applied to the final response before it is sent.

// get a cookie ctx.cookies.get('auth') // set a cookie ctx.cookies.set('auth', auth) // set a cookie with options ctx.cookies.set('auth', auth, { expires: 30 }) // remove a cookie ctx.cookies.set('auth', null)

See the useCookie reference to learn more about cookies, options and synchronization.

req

Restricted to: Middleware

A Web API Request object.

Firebolt adds some additional fields to this object to make it easier to work with:

  • .pathname The pathname (eg /cities/123)
  • .href: The pathname and search params ( eg /cities/123?details=true)

headers

Restricted to: Middleware

Allows you to append or modify headers in middleware. The object is shared across all middleware and headers are applied to the final response before it is sent.

This can be useful to enable CORS or tag requests with an ID etc.

expire(seconds)

Restricted to: Loaders

By default data fetched from a Loader is cached indefinitely. Use this to set the data to expire after a certain amount of time, or immediately.

When data expires it becomes invalidated. Any components still observing the data it will be re-fetched in the background and automatically updated without triggering Suspense boundaries. If no components are observing the data then the data is evicted, causing the next read to trigger Suspense boundaries as if the data was never there to begin with.

export async function getTickets(ctx) { const tickets = await ctx.db('tickets') ctx.expire(60) // data will become invalidated after 60 seconds return tickets }

invalidate(...args)

Restricted to: Actions

Notifies the client to invalidate data matching the supplied args. Equivalent to calling cache.invalidate(...args) on the client.

redirect(url, type)

Restricted to: Actions, Loaders

Throws a redirect that will navigate the user to another page. The type argument can be push (default) or replace.

ctx.redirect('/login')

error(code)

Restricted to: Actions, Loaders

Throws an operational error that can be caught by an <ErrorBoundary>, decorating the error with an optional code prop that can be used to provide more context about operational errors.

params

Restricted to: Handlers

An object that includes search params and dynamic route segments for the current route.