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-9949] paper-handlebars - cdn helper, add new resourceHints attribute #189

Merged
merged 1 commit into from
Aug 18, 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
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