Skip to content

Commit 8fa73cd

Browse files
committed
Change code to generate og images using file system
1 parent 594ae74 commit 8fa73cd

File tree

7 files changed

+2483
-93
lines changed

7 files changed

+2483
-93
lines changed

gatsby-node.js

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,7 @@
11
const { generateOgImages } = require("./src/generator");
22
const { config } = require("./src/config");
3-
const { imageGenerationJobCache } = require("./src/cache");
43

5-
exports.onPreInit = async (pluginConfig) => {
6-
config.init(pluginConfig);
7-
};
8-
9-
exports.onPreBootstrap = async ({ cache }) => {
10-
await imageGenerationJobCache.init(cache);
11-
};
12-
13-
exports.onCreatePage = async ({ page, cache }) => {
14-
const { componentGenerationDir } = config.getConfig()
15-
if (page.path.startsWith(`/${componentGenerationDir}/`)) {
16-
await imageGenerationJobCache.add(cache, page.context.ogImage["__ogImageGenerationContext"]);
17-
}
18-
};
19-
20-
exports.onPostBuild = async ({ cache }) => {
21-
const jobDefinitions = await imageGenerationJobCache.getAll(cache);
22-
await generateOgImages(jobDefinitions);
4+
exports.onPostBuild = async () => {
5+
const currentConfig = config.getConfig();
6+
await generateOgImages(currentConfig);
237
};

index.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,24 @@ const { join } = require("path");
22
const { config } = require("./src/config");
33

44
exports.createOpenGraphImage = (createPage, options) => {
5-
config.init(options)
6-
const { defaultSize, componentGenerationDir, defaultWaitCondition } = config.getConfig();
7-
const { path, component, context } = options;
5+
config.init(options);
86

9-
const size = { ...defaultSize, ...(options.size || {}) };
10-
const waitCondition = options.waitCondition ? options.waitCondition : defaultWaitCondition;
11-
const componentPath = join(componentGenerationDir, encodeURIComponent(path.split("/").join("")));
12-
const imgPath = join("public", path);
7+
const { size, componentGenerationDir } = config.getConfig();
8+
const { component, context } = options;
139

14-
const generationContext = { componentPath, imgPath, size, waitCondition };
15-
const ogImageMetaData = { path, size, __ogImageGenerationContext: generationContext };
10+
const componentPath = `${componentGenerationDir}/${context.id}`;
11+
const imagePath = join("public", componentGenerationDir, `${context.id}.png`);
12+
13+
const ogImageMetadata = { componentPath, imagePath, size };
1614

1715
createPage({
1816
path: componentPath,
19-
component: component,
17+
component,
2018
context: {
2119
...context,
22-
ogImage: ogImageMetaData,
20+
ogImage: ogImageMetadata,
2321
},
2422
});
2523

26-
return ogImageMetaData;
24+
return ogImageMetadata;
2725
};

0 commit comments

Comments
 (0)