Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-theme-bodiless): Properly prefix paths for bodiless gatsby… #1502

Merged
merged 2 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 17 additions & 5 deletions packages/gatsby-theme-bodiless/create-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,17 @@ const fixed = async ({
};
};

const generateGatsbyImage = async ({ file, preset, reporter }, options) => {
const generateGatsbyImage = async ({
file, preset, reporter, pathPrefix
}, options) => {
console.log('pp', pathPrefix);
wodenx marked this conversation as resolved.
Show resolved Hide resolved
// skip image generation when unknown preset is passed
if (!Object.values(GatsbyImagePresets).includes(preset)) {
return undefined;
}
const { sharpArgs } = options || {};
sharpArgs.pathPrefix = pathPrefix;

switch (preset) {
case GatsbyImagePresets.Fixed:
return fixed({
Expand Down Expand Up @@ -394,7 +399,7 @@ const generateGatsbyImage = async ({ file, preset, reporter }, options) => {
* leveraging logic from gatsby-source-filesystem
* https://github.com/gatsbyjs/gatsby/blob/39baf4eb504dcbb4d231f4baf8b109d0dcabb1da/packages/gatsby-source-filesystem/src/extend-file-node.js
*/
const copyFileToStatic = (node, reporter) => {
const copyFileToStatic = (node, reporter, pathPrefix = '') => {
const fileAbsolutePath = node.absolutePath;
const fileName = `${node.internal.contentDigest}/${pathUtil.basename(fileAbsolutePath)}`;

Expand Down Expand Up @@ -425,7 +430,7 @@ const copyFileToStatic = (node, reporter) => {
);
}

return `/static/${fileName}`;
return `${pathPrefix}/static/${fileName}`;
};

const createImageNode = ({ node, content }) => {
Expand Down Expand Up @@ -461,12 +466,15 @@ const createImageNode = ({ node, content }) => {
return imageNode;
};

const generateImages = async ({ imageNode, content, reporter }, options) => {
const generateImages = async ({
imageNode, content, reporter, pathPrefix
}, options) => {
const parsedContent = JSON.parse(content);
return generateGatsbyImage({
file: imageNode,
preset: parsedContent.preset,
reporter,
pathPrefix,
}, options);
};

Expand All @@ -475,6 +483,7 @@ const createBodilessNode = async ({
actions,
loadNodeContent,
reporter,
pathPrefix,
}, pluginOptions) => {
const nodeContent = await loadNodeContent(node);
const { createNode, createParentChildLink } = actions;
Expand All @@ -485,14 +494,15 @@ const createBodilessNode = async ({
if (imageNode !== undefined) {
const publicUrl = pathUtil.isAbsolute(imageNode.path)
? imageNode.path
: copyFileToStatic(imageNode, reporter);
: copyFileToStatic(imageNode, reporter, pathPrefix);
let gatsbyImgData;
// skip gatsby img data generation when an image from json does not exist in filesystem
if (fse.existsSync(imageNode.absolutePath)) {
gatsbyImgData = await generateImages({
imageNode,
content: nodeContent,
reporter,
pathPrefix,
}, gatsbyImageOptions);
}

Expand Down Expand Up @@ -535,6 +545,7 @@ exports.onCreateNode = ({
actions,
loadNodeContent,
reporter,
pathPrefix,
}, pluginOptions) => {
// Add slug field to Bodiless node
if (node.internal.type === BODILESS_NODE_TYPE) {
Expand All @@ -553,6 +564,7 @@ exports.onCreateNode = ({
actions,
loadNodeContent,
reporter,
pathPrefix,
}, pluginOptions);
}
};
Expand Down
1 change: 1 addition & 0 deletions sites/__cxstarter__/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,6 @@ module.exports = {
flags: {
DEV_SSR: false,
},
pathPrefix: process.env.GATSBY_PATH_PREFIX,
wodenx marked this conversation as resolved.
Show resolved Hide resolved
plugins,
};