Skip to content

Commit

Permalink
[STRF-9949] paper-handlebars - cdn helper, add new resourceHints attr…
Browse files Browse the repository at this point in the history
…ibute (#189)
  • Loading branch information
rafa-avila-bc authored Aug 18, 2022
1 parent 740abc9 commit 098eecb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
22 changes: 21 additions & 1 deletion helpers/cdn.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
'use strict';

const factory = require('./lib/cdnify');
const cdnify = require('./lib/cdnify');
const {addResourceHint} = require('./lib/resourceHints');

const factory = globals => {
const cdn = cdnify(globals);

return function (path) {
const fullPath = cdn(path);

const options = arguments[arguments.length - 1];
if (options.hash.resourceHint) {
addResourceHint(
globals,
fullPath,
options.hash.resourceHint
);
}

return fullPath;
};
};

module.exports = [{
name: 'cdn',
Expand Down
36 changes: 32 additions & 4 deletions spec/helpers/cdn.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const Lab = require('lab'),
lab = exports.lab = Lab.script(),
describe = lab.experiment,
it = lab.it,
testRunner = require('../spec-helpers').testRunner;
lab = exports.lab = Lab.script(),
describe = lab.experiment,
it = lab.it,
{testRunner, buildRenderer} = require('../spec-helpers');
const {expect} = require("code");

describe('cdn helper', function () {
const context = {
Expand All @@ -23,6 +24,33 @@ describe('cdn helper', function () {
// Build a test runner that uses a default context
const runTestCases = testRunner({context, siteSettings, themeSettings, hbVersion});

it('should render the css cdn url and produce resource hints when attribute resourceHint is part of the template', function (done) {
const renderer = buildRenderer(siteSettings, themeSettings, {}, hbVersion);
const runTestCases = testRunner({context, renderer});
runTestCases([
{
input: '{{cdn "assets/css/style.css" resourceHint="preload"}}',
output: 'https://cdn.bcapp/3dsf74g/stencil/123/css/style.css',
},
{
input: '{{cdn "/assets/css/style.modal.css" resourceHint="preconnect"}}',
output: 'https://cdn.bcapp/3dsf74g/stencil/123/css/style.modal.css',
},
{
input: '{{cdn "/assets/css/style.modal.css" }}',
output: 'https://cdn.bcapp/3dsf74g/stencil/123/css/style.modal.css',
}
], () => {
const hints = renderer.getResourceHints();
expect(hints).to.have.length(2);
hints.forEach(hint => {
expect(hint.type).to.not.exist();
expect(hint.state).to.satisfy((state) => state === 'preload' || state === 'preconnect');
});
done();
});
});

it('should render the css cdn url', function (done) {
runTestCases([
{
Expand Down

0 comments on commit 098eecb

Please sign in to comment.