Basics
Features
Best Practices
Addon Development
Related Docs
Dark Mode

#Pages and Layouts Direct Access

In almost all cases, your Addon should create Pages and RepoLayouts for your users. Addons in general are meant to create things for your users to use as they wish, which includes remixing your Addon's Components, changing Attributes, adding their own Fields to Collections, and in the case of Pages and Layouts, using your Pages and RepoLayouts as an example or not at all.

These features should only be used in the case of something like an external site importer or migration tool.

Do not configure your Addon to automatically create Pages when it starts. It should be your users' choice whether they use Pages provided by your Addon and if so, what their title, path, and content should be.

Pages are not removed when your Addon is uninstalled.

#Page Schema

{

  // uneditable properties
  _id: '63ed535f146fc26146829ffb',
  managedByAddonId: '639b8f837f5fb8a39404280a',
  parentVersionId: '6503da44456b3c276ad4ab8c',
  updatedAt: 1671486365027,
  createdAt: 1671486531695,
  builtPaths: [],

  // basic properties
  title: '',
  type: '',
  path: '',
  description: '',
  layoutId: '',
  isEnabled: true,

  // body properties
  lang: 'pug',
  body: '',

  // style properties
  styleLang: 'stylus',
  style: '',

  // tags
  tags: [],

  // scripts
  script: '',
  usesDataScript: false,
  dataScript: '',

  // seo
  favicon: '',
  noindex: false,
  nofollow: false,
  canonicalUrl: '',
  ogType: '',
  ogTitle: '',
  ogImages: '',
  ogDescription: '',
  ogArticleAuthors: '',
  ogArticlePublishedTime: '',
  ogArticleModifiedTime: '',
  ogArticleExpirationTime: '',
  ogArticleSection: '',
  ogArticleTags: '',
  schemas: [],

}

#Uneditable Properties

NameDescription
_idAssigned by MercuryCMS when the Page is created
parentVersionIdUsed by MercuryCMS to manage revision history
managedByAddonIdAll of your created Pages have this automatically added. Pages are not removed when the Addon is uninstalled
createdAtJavaScript Date the Page was created
updatedAtJavaScript Date the Page was last modified
builtPathsA list of paths that were generated when this Page last built for Staging

#Basic Properties

NameDescription
titleThe Page's Title used in the <title> tag
typeThe Page's Type. This can be '' for normal Pages, '404' for 404s, or '500' for 500 pages. Default is ''
pathThe path of the Page. The user will likely change this path. Example: /path/to/page. For 404 and 500 pages, this will be a regex to match against the path
descriptionOptional, but you should always provide a description of what the Page does
layoutIdThe Layout for the Page to use
isEnabledSet to false to disable the Page

#Body Properties

NameDescription
langLanguage to use for the body. Either "html" or "pug". Default is "pug"
bodyBody HTML/Pug. This is meant to be further edited by the user after the Page is created, so don't use minification

#Style Properties

NameDescription
styleLangLanguage to use for the style. Either "css" or "stylus". Default is "stylus"
styleStyle CSS/Stylus. This is meant to be further edited by the user after the Page is created, so don't use minification

#Tags

This is a list of Tags in the order they should be added to the Page. See Tags for more information.

NameDescription
nameThe name of the Tag. For example: "Google Analytics"
locationOne of "head", "bodyStart", or "bodyEnd". Default is "head"
langThe language of the tag body. Either "pug" or "html". Default is "html"
bodyThe HTML/Pug body of the Tag

#Scripts

NameDescription
scriptThe script to be used on the Page
usesDataScriptWhen true, the Build system will use the Page's dataScript to determine dynamic data and paths. Otherwise dataScript is ignored
dataScriptThe script used to create determine the data for use in the Page's markup

#SEO Properties

Many of a Page's SEO properties relate to Open Graph.

NameDescription
faviconA Media ID to use for the Page's favicon
noindexWhen true, tell robots not to index the Page
nofollowWhen true, tell robots not to follow links on the Page
canonicalUrlThe Page's canonical URL
ogTypeThe Page's Open Graph type. Either article, book, music.album, music.playlist, profile, music.radio_station, music.song, video.episode, video.movie, video.other, video.tv_show, or website. Default is website
ogTitleThe Page's Open Graph title
ogImagesA list of paths or URLs for the Page's Open Graph images as a comma-separated string
ogDescriptionThe Page's Open Graph description
ogArticleAuthorsThe Page's Open Graph authors. Only applies if ogType is article
ogArticlePublishedTimeThe Page's Open Graph published time. This will generally be a generated value using Templating. Only applies if ogType is article
ogArticleModifiedTimeThe Page's Open Graph modified time. This will generally be a generated value using Templating. Only applies if ogType is article
ogArticleExpirationTimeThe Page's Open Graph expiration time. This will generally be a generated value using Templating. Only applies if ogType is article
ogArticleSectionThe Page's Open Graph article section (article category like "Technology"). Only applies if ogType is article
ogArticleTagsThe Page's Open Graph tags (keywords) as a comma-separated string. Only applies if ogType is article
schemasAn array of Rich Snippets to add to the page. See Rich Snippets

#Creating Pages

You can create Pages using cms.createPage(), which will return a Page Draft. Only ever create Pages from direct user-interaction. Do not automatically create Pages for users when your Addon starts.

let pageDef = {
  title: 'My Page',
  path: '/my-page',
  description: 'A Demo Page',
  body: '.my-page\n  This is my demo page!',
  style: ".my-page\n  color {{ addons['demo-addon'].mainColor }}",
}

// use static-cms-addon
import cms from 'static-cms-addon'

let pageDraft = await cms.createPage(pageDef)
console.log(pageDraft.doc._id) // "63ed535f146fc26146829ffb"

// use axios
let response = await axios.post(`http://localhost:${cmsPort}/api/addon/${addonId}/page`, pageDef)
console.log(response.data.pageDraft.doc._id) // "63ed535f146fc26146829ffb"

You can also load the Page from a zip or folder using cms.loadPagePackage().

let pageDef = cms.loadPagePackage('pages/myPage.page.zip').page
let pageDraft = await cms.createPage(pageDef)

#Converting HTML and CSS

This API is only ever meant to be used for the purposes of importing external content or migrating from other sites. Because most external sites use HTML and CSS, you can specify convertBodyTo: 'pug' and convertStyleTo: 'stylus' when creating Pages. If the HTML and CSS provided is invalid and cannot be converted, it will still save the Page in the original language. lang and styleLang will be automatically set when using these options. Ex:

let pageDef = {
  title: 'My Page',
  path: '/my-page',
  description: 'A Demo Page',
  body: '<div class="my-page">This is my demo page!</div>',
  style: '.my-page {\n  color: red;\n}',
  convertBodyTo: 'pug',
  convertStyleTo: 'stylus',
}

// use static-cms-addon
import cms from 'static-cms-addon'

let pageDraft = await cms.createPage(pageDef)
console.log(pageDraft.doc._id) // "63ed535f146fc26146829ffb"

// use axios
let response = await axios.post(`http://localhost:${cmsPort}/api/addon/${addonId}/page`, pageDef)
console.log(response.data.pageDraft.doc._id) // "63ed535f146fc26146829ffb"

#Listing Pages

You can use cms.getPages() and cms.getPageDrafts() to get a list of the Pages in the CMS. This includes Pages your Addon has not created. Any Pages your Addon has created will have a managedByAddonId key with your Addon's ID as the value.

// use static-cms-addon
import cms from 'static-cms-addon'

let pages = await cms.getPages()
let pageDrafts = await cms.getPageDrafts()
console.log(pages.map(page => page.title)) // ['My Page', 'Another Page']
console.log(pages.map(pageDraft => pageDraft.doc.title)) // ['A Third Page']

// use axios
let response = await axios.get(`http://localhost:${cmsPort}/api/addon/${addonId}/page`)
console.log(response.data.pages.map(page => page.title)) // ['My Page', 'Another Page']
console.log(response.data.pageDrafts.map(pageDraft => pageDraft.doc.title)) // ['A Third Page']

#User Modification

Once you have created a Page, your Addon can no longer modify it or delete it. It is also not removed when your Addon is uninstalled. The user may decide to rename it, change its path, alter its content, or delete it. Remember, do not create Pages when your Addon starts or the user will become frustrated with your Addon and uninstall it.

#Rich Snippets

For details about adding Rich Snippets to Pages and Layouts, see Rich Snippets.