I'm following this answer which was really useful for me to have some pre-defined widgets on newly created pages.
// lib/modules/apostrophe-pages/index.js
construct: function(self, options) {
self.beforeInsert = function(req, page, options, callback) {
page.navigationArea = {
"type" : "area",
"items" : [
{
"by" : "id",
"limitByAll" : 5,
"limitByTag" : 5,
"navShadow" : false,
"linksArray" : [ ],
"_id" : self.apos.utils.generateId(),
"nav" : "default",
"pieceIds" : [ ],
"logoId" : null,
"navBackgroundImageId" : null,
"navColor" : null,
"navTextColor" : null,
"gradientColorTop" : null,
"gradientColorBottom" : null,
"tags" : [ ],
"type" : "navigations"
}
]
}
return setImmediate(callback);
};
}
But this adds the navigationArea
only on pages I create. Home which is created at startup seems to be excluded from that. The Home will have navigationArea
empty at first startup and I need to add it manually. How can I make sure that the function self.beforeInsert
is also applied to home?
I think if this was added before the database was ever created then beforeInsert
would cover it. Since there wasn't a home page yet, it would be inserted when the app first starts up. But let's assume you already created the app.
This might be job for a migration. You could write a migration that looks for the home page (slug: '/'
) and looks to see if that area is empty. If it is, then update the doc with your data. It should only run once since migrations are tracked in a database collection. Even if it does run again, as long as you are querying only "home pages" that don't yet have the data, you'll be safe.