Skip to content

Commit f71206d

Browse files
committed
removing function deployment and function redirects
1 parent 64da735 commit f71206d

File tree

4 files changed

+41
-159
lines changed

4 files changed

+41
-159
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
},
1414
"license": "MIT",
1515
"dependencies": {
16-
"@vercel/ncc": "^0.33.1",
1716
"cloudinary": "^1.27.1",
1817
"fs-extra": "^10.0.0",
1918
"glob": "^7.2.0",

src/index.js

Lines changed: 41 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
const fs = require('fs-extra')
22
const path = require('path');
33
const glob = require('glob');
4-
const ncc = require('@vercel/ncc');
54

65
const { configureCloudinary, updateHtmlImagesToCloudinary } = require('./lib/cloudinary');
7-
const { PREFIX, PUBLIC_ASSET_PATH } = require('./data/cloudinary');
6+
const { PUBLIC_ASSET_PATH } = require('./data/cloudinary');
87

98
const CLOUDINARY_MEDIA_FUNCTIONS = [
109
{
@@ -22,150 +21,72 @@ const CLOUDINARY_MEDIA_FUNCTIONS = [
2221
module.exports = {
2322

2423
async onBuild({ netlifyConfig, constants, inputs }) {
25-
const { FUNCTIONS_SRC, INTERNAL_FUNCTIONS_SRC, PUBLISH_DIR } = constants;
24+
const host = process.env.DEPLOY_PRIME_URL;
25+
26+
if ( !host ) {
27+
console.warn('Can not determine Netlify host, not proceeding with on-page image replacement.');
28+
console.log('Note: the Netlify CLI does not currently support the ability to determine the host locally, try deploying on Netlify.');
29+
return;
30+
}
31+
32+
const { PUBLISH_DIR } = constants;
33+
2634
const {
2735
deliveryType,
28-
folder = process.env.SITE_NAME,
2936
uploadPreset,
37+
folder = process.env.SITE_NAME
3038
} = inputs;
3139

3240
const cloudName = process.env.CLOUDINARY_CLOUD_NAME || inputs.cloudName;
41+
const apiKey = process.env.CLOUDINARY_API_KEY;
42+
const apiSecret = process.env.CLOUDINARY_API_SECRET;
3343

3444
if ( !cloudName ) {
35-
throw new Error('Cloudinary Cloud Name required. Please set cloudName input or use environment variable CLOUDINARY_CLOUD_NAME');
45+
throw new Error('Cloudinary Cloud Name required. Please use environment variable CLOUDINARY_CLOUD_NAME');
3646
}
3747

48+
configureCloudinary({
49+
cloudName,
50+
apiKey,
51+
apiSecret
52+
});
53+
3854
const imagesDirectory = glob.sync(`${PUBLISH_DIR}/images/**/*`);
3955
const imagesFiles = imagesDirectory.filter(file => !!path.extname(file));
4056

41-
imagesFiles.forEach(file => {
57+
await Promise.all(imagesFiles.map(async file => {
4258
const filePath = file.replace(PUBLISH_DIR, '');
4359

44-
netlifyConfig.redirects.unshift({
45-
from: `${path.join(PUBLIC_ASSET_PATH, filePath)}*`,
46-
to: `${filePath}:splat`,
47-
status: 200,
48-
force: true
49-
});
60+
const cldAssetPath = path.join(PUBLIC_ASSET_PATH, filePath);
61+
const cldAssetUrl = `${host}/${cldAssetPath}`;
5062

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: 302,
55-
force: true
63+
const assetRedirectUrl = await getCloudinaryUrl({
64+
deliveryType: 'fetch',
65+
folder,
66+
path: `${cldAssetUrl}:splat`,
67+
uploadPreset
5668
});
5769

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-
7370
netlifyConfig.redirects.unshift({
74-
from: path.join(PUBLIC_ASSET_PATH, mediaPath, '*'),
75-
to: mediaPathSplat,
71+
from: `${cldAssetPath}*`,
72+
to: `${filePath}:splat`,
7673
status: 200,
7774
force: true
7875
});
7976

8077
netlifyConfig.redirects.unshift({
81-
from: path.join(mediaPath, '*'),
82-
to: `/.netlify/functions/${functionName}?path=${mediaPathSplat}&${paramsString}`,
78+
from: `${filePath}*`,
79+
to: assetRedirectUrl,
8380
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-
96-
const functionsPath = INTERNAL_FUNCTIONS_SRC || FUNCTIONS_SRC;
97-
98-
// Copy all of the templates over including the functions to deploy
99-
100-
const functionTemplatesPath = path.join(__dirname, 'templates/functions');
101-
const functionTemplates = await fs.readdir(functionTemplatesPath);
102-
103-
if ( !Array.isArray(functionTemplates) || functionTemplates.length == 0 ) {
104-
throw new Error(`Failed to generate templates: can not find function templates in ${functionTemplatesPath}`);
105-
}
106-
107-
try {
108-
await Promise.all(functionTemplates.map(async templateFileName => {
109-
const bundle = await ncc(path.join(functionTemplatesPath, templateFileName));
110-
const { name, base } = path.parse(templateFileName);
111-
const templateDirectory = path.join(functionsPath, name);
112-
const filePath = path.join(templateDirectory, base);
113-
114-
await fs.ensureDir(templateDirectory);
115-
await fs.writeFile(filePath, bundle.code, 'utf8');
116-
}));
117-
} catch(e) {
118-
throw new Error(`Failed to generate templates: ${e}`);
119-
}
120-
121-
// Configure reference parameters for Cloudinary delivery to attach to redirect
122-
123-
const params = {
124-
uploadPreset,
125-
deliveryType,
126-
cloudName,
127-
folder
128-
}
129-
130-
const paramsString = Object.keys(params)
131-
.filter(key => typeof params[key] !== 'undefined')
132-
.map(key => `${key}=${encodeURIComponent(params[key])}`)
133-
.join('&');
134-
135-
// Redirect any requests that hits /[media type]/* to a serverless function
136-
137-
CLOUDINARY_MEDIA_FUNCTIONS.forEach(({ name: mediaName, inputKey, path: defaultPath }) => {
138-
const mediaPath = inputs[inputKey] || defaultPath;
139-
const mediaPathSplat = path.join(mediaPath, ':splat');
140-
const functionName = `${PREFIX}_${mediaName}`;
141-
142-
netlifyConfig.redirects.unshift({
143-
from: path.join(PUBLIC_ASSET_PATH, mediaPath, '*'),
144-
to: mediaPathSplat,
145-
status: 200,
14681
force: true
14782
});
148-
149-
netlifyConfig.redirects.unshift({
150-
from: path.join(mediaPath, '*'),
151-
to: `/.netlify/functions/${functionName}?path=${mediaPathSplat}&${paramsString}`,
152-
status: 302,
153-
force: true,
154-
});
155-
});
156-
83+
}));
15784
},
15885

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

16289
async onPostBuild({ constants, inputs }) {
163-
const { PUBLISH_DIR } = constants;
164-
const {
165-
deliveryType,
166-
uploadPreset,
167-
folder = process.env.SITE_NAME
168-
} = inputs;
16990

17091
const host = process.env.DEPLOY_PRIME_URL;
17192

@@ -175,6 +96,13 @@ module.exports = {
17596
return;
17697
}
17798

99+
const { PUBLISH_DIR } = constants;
100+
const {
101+
deliveryType,
102+
uploadPreset,
103+
folder = process.env.SITE_NAME
104+
} = inputs;
105+
178106
const cloudName = process.env.CLOUDINARY_CLOUD_NAME || inputs.cloudName;
179107
const apiKey = process.env.CLOUDINARY_API_KEY;
180108
const apiSecret = process.env.CLOUDINARY_API_SECRET;

src/templates/functions/cld_images.js

Lines changed: 0 additions & 40 deletions
This file was deleted.

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -597,11 +597,6 @@
597597
dependencies:
598598
"@types/yargs-parser" "*"
599599

600-
"@vercel/ncc@^0.33.1":
601-
version "0.33.1"
602-
resolved "https://registry.yarnpkg.com/@vercel/ncc/-/ncc-0.33.1.tgz#b240080a3c1ded9446a30955a06a79851bb38f71"
603-
integrity sha512-Mlsps/P0PLZwsCFtSol23FGqT3FhBGb4B1AuGQ52JTAtXhak+b0Fh/4T55r0/SVQPeRiX9pNItOEHwakGPmZYA==
604-
605600
abab@^2.0.3, abab@^2.0.5:
606601
version "2.0.5"
607602
resolved "https://registry.yarnpkg.com/abab/-/abab-2.0.5.tgz#c0b678fb32d60fc1219c784d6a826fe385aeb79a"

0 commit comments

Comments
 (0)