Skip to content

Commit 896bcda

Browse files
authored
feat: Use Netlify Host with Production Context (#47)
# Description The build plugin currently primarily utilizes the `DEPLOY_PRIME_URL` environment variable to determine the Netlify host for building URLs specifically with the Fetch API. The issues is that in production context, this URL will still prepend the branch (like "main") instead of the primary Netlify host. This checks the deploy context and if its production, uses the `NETLIFY_HOST` variable which includes that primary host. ## Issue Ticket Number <!-- Specifiy which issue this fixes by referencing the issue number (`#11`) or issue URL. --> <!-- Example: Fixes #1 --> Fixes #45 ## Type of change <!-- Please select all options that are applicable. --> - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update # Checklist <!-- These must all be followed and checked. --> - [ ] I have followed the contributing guidelines of this project as mentioned in [CONTRIBUTING.md](/CONTRIBUTING.md) - [ ] I have created an [issue](https://github.com/colbyfayock/netlify-plugin-cloudinary/issues) ticket for this PR - [ ] I have checked to ensure there aren't other open [Pull Requests](https://github.com/colbyfayock/netlify-plugin-cloudinary/pulls) for the same update/change? - [ ] I have performed a self-review of my own code - [ ] I have run tests locally to ensure they all pass - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes needed to the documentation
1 parent 7f91265 commit 896bcda

File tree

3 files changed

+71
-15
lines changed

3 files changed

+71
-15
lines changed

src/data/errors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
22
ERROR_CLOUD_NAME_REQUIRED: 'A Cloudinary Cloud Name is required. Please set cloudName input or use the environment variable CLOUDINARY_CLOUD_NAME',
33
ERROR_NETLIFY_HOST_UNKNOWN: 'Cannot determine Netlify host, can not proceed with plugin.',
4-
EEROR_NETLIFY_HOST_CLI_SUPPORT: 'Note: The Netlify CLI does not currently support the ability to determine the host locally, try deploying on Netlify.'
4+
ERROR_NETLIFY_HOST_CLI_SUPPORT: 'Note: The Netlify CLI does not currently support the ability to determine the host locally, try deploying on Netlify.'
55
}

src/index.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const glob = require('glob');
44

55
const { configureCloudinary, updateHtmlImagesToCloudinary, getCloudinaryUrl } = require('./lib/cloudinary');
66
const { PUBLIC_ASSET_PATH } = require('./data/cloudinary');
7-
const { ERROR_CLOUD_NAME_REQUIRED, ERROR_NETLIFY_HOST_UNKNOWN, EEROR_NETLIFY_HOST_CLI_SUPPORT } = require('./data/errors');
7+
const { ERROR_CLOUD_NAME_REQUIRED, ERROR_NETLIFY_HOST_UNKNOWN, ERROR_NETLIFY_HOST_CLI_SUPPORT } = require('./data/errors');
88

99
const CLOUDINARY_ASSET_DIRECTORIES = [
1010
{
@@ -23,11 +23,14 @@ const _cloudinaryAssets = {};
2323

2424
module.exports = {
2525
async onBuild({ netlifyConfig, constants, inputs, utils }) {
26-
console.log('Creating redirects...');
26+
console.log('[Cloudinary] Creating redirects...');
2727

2828
const { PUBLISH_DIR } = constants;
2929

30-
const host = process.env.DEPLOY_PRIME_URL || process.env.NETLIFY_HOST;
30+
const isProduction = process.env.CONTEXT === 'production';
31+
const host = isProduction ? process.env.NETLIFY_HOST : process.env.DEPLOY_PRIME_URL;
32+
33+
console.log(`[Cloudinary] Using host: ${host}`);
3134

3235
const {
3336
deliveryType,
@@ -37,8 +40,8 @@ module.exports = {
3740
} = inputs;
3841

3942
if ( !host && deliveryType === 'fetch' ) {
40-
console.warn(ERROR_NETLIFY_HOST_UNKNOWN);
41-
console.log(EEROR_NETLIFY_HOST_CLI_SUPPORT);
43+
console.warn(`[Cloudinary] ${ERROR_NETLIFY_HOST_UNKNOWN}`);
44+
console.log(`[Cloudinary] ${ERROR_NETLIFY_HOST_CLI_SUPPORT}`);
4245
return;
4346
}
4447

@@ -64,8 +67,8 @@ module.exports = {
6467
const imagesFiles = imagesDirectory.filter(file => !!path.extname(file));
6568

6669
if ( imagesFiles.length === 0 ) {
67-
console.warn(`No image files found in ${imagesPath}`);
68-
console.log(`Did you update your images path? You can set the imagesPath input in your Netlify config.`);
70+
console.warn(`[Cloudinary] No image files found in ${imagesPath}`);
71+
console.log(`[Cloudinary] Did you update your images path? You can set the imagesPath input in your Netlify config.`);
6972
}
7073

7174
try {
@@ -143,20 +146,20 @@ module.exports = {
143146
}));
144147
}
145148

146-
console.log('Done.');
149+
console.log('[Cloudinary] Done.');
147150
},
148151

149152
// Post build looks through all of the output HTML and rewrites any src attributes to use a cloudinary URL
150153
// This only solves on-page references until any JS refreshes the DOM
151154

152155
async onPostBuild({ constants, inputs, utils }) {
153-
console.log('Replacing on-page images with Cloudinary URLs...');
156+
console.log('[Cloudinary] Replacing on-page images with Cloudinary URLs...');
154157

155158
const host = process.env.DEPLOY_PRIME_URL || process.env.NETLIFY_HOST;
156159

157160
if ( !host ) {
158-
console.warn(ERROR_NETLIFY_HOST_UNKNOWN);
159-
console.log(EEROR_NETLIFY_HOST_CLI_SUPPORT);
161+
console.warn(`[Cloudinary] ${ERROR_NETLIFY_HOST_UNKNOWN}`);
162+
console.log(`[Cloudinary] ${ERROR_NETLIFY_HOST_CLI_SUPPORT}`);
160163
return;
161164
}
162165

@@ -210,10 +213,10 @@ module.exports = {
210213
const errors = results.filter(({ errors }) => errors.length > 0);
211214

212215
if ( errors.length > 0) {
213-
console.log(`Done with ${errors.length} errors...`);
216+
console.log(`[Cloudinary] Done with ${errors.length} errors...`);
214217
console.log(JSON.stringify(errors, null, 2));
215218
} else {
216-
console.log('Done.');
219+
console.log('[Cloudinary] Done.');
217220
}
218221
}
219222

tests/on-build.test.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ describe('onBuild', () => {
1414

1515
describe('Redirects', () => {
1616

17-
test('should create redirects with defaut fetch-based configuration', async () => {
17+
test('should create redirects with defaut fetch-based configuration in production context', async () => {
1818
const imagesFunctionName = 'cld_images';
1919

2020
fs.readdir.mockResolvedValue([imagesFunctionName]);
2121

2222
process.env.SITE_NAME = 'cool-site';
2323
process.env.CLOUDINARY_CLOUD_NAME = 'testcloud';
24+
process.env.CONTEXT = 'production';
2425
process.env.NETLIFY_HOST = 'https://netlify-plugin-cloudinary.netlify.app';
2526

2627
const deliveryType = 'fetch';
@@ -65,6 +66,58 @@ describe('onBuild', () => {
6566
expect(redirects[2]).toEqual(defaultRedirect);
6667
});
6768

69+
test('should create redirects with defaut fetch-based configuration in deploy preview context', async () => {
70+
const imagesFunctionName = 'cld_images';
71+
72+
fs.readdir.mockResolvedValue([imagesFunctionName]);
73+
74+
process.env.SITE_NAME = 'cool-site';
75+
process.env.CLOUDINARY_CLOUD_NAME = 'testcloud';
76+
process.env.CONTEXT = 'deploy-preview';
77+
process.env.DEPLOY_PRIME_URL = 'https://deploy-preview-1234--netlify-plugin-cloudinary.netlify.app';
78+
79+
const deliveryType = 'fetch';
80+
const imagesPath = '/images';
81+
82+
const defaultRedirect = {
83+
from: '/path',
84+
to: '/other-path',
85+
status: 200
86+
}
87+
88+
const redirects = [defaultRedirect];
89+
90+
const netlifyConfig = {
91+
redirects
92+
};
93+
94+
await onBuild({
95+
netlifyConfig,
96+
constants: {
97+
PUBLISH_DIR: `.next/out${imagesPath}`
98+
},
99+
inputs: {
100+
deliveryType
101+
}
102+
});
103+
104+
expect(redirects[0]).toEqual({
105+
from: `${imagesPath}/*`,
106+
to: `https://res.cloudinary.com/${process.env.CLOUDINARY_CLOUD_NAME}/image/${deliveryType}/f_auto,q_auto/${process.env.DEPLOY_PRIME_URL}/cld-assets${imagesPath}/:splat`,
107+
status: 302,
108+
force: true
109+
});
110+
111+
expect(redirects[1]).toEqual({
112+
from: `/cld-assets${imagesPath}/*`,
113+
to: `${imagesPath}/:splat`,
114+
status: 200,
115+
force: true
116+
});
117+
118+
expect(redirects[2]).toEqual(defaultRedirect);
119+
});
120+
68121
});
69122

70123
});

0 commit comments

Comments
 (0)