Skip to content

Commit 06e2c12

Browse files
feat: use node-cache during build (#922)
1 parent 1e703b1 commit 06e2c12

File tree

9 files changed

+92
-35
lines changed

9 files changed

+92
-35
lines changed

package-lock.json

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
"lint-staged": "15.2.7",
9595
"lodash": "4.17.21",
9696
"md5": "2.3.0",
97+
"node-cache": "5.1.2",
9798
"node-fetch": "2.7.0",
9899
"npm-run-all2": "6.2.2",
99100
"piscina": "4.6.1",

src/_data/site.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@ module.exports = async () => {
4141
site.icon = iconUrl;
4242

4343
// Determine image dimensions before server runs for structured data
44-
const logoDimensions = await getImageDimensions(logoUrl);
45-
const coverImageDimensions = await getImageDimensions(coverImageUrl);
44+
const logoDimensions = await getImageDimensions(
45+
logoUrl,
46+
`Site logo: ${logoUrl}`
47+
);
48+
const coverImageDimensions = await getImageDimensions(
49+
coverImageUrl,
50+
`Site cover image: ${coverImageUrl}`
51+
);
4652

4753
site.image_dimensions = {
4854
logo: {

utils/cache.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const NodeCache = require('node-cache');
2+
3+
const cache = new NodeCache();
4+
5+
const getCache = key => cache.get(key);
6+
7+
const setCache = (key, data) => cache.set(key, data);
8+
9+
module.exports = {
10+
getCache,
11+
setCache
12+
};

utils/get-image-dimensions.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
11
const probe = require('probe-image-size');
22
const errorLogger = require('./error-logger');
3-
// Cache image dimensions we know will be repeated, like author profile and cover images
4-
const imageDimensionMap = {};
3+
const { getCache, setCache } = require('./cache');
4+
// // Cache image dimensions we know will be repeated, like author profile and cover images
5+
// const imageDimensionMap = {};
56
const defaultDimensions = { width: 600, height: 400 };
67

7-
const getImageDimensions = async (url, title, cache) => {
8+
const getImageDimensions = async (url, description) => {
89
try {
9-
if (cache && imageDimensionMap[url]) return imageDimensionMap[url];
10+
let imageDimensions = getCache(url);
11+
if (imageDimensions) return imageDimensions;
1012

1113
const res = await probe(url);
12-
const width = res?.width ? res?.width : defaultDimensions.width;
13-
const height = res?.height ? res?.height : defaultDimensions.height;
14-
15-
if (cache) {
16-
imageDimensionMap[url] = { width, height };
17-
}
18-
19-
return {
20-
width,
21-
height
14+
imageDimensions = {
15+
width: res?.width ? res?.width : defaultDimensions.width,
16+
height: res?.height ? res?.height : defaultDimensions.height
2217
};
18+
setCache(url, imageDimensions);
19+
20+
return imageDimensions;
2321
} catch (err) {
24-
if (err.statusCode) errorLogger({ type: 'image', name: title }); // Only write HTTP status code errors to log
22+
if (err.statusCode) errorLogger({ type: 'image', name: description }); // Only write HTTP status code errors to log
2523

26-
return {
27-
width: defaultDimensions.width,
28-
height: defaultDimensions.height
29-
};
24+
return defaultDimensions;
3025
}
3126
};
3227

utils/ghost/original-post-handler.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ const originalPostHandler = async post => {
4242
...originalPost.primary_author.image_dimensions
4343
};
4444
originalPost.primary_author.image_dimensions.profile_image =
45-
await getImageDimensions(originalPost.primary_author.profile_image);
45+
await getImageDimensions(
46+
originalPost.primary_author.profile_image,
47+
`Original author profile image: ${originalPost.primary_author.profile_image}`
48+
);
4649
}
4750

4851
// Add an `original_post` object to the current post

utils/ghost/process-batch.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ const processBatch = async ({
5858

5959
// Set the source of the publication and whether it's a page or post for tracking and later processing
6060
obj.source = 'Ghost';
61-
obj.contentType = contentType === 'posts' ? 'post' : 'page';
61+
const singularContentType = contentType.slice(0, -1);
62+
obj.contentType = singularContentType;
6263

6364
// Set a default feature image for posts if one doesn't exist
6465
if (contentType === 'posts' && !obj.feature_image)
@@ -70,7 +71,7 @@ const processBatch = async ({
7071
obj.image_dimensions = { ...obj.image_dimensions };
7172
obj.image_dimensions.feature_image = await getImageDimensions(
7273
obj.feature_image,
73-
obj.title
74+
`Ghost ${singularContentType} feature image: ${obj.title}`
7475
);
7576
}
7677

@@ -82,8 +83,7 @@ const processBatch = async ({
8283
obj.primary_author.image_dimensions.profile_image =
8384
await getImageDimensions(
8485
obj.primary_author.profile_image,
85-
obj.primary_author.name,
86-
true
86+
`Ghost author profile image: ${obj.primary_author.name}`
8787
);
8888
}
8989

@@ -94,8 +94,7 @@ const processBatch = async ({
9494
obj.primary_author.image_dimensions.cover_image =
9595
await getImageDimensions(
9696
obj.primary_author.cover_image,
97-
obj.primary_author.name,
98-
true
97+
`Ghost author cover image: ${obj.primary_author.name}`
9998
);
10099
}
101100

@@ -106,8 +105,7 @@ const processBatch = async ({
106105
tag.image_dimensions = { ...tag.image_dimensions };
107106
tag.image_dimensions.feature_image = await getImageDimensions(
108107
tag.feature_image,
109-
tag.name,
110-
true
108+
`Ghost tag feature image: ${tag.name}`
111109
);
112110
}
113111
})

utils/hashnode/process-batch.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ const processBatch = async ({
2424
newObj.title = oldObj.title;
2525
// Set the source of the publication and whether it's a page or post for tracking and later processing
2626
newObj.source = 'Hashnode';
27-
newObj.contentType = contentType === 'posts' ? 'post' : 'page';
27+
const singularContentType = contentType.slice(0, -1);
28+
newObj.contentType = singularContentType;
2829

2930
newObj.html = await modifyHTMLContent({
3031
postContent: oldObj.content.html,
@@ -56,7 +57,7 @@ const processBatch = async ({
5657
newObj.image_dimensions = {};
5758
newObj.image_dimensions.feature_image = await getImageDimensions(
5859
newObj.feature_image,
59-
newObj.title
60+
`Hashnode ${singularContentType} feature image: ${newObj.title}`
6061
);
6162

6263
const newObjAuthor = {};
@@ -88,8 +89,7 @@ const processBatch = async ({
8889
newObjAuthor.image_dimensions = {};
8990
newObjAuthor.image_dimensions.profile_image = await getImageDimensions(
9091
newObjAuthor.profile_image,
91-
newObjAuthor.name,
92-
true
92+
`Hashnode author profile image: ${newObjAuthor.name}`
9393
);
9494
}
9595
newObj.primary_author = newObjAuthor;

utils/modify-html-content.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ const modifyHTMLContent = async ({ postContent, postTitle, source }) => {
3838
images.map(async image => {
3939
// To do: swap out the image URLs here once we have them auto synced
4040
// with an S3 bucket
41-
const { width, height } = await getImageDimensions(image.src, postTitle);
41+
const { width, height } = await getImageDimensions(
42+
image.src,
43+
`Body image in ${postTitle}: ${image.src}`
44+
);
4245

4346
image.setAttribute('width', width);
4447
image.setAttribute('height', height);

0 commit comments

Comments
 (0)