Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[STRF-9951] expose getter to access renderer's resource hints. #285

Merged
merged 1 commit into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
17 changes: 16 additions & 1 deletion spec/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ describe('render()', function() {
'pages/partial': '<p>{{variable}}</p>',
'pages/greet': '<h1>{{lang \'good\'}} {{lang \'morning\'}}</h1>',
'pages/pre': '{{{pre object}}}',
'pages/hints': '{{{ earlyHint themeCss "preload" type="style" }}}'
}));
},
getTranslations: () => {
Expand All @@ -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) {
Expand All @@ -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() {
Expand Down