Skip to content

Commit 93428d6

Browse files
committed
updating redirects to use wildcard
1 parent 3f4dcca commit 93428d6

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

src/index.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const glob = require('glob');
55
const { configureCloudinary, updateHtmlImagesToCloudinary, getCloudinaryUrl } = require('./lib/cloudinary');
66
const { PUBLIC_ASSET_PATH } = require('./data/cloudinary');
77

8-
const CLOUDINARY_MEDIA_FUNCTIONS = [
8+
const CLOUDINARY_ASSET_DIRECTORIES = [
99
{
1010
name: 'images',
1111
inputKey: 'imagesPath',
@@ -21,7 +21,7 @@ const CLOUDINARY_MEDIA_FUNCTIONS = [
2121
module.exports = {
2222

2323
async onBuild({ netlifyConfig, constants, inputs }) {
24-
const host = process.env.DEPLOY_PRIME_URL;
24+
const host = process.env.DEPLOY_PRIME_URL || process.env.NETLIFY_HOST;
2525

2626
if ( !host ) {
2727
console.warn('Cannot determine Netlify host, not proceeding with on-page image replacement.');
@@ -51,31 +51,27 @@ module.exports = {
5151
apiSecret
5252
});
5353

54-
const imagesDirectory = glob.sync(`${PUBLISH_DIR}/images/**/*`);
55-
const imagesFiles = imagesDirectory.filter(file => !!path.extname(file));
56-
57-
await Promise.all(imagesFiles.map(async file => {
58-
const filePath = file.replace(PUBLISH_DIR, '');
59-
60-
const cldAssetPath = `/${path.join(PUBLIC_ASSET_PATH, filePath)}`;
54+
await Promise.all(CLOUDINARY_ASSET_DIRECTORIES.map(async ({ name: mediaName, inputKey, path: defaultPath }) => {
55+
const mediaPath = inputs[inputKey] || defaultPath;
56+
const cldAssetPath = `/${path.join(PUBLIC_ASSET_PATH, mediaPath)}`;
6157
const cldAssetUrl = `${host}/${cldAssetPath}`;
6258

6359
const assetRedirectUrl = await getCloudinaryUrl({
6460
deliveryType: 'fetch',
6561
folder,
66-
path: `${cldAssetUrl}:splat`,
62+
path: `${cldAssetUrl}/:splat`,
6763
uploadPreset
6864
});
6965

7066
netlifyConfig.redirects.unshift({
7167
from: `${cldAssetPath}*`,
72-
to: `${filePath}:splat`,
68+
to: `${mediaPath}/:splat`,
7369
status: 200,
7470
force: true
7571
});
7672

7773
netlifyConfig.redirects.unshift({
78-
from: `${filePath}*`,
74+
from: `${mediaPath}/*`,
7975
to: assetRedirectUrl,
8076
status: 302,
8177
force: true

0 commit comments

Comments
 (0)