Skip to content

This integration provides an import that exposes when in the lifecycle of an Astro codebase your code is running.

When/Where your code is running is made available as an exported constant, so you can change even the exported values of your modules based on it.

Installing the integration

This integration is needed during build time only, so you can install it as a dev dependency.

Terminal window
npm i @inox-tools/astro-when

Then add it to your Astro config:

astro.config.mjs
import astroWhen from '@inox-tools/astro-when';
export default defineConfig({
integrations: [astroWhen()],
});

How to use

Anywhere in your code, be it on a TypeScript file, an Astro component, an MDX file or any UI framework file, you can an import from @it-astro:when. This module exports the following:

  • An enum When defining the constant for each scenario your code might be running.
  • A constant whenAmI, which is a value of type When.

The possible values are:

  • When.Client: When your code is running on the client side.
  • When.Server: When your code is running on a prod server serving an SSR route.
  • When.Prerender: When your code is running during build to prerendeer a route on a project outputing for an SSR runtime.
  • When.StaticBuild: When your code is running during build to prerender a route on a project that is entirely static (no SSR adapter and output: 'static').
  • When.DevServer: When your code is running on the server during development.

Example

You can check this example on when each value shows up. You can also see it in action on a deployment of that same example here.

License

Astro When is available under the MIT license.