#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
Name | Description |
---|---|
_id | Assigned by MercuryCMS when the Page is created |
parentVersionId | Used by MercuryCMS to manage revision history |
managedByAddonId | All of your created Pages have this automatically added. Pages are not removed when the Addon is uninstalled |
createdAt | JavaScript Date the Page was created |
updatedAt | JavaScript Date the Page was last modified |
builtPaths | A list of paths that were generated when this Page last built for Staging |
#Basic Properties
Name | Description |
---|---|
title | The Page's Title used in the <title> tag |
type | The Page's Type. This can be '' for normal Pages, '404' for 404s, or '500' for 500 pages. Default is '' |
path | The 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 |
description | Optional, but you should always provide a description of what the Page does |
layoutId | The Layout for the Page to use |
isEnabled | Set to false to disable the Page |
#Body Properties
Name | Description |
---|---|
lang | Language to use for the body. Either "html" or "pug" . Default is "pug" |
body | Body HTML/Pug. This is meant to be further edited by the user after the Page is created, so don't use minification |
#Style Properties
Name | Description |
---|---|
styleLang | Language to use for the style. Either "css" or "stylus" . Default is "stylus" |
style | Style 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.
Name | Description |
---|---|
name | The name of the Tag. For example: "Google Analytics" |
location | One of "head" , "bodyStart" , or "bodyEnd" . Default is "head" |
lang | The language of the tag body. Either "pug" or "html" . Default is "html" |
body | The HTML/Pug body of the Tag |
#Scripts
Name | Description |
---|---|
script | The script to be used on the Page |
usesDataScript | When true , the Build system will use the Page's dataScript to determine dynamic data and paths. Otherwise dataScript is ignored |
dataScript | The 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.
Name | Description |
---|---|
favicon | A Media ID to use for the Page's favicon |
noindex | When true , tell robots not to index the Page |
nofollow | When true , tell robots not to follow links on the Page |
canonicalUrl | The Page's canonical URL |
ogType | The 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 |
ogTitle | The Page's Open Graph title |
ogImages | A list of paths or URLs for the Page's Open Graph images as a comma-separated string |
ogDescription | The Page's Open Graph description |
ogArticleAuthors | The Page's Open Graph authors. Only applies if ogType is article |
ogArticlePublishedTime | The Page's Open Graph published time. This will generally be a generated value using Templating. Only applies if ogType is article |
ogArticleModifiedTime | The Page's Open Graph modified time. This will generally be a generated value using Templating. Only applies if ogType is article |
ogArticleExpirationTime | The Page's Open Graph expiration time. This will generally be a generated value using Templating. Only applies if ogType is article |
ogArticleSection | The Page's Open Graph article section (article category like "Technology"). Only applies if ogType is article |
ogArticleTags | The Page's Open Graph tags (keywords) as a comma-separated string. Only applies if ogType is article |
schemas | An 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.