Basics
Features
Best Practices
Addon Development
Related Docs
Dark Mode

#buildStarted Event

The buildStarted Event is fired when a Build is initiated, either by the user or an Addon. You can respond with an optional buildObjects key that allows you to define objects to be available for Templating. This is especially useful for putting your Addon's settings into the Build to be used by Components.

import cms from 'static-cms-addon'

let app = cms.createServer()

app.on('buildStarted', async e => {
  let settings = await cms.database.collection('settings').findOne()
  e.send({
    buildObjects: {
      apiUrl: settings.apiUrl,
    }
  })
})

app.listenToEvents('/api/events')
app.listen(cms.getPort())

In the above example, if your Addon's name in the manifest is some-addon, you can then use {{ addons['some-addon'].apiUrl }} when Templating.

Warning: Do not simply return all of your Addon's settings this way! Anything you return is available for Templating, which could include sensitive data you never want on the public site.

In addition to providing the settings your Addon's Components use, you should consider which settings the user might want to access for their own Components and Pages. Also, in addition to settings, you can provide other data, including live data fetched from an external API.

You may also choose not to return any buildObjects, but simply use this as a trigger for functionality within your Addon.

This Event will fire every time a Production Build/Audit, or Staging Site Audit is started. However, the Staging Site itself only fires this Event when data is changed within the CMS so it can update the Staging's Site's content. If your Addon needs to update buildObjects, you can trigger a Staging Site build by calling cms.build() (See the Addon Build API).