Skip to content

Commit 6bb45a7

Browse files
committed
adding individual redirects for each file
1 parent ed12cad commit 6bb45a7

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

demo/public/images/test/beach.jpeg

553 KB
Loading

src/index.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const CLOUDINARY_MEDIA_FUNCTIONS = [
2222
module.exports = {
2323

2424
async onBuild({ netlifyConfig, constants, inputs }) {
25-
const { FUNCTIONS_SRC, INTERNAL_FUNCTIONS_SRC } = constants;
25+
const { FUNCTIONS_SRC, INTERNAL_FUNCTIONS_SRC, PUBLISH_DIR } = constants;
2626
const {
2727
deliveryType,
2828
folder = process.env.SITE_NAME,
@@ -35,6 +35,64 @@ module.exports = {
3535
throw new Error('Cloudinary Cloud Name required. Please set cloudName input or use environment variable CLOUDINARY_CLOUD_NAME');
3636
}
3737

38+
const imagesDirectory = glob.sync(`${PUBLISH_DIR}/images/**/*`);
39+
const imagesFiles = imagesDirectory.filter(file => !!path.extname(file));
40+
41+
imagesFiles.forEach(file => {
42+
const filePath = file.replace(PUBLISH_DIR, '');
43+
44+
netlifyConfig.redirects.unshift({
45+
from: `${path.join(PUBLIC_ASSET_PATH, filePath)}*`,
46+
to: `${filePath}:splat`,
47+
status: 200,
48+
force: true
49+
});
50+
51+
netlifyConfig.redirects.unshift({
52+
from: `${filePath}*`,
53+
to: `https://res.cloudinary.com/colbycloud/image/fetch/f_auto,q_auto/${process.env.DEPLOY_PRIME_URL}/${path.join(PUBLIC_ASSET_PATH, filePath)}:splat`,
54+
status: 200,
55+
force: true
56+
});
57+
58+
// netlifyConfig.redirects.unshift({
59+
// from: `${filePath}*`,
60+
// to: `/.netlify/functions/${functionName}?path=${mediaPathSplat}&${paramsString}`,
61+
// status: 302,
62+
// force: true,
63+
// });
64+
})
65+
66+
return;
67+
68+
CLOUDINARY_MEDIA_FUNCTIONS.forEach(({ name: mediaName, inputKey, path: defaultPath }) => {
69+
const mediaPath = inputs[inputKey] || defaultPath;
70+
const mediaPathSplat = path.join(mediaPath, ':splat');
71+
const functionName = `${PREFIX}_${mediaName}`;
72+
73+
netlifyConfig.redirects.unshift({
74+
from: path.join(PUBLIC_ASSET_PATH, mediaPath, '*'),
75+
to: mediaPathSplat,
76+
status: 200,
77+
force: true
78+
});
79+
80+
netlifyConfig.redirects.unshift({
81+
from: path.join(mediaPath, '*'),
82+
to: `/.netlify/functions/${functionName}?path=${mediaPathSplat}&${paramsString}`,
83+
status: 302,
84+
force: true,
85+
});
86+
});
87+
88+
89+
90+
91+
console.log('PUBLISH_DIR', PUBLISH_DIR);
92+
console.log('imagesFiles', imagesFiles);
93+
94+
throw new Error('asdf');
95+
3896
const functionsPath = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC;
3997

4098
// Copy all of the templates over including the functions to deploy

0 commit comments

Comments
 (0)