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

Append CDN fingerprint to assets using CDN helpers #121

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion helpers/getContentImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ const factory = globals => {
const siteSettings = globals.getSiteSettings();

const cdnUrl = siteSettings.cdn_url || '';
const contentFolderFingerprint = siteSettings.content_folder_fingerprint || '';

const options = arguments[arguments.length - 1];

return getObjectStorageImage(cdnUrl, 'content', path, options);
return getObjectStorageImage(cdnUrl, 'content', path, options, contentFolderFingerprint);
};
};

Expand Down
3 changes: 2 additions & 1 deletion helpers/getContentImageSrcset.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ const factory = globals => {
const siteSettings = globals.getSiteSettings();

const cdnUrl = siteSettings.cdn_url || '';
const contentFolderFingerprint = siteSettings.content_folder_fingerprint || '';

return getObjectStorageImageSrcset(cdnUrl, 'content', path);
return getObjectStorageImageSrcset(cdnUrl, 'content', path, contentFolderFingerprint);
};
};

Expand Down
3 changes: 2 additions & 1 deletion helpers/getImageManagerImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ const factory = globals => {
const siteSettings = globals.getSiteSettings();

const cdnUrl = siteSettings.cdn_url || '';
const imageManagerFingerprint = siteSettings.image_manager_fingerprint || '';

const options = arguments[arguments.length - 1];

return getObjectStorageImage(cdnUrl, 'image-manager', path, options);
return getObjectStorageImage(cdnUrl, 'image-manager', path, options, imageManagerFingerprint);
};
};

Expand Down
3 changes: 2 additions & 1 deletion helpers/getImageManagerImageSrcset.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ const factory = globals => {
const siteSettings = globals.getSiteSettings();

const cdnUrl = siteSettings.cdn_url || '';
const imageManagerFingerprint = siteSettings.image_manager_fingerprint || '';

return getObjectStorageImageSrcset(cdnUrl, 'image-manager', path);
return getObjectStorageImageSrcset(cdnUrl, 'image-manager', path, imageManagerFingerprint);
};
};

Expand Down
8 changes: 7 additions & 1 deletion helpers/lib/cdnify.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
const URL = require('url').URL;

// Return a function that can be used to translate paths to cdn paths
module.exports = globals => {
Expand All @@ -14,6 +15,7 @@ module.exports = globals => {
const cdnUrl = siteSettings.cdn_url || '';
const versionId = siteSettings.theme_version_id;
const editSessionId = siteSettings.theme_session_id;
const contentFolderFingerprint = siteSettings.content_folder_fingerprint || '';
const cdnSettings = themeSettings.cdn;

if (path instanceof globals.handlebars.SafeString) {
Expand Down Expand Up @@ -56,7 +58,11 @@ module.exports = globals => {
}

if (protocol === 'webdav:') {
return [cdnUrl, 'content', path].join('/');
let assetUrl = new URL([cdnUrl, 'content', path].join('/'))
if (contentFolderFingerprint) {
assetUrl.searchParams.set('t', contentFolderFingerprint);
}
return assetUrl.toString();
}

if (cdnSettings) {
Expand Down
18 changes: 14 additions & 4 deletions helpers/lib/getObjectStorageImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const utils = require('handlebars-utils');
const common = require('./common');
const SafeString = require('handlebars').SafeString;
const URL = require('url').URL;

const srcsets = {
'80w': '80w',
Expand All @@ -14,7 +15,16 @@ const srcsets = {
'2560w': '2560w',
};

function getObjectStorageImage(cdnUrl, source, path, options) {
function generateUrl(cdnUrl, size, source, path, fingerprint) {
// Build sized image URL, appending fingerprint if known
let assetUrl = new URL(`${cdnUrl}/images/stencil/${size}/${source}/${path}`)
if (fingerprint) {
assetUrl.searchParams.set('t', contentFolderFingerprint);
}
return assetUrl.toString()
}

function getObjectStorageImage(cdnUrl, source, path, options, fingerprint) {
if (!utils.isString (path) || common.isValidURL(path)) {
throw new TypeError("Invalid image path - please use a filename or folder path starting from the appropriate folder");
}
Expand All @@ -32,16 +42,16 @@ function getObjectStorageImage(cdnUrl, source, path, options) {
}
}

return new SafeString(`${cdnUrl}/images/stencil/${size}/${source}/${path}`);
return new SafeString(generateUrl(cdnUrl, size, source, path, fingerprint));
}

function getObjectStorageImageSrcset(cdnUrl, source, path) {
function getObjectStorageImageSrcset(cdnUrl, source, path, fingerprint) {
if (!utils.isString (path) || common.isValidURL(path)) {
throw new TypeError("Invalid image path - please use a filename or folder path starting from the appropriate folder");
}

return new SafeString(Object.keys(srcsets).map(descriptor => {
return ([`${cdnUrl}/images/stencil/${srcsets[descriptor]}/${source}/${path} ${descriptor}`].join(' '));
return ([`${generateUrl(cdnUrl, srcsets[descriptor], source, path, fingerprint)} ${descriptor}`].join(' '));
}).join(', '));
}

Expand Down