Skip to content

Commit

Permalink
feat: use node-cache during build (#922)
Browse files Browse the repository at this point in the history
  • Loading branch information
scissorsneedfoodtoo authored Jul 24, 2024
1 parent 1e703b1 commit 06e2c12
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 35 deletions.
39 changes: 39 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"lint-staged": "15.2.7",
"lodash": "4.17.21",
"md5": "2.3.0",
"node-cache": "5.1.2",
"node-fetch": "2.7.0",
"npm-run-all2": "6.2.2",
"piscina": "4.6.1",
Expand Down
10 changes: 8 additions & 2 deletions src/_data/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ module.exports = async () => {
site.icon = iconUrl;

// Determine image dimensions before server runs for structured data
const logoDimensions = await getImageDimensions(logoUrl);
const coverImageDimensions = await getImageDimensions(coverImageUrl);
const logoDimensions = await getImageDimensions(
logoUrl,
`Site logo: ${logoUrl}`
);
const coverImageDimensions = await getImageDimensions(
coverImageUrl,
`Site cover image: ${coverImageUrl}`
);

site.image_dimensions = {
logo: {
Expand Down
12 changes: 12 additions & 0 deletions utils/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const NodeCache = require('node-cache');

const cache = new NodeCache();

const getCache = key => cache.get(key);

const setCache = (key, data) => cache.set(key, data);

module.exports = {
getCache,
setCache
};
33 changes: 14 additions & 19 deletions utils/get-image-dimensions.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
const probe = require('probe-image-size');
const errorLogger = require('./error-logger');
// Cache image dimensions we know will be repeated, like author profile and cover images
const imageDimensionMap = {};
const { getCache, setCache } = require('./cache');
// // Cache image dimensions we know will be repeated, like author profile and cover images
// const imageDimensionMap = {};
const defaultDimensions = { width: 600, height: 400 };

const getImageDimensions = async (url, title, cache) => {
const getImageDimensions = async (url, description) => {
try {
if (cache && imageDimensionMap[url]) return imageDimensionMap[url];
let imageDimensions = getCache(url);
if (imageDimensions) return imageDimensions;

const res = await probe(url);
const width = res?.width ? res?.width : defaultDimensions.width;
const height = res?.height ? res?.height : defaultDimensions.height;

if (cache) {
imageDimensionMap[url] = { width, height };
}

return {
width,
height
imageDimensions = {
width: res?.width ? res?.width : defaultDimensions.width,
height: res?.height ? res?.height : defaultDimensions.height
};
setCache(url, imageDimensions);

return imageDimensions;
} catch (err) {
if (err.statusCode) errorLogger({ type: 'image', name: title }); // Only write HTTP status code errors to log
if (err.statusCode) errorLogger({ type: 'image', name: description }); // Only write HTTP status code errors to log

return {
width: defaultDimensions.width,
height: defaultDimensions.height
};
return defaultDimensions;
}
};

Expand Down
5 changes: 4 additions & 1 deletion utils/ghost/original-post-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ const originalPostHandler = async post => {
...originalPost.primary_author.image_dimensions
};
originalPost.primary_author.image_dimensions.profile_image =
await getImageDimensions(originalPost.primary_author.profile_image);
await getImageDimensions(
originalPost.primary_author.profile_image,
`Original author profile image: ${originalPost.primary_author.profile_image}`
);
}

// Add an `original_post` object to the current post
Expand Down
14 changes: 6 additions & 8 deletions utils/ghost/process-batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ const processBatch = async ({

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

// Set a default feature image for posts if one doesn't exist
if (contentType === 'posts' && !obj.feature_image)
Expand All @@ -70,7 +71,7 @@ const processBatch = async ({
obj.image_dimensions = { ...obj.image_dimensions };
obj.image_dimensions.feature_image = await getImageDimensions(
obj.feature_image,
obj.title
`Ghost ${singularContentType} feature image: ${obj.title}`
);
}

Expand All @@ -82,8 +83,7 @@ const processBatch = async ({
obj.primary_author.image_dimensions.profile_image =
await getImageDimensions(
obj.primary_author.profile_image,
obj.primary_author.name,
true
`Ghost author profile image: ${obj.primary_author.name}`
);
}

Expand All @@ -94,8 +94,7 @@ const processBatch = async ({
obj.primary_author.image_dimensions.cover_image =
await getImageDimensions(
obj.primary_author.cover_image,
obj.primary_author.name,
true
`Ghost author cover image: ${obj.primary_author.name}`
);
}

Expand All @@ -106,8 +105,7 @@ const processBatch = async ({
tag.image_dimensions = { ...tag.image_dimensions };
tag.image_dimensions.feature_image = await getImageDimensions(
tag.feature_image,
tag.name,
true
`Ghost tag feature image: ${tag.name}`
);
}
})
Expand Down
8 changes: 4 additions & 4 deletions utils/hashnode/process-batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const processBatch = async ({
newObj.title = oldObj.title;
// Set the source of the publication and whether it's a page or post for tracking and later processing
newObj.source = 'Hashnode';
newObj.contentType = contentType === 'posts' ? 'post' : 'page';
const singularContentType = contentType.slice(0, -1);
newObj.contentType = singularContentType;

newObj.html = await modifyHTMLContent({
postContent: oldObj.content.html,
Expand Down Expand Up @@ -56,7 +57,7 @@ const processBatch = async ({
newObj.image_dimensions = {};
newObj.image_dimensions.feature_image = await getImageDimensions(
newObj.feature_image,
newObj.title
`Hashnode ${singularContentType} feature image: ${newObj.title}`
);

const newObjAuthor = {};
Expand Down Expand Up @@ -88,8 +89,7 @@ const processBatch = async ({
newObjAuthor.image_dimensions = {};
newObjAuthor.image_dimensions.profile_image = await getImageDimensions(
newObjAuthor.profile_image,
newObjAuthor.name,
true
`Hashnode author profile image: ${newObjAuthor.name}`
);
}
newObj.primary_author = newObjAuthor;
Expand Down
5 changes: 4 additions & 1 deletion utils/modify-html-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ const modifyHTMLContent = async ({ postContent, postTitle, source }) => {
images.map(async image => {
// To do: swap out the image URLs here once we have them auto synced
// with an S3 bucket
const { width, height } = await getImageDimensions(image.src, postTitle);
const { width, height } = await getImageDimensions(
image.src,
`Body image in ${postTitle}: ${image.src}`
);

image.setAttribute('width', width);
image.setAttribute('height', height);
Expand Down

0 comments on commit 06e2c12

Please sign in to comment.