From 9fc8f9970d30f5857c3557e591b878d0b5ce3841 Mon Sep 17 00:00:00 2001 From: Rafa Avila Date: Mon, 22 Aug 2022 15:11:19 -0500 Subject: [PATCH] [STRF-9951] expose getter to access renderer's resource hints. --- index.js | 21 +++++++++++++++++++++ package.json | 2 +- spec/index.js | 17 ++++++++++++++++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 66b2703..6bbfe0f 100644 --- a/index.js +++ b/index.js @@ -272,6 +272,27 @@ class Paper { return result; }); } + + /** + * Get resource hints produced by rendering process. + * If any helper included in the theme/template/string was + * configured to produce a resource hint, then, + * AFTER A SUCCESSFUL rendering, this getter may be called. + * + * Objects in the returned array will contain following properties: + * 1. `src: String` + * 1. `state: String` + * + * and MAY contain all or some of the following optional properties: + * 1. `type: String` + * 1. `cors: String` + * + * For more details check https://github.com/bigcommerce/paper-handlebars/blob/master/helpers/lib/resourceHints.js + * @returns {Object[]} + */ + getResourceHints() { + return this.renderer.getResourceHints(); + } } module.exports = Paper; diff --git a/package.json b/package.json index 26033f4..2c69513 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ }, "homepage": "https://github.com/bigcommerce/paper", "dependencies": { - "@bigcommerce/stencil-paper-handlebars": "5.0.6", + "@bigcommerce/stencil-paper-handlebars": "5.1.0", "accept-language-parser": "~1.4.1", "messageformat": "~0.2.2" }, diff --git a/spec/index.js b/spec/index.js index 5476832..e92383e 100644 --- a/spec/index.js +++ b/spec/index.js @@ -90,6 +90,7 @@ describe('render()', function() { 'pages/partial': '

{{variable}}

', 'pages/greet': '

{{lang \'good\'}} {{lang \'morning\'}}

', 'pages/pre': '{{{pre object}}}', + 'pages/hints': '{{{ earlyHint themeCss "preload" type="style" }}}' })); }, getTranslations: () => { @@ -99,7 +100,8 @@ describe('render()', function() { const context = { variable: 'hello world', - object: {} + object: {}, + themeCss: '/my/asset/style.css' }; it('should render pages/product', function(done) { @@ -121,6 +123,19 @@ describe('render()', function() { }); }); }); + + it('should render pages/hints and find resource hints', done => { + const paper = new Paper(null, null, assembler); + paper.loadTheme('pages/product', '') + .then(() => paper.render('pages/hints', context)) + .then(result => { + expect(result).to.equals(context.themeCss); + let hints = paper.getResourceHints(); + expect(hints).to.have.length(1); + expect(hints[0]).to.equals({src: context.themeCss, state: 'preload', type: 'style', cors: 'no'}); + done(); + }); + }); }); describe('renderTheme()', function() {