@kth/cortina-block v6.2.0
@kth/cortina-block
Fetch Cortina blocks and optionally cache using Redis.
Installation
npm install @kth/cortina-blockUsage
A basic NodeJS example is located under /examples/.
const cortina = require('@kth/cortina-block')
const options = {
/* see below for options */
}
// somewhere else, usually in an express controller
cortina(options)
.then(blocks => {
// blocks should be used in the layout/view
// each block contains HTML,
// meaning it should not be escaped in the view
// blocks is a plain object with the following properties:
// title, image, footer, search, language, analytics
res.render('page', { blocks })
})
.catch(err = > {
log.error({ err }, 'failed to get cortina blocks')
// either display the error:
res.render('error', { err })
// or render page without blocks:
res.render('page', { blocks: {} })
})Options
urlis required. Should point to the Cortina block API endpoint.headersis optional, pass headers used when fetching Cortina blocks.debugis optional, defaults tofalse. Enables logging of Redis errors.versionis optional, defaults tohead. Change if needed.languageis optional, defaults toen.redisKeyis optional, defaults toCortinaBlock_. Used as a prefix for the Redis cache. Thelanguagesetting will be appended.redisExpireis optional, defaults to600(10 minutes). Sets expiration time in seconds for blocks stored in Redis.redisis optional. Set to a Redis client instance when you want to enable Redis caching. Basically requires an object withgetandsetfunctions.blocksis optional. It's a plain object containing Cortina block IDs. The following IDs are default and can be overridden.titledefaults to1.260060.megaMenudefaults to1.855134.secondaryMenudefaults to1.865038.imagedefaults to1.77257.footerdefaults to1.202278.searchdefaults to1.77262.languageoptional object with language block IDs.endefaults to1.77273.svdefaults to1.272446.
klaroConfigdefaults to1.1137647.matomoAnalyticsdefaults to1.714097.
You can also add application specific blocks to the options obejct like this:
addBlocks: { placesSearch: '1.672888' }
Run tests
Clone this repository, run npm install followed by npm test.
New version
Run the npm version <version> command. See https://docs.npmjs.com/cli/version
for more details.
Prepare helper
A helper to modify paths in the blocks HTML for logo, site link/name, and locale link. The resulting blocks object should not be cached (i.e. called this after getting the blocks from the cache/API), because the locale URL changes with the request URL.
Usage
const cortina = require('@kth/cortina-block')
function prepare(blocks, req, config) {
return cortina.prepare(blocks, {
urls: {
request: req.url,
app: config.full.hostUrl + config.full.proxyPrefixPath.uri,
},
// more options below e.g.
// addBlocks: {
// myBlock: '1.1234',
// },
})
}Options
blocksis required. You get this from the main call tocortina.configis required. A plain object with the following properties:siteNameis optional. Set this to override the site link text.localeTextis optional. Set this to override the locale link text.urlsis required. A plain object with the following properties:prodis optional, defaults to//www.kth.se. The production URL.requestis required. Usually the expressreq.urlvalue.appis optional, defaults to an empty string. Usually the host URL and the proxy prefix path.siteUrlis optional, overides default url on the site name only.
selectorsis optional, defaults to a plain object with the following properties:logois optional, defaults to.imageWrapper img.siteNameis optional, defaults to.siteName a.localeLinkis optional, defaults to.block.link a.localeLink.
Returned blocks
title: "\n\n\n <h1 class=\"bloc…logy</a>\n </h1>\n\n\n",
megaMenu: "\n\n\n\n <nav class=\"b…\n </nav>\n\n \n\n",
secondaryMenu: "\n\n\n <div class=\"blo… </ul>\n </div>\n\n\n",
image: "\n\n\n <figure class=\"… \n </figure>\n\n\n",
footer: "\n\n\n <div class=\"blo… </div>\n </div>\n\n\n",
search: "\n\n\n <div class=\"blo…aded=!0);</script>\n\n\n",
language: "\n\n\n <a class=\"block…KTH på svenska</a>\n\n\n",
analytics: "\n\n\n \n <!-- conte…r\n};</script>\n \n\n\n",
gtmAnalytics: "\n\n\n <!-- Begin JavaS…entId-1_714097 -->\n\n\n",
gtmNoscript: "\n\n\n <!-- Begin HTML …entId-1_714099 -->\n\n\n"2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago