Publishing
When setting up a site in your Clay instance you add a controller (index.js
file) to define the rules around your sites. One of the rules you can define is how to determine what url to publish a page to. This can be done in a number of ways, whether it's just using the current date or analyzing the data in the content of the page. The implementation is your decision (as long as the url matches a defined Express route).
Setting Publish Rules
To add publishing rules simply export an Array of functions under the resolvePublishUrl
property in your site's controller.
The functions will receive the same arguments as a model.js
/upgrade.js
:
uri
: corresponds to the uri for the page being publisheddata
: the data for the page. Is not fully resolved page data, simply any references in page areaslocals
: information around the request, including the site it's being published to
Each function in the Array should return a Promise which only resolves a single string value, the url to be published to. The functions will be executed in order, but the first one that resolves a value will be accepted and the rest will be ignored. This is "reject quickly/resolve slowly" pattern, meaning each of the functions should reject quickly if the data doesn't match a format they want.
The value that the functions return should be the full url (protocol, host, path, etc). This value will be used when creating uris
to map vanity urls to proper instances of pages
.
Dynamic Pages & Publishing
With this property set Amphora will publish the page without generating a uri. As long as your router properly defines the page to use for the route, the page will render its dynamic content.
Modifying Page Data
Sometimes you'll want to modify the @published
instance of page data when publishing. Rather than increasing the complexity of resolvePublishUrl
to handle this task, a separate API is provided purely for this process.
Exporting an Array in modifyPublishedData
will signify to Amphora that page data should be modified before being saved. A common use case for this is adding a "last modified" property to the page data for site maps or other uses. Using this API, your controller might include the following:
Each function in the modifyPublishedData
will only recieve the page data, not the locals
or uri
of the page. The purpose of this is to discourage complex/time consuming modifications of the data. While functions in this Array can be both synchronous or asynchronous, it's advised to keep these functions simple.
publishUrl
publishUrl
The question often arises of how to determine the vanity url of a page inside of a component's model.js
. When a page is published, a @published
instance of the page and every component instance is created. During this process, the publishUrl
property is added to the locals
object sent to each component. This essentially allows you to know when a save
is being run on publish or not and is handy if the vanity url is needed in your component.
Last updated