-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Responsive Images (remote) #2902
Comments
I don't know much about the Prismic plugin, but take a look at the Contentful one. I believe that it uses Gatsby's responsive image solution to generate the srcset, but the resulting URLs are served from Contentful's image delivery using its image manipulation query string parameters. |
Contentful is a bit unique in that they have an Image API. So what the gatsby-source-contentful plugin does is create a GraphQL type for images with the same API as gatsby-transformer-sharp so you can write queries the same way but not have to do the image processing locally like with sharp. Assuming Prismic doesn't have an image API like contentful, you'll want to do your first option, add a PR to gatsby-source-prismic to download images locally. This is pretty easy to do — gatsby-source-filesystem exposes a helper function which does all the hard work. See for example how gatsby-source-wordpress does things
|
Thanks for the quick response and the links. Prismic has no image API so i'll look at implementing something like you suggest. Before I look at adding this to the source plugin, I should be able to achieve the same in my local But that said, how would I debug this? I.e. normally I simply run |
Yup! You can use the same createRemoteFileNode function in gatsby-node.js and onCreateNode to download URLs and create links to them with createNodeField. This is pretty easy to debug with console.log/breakpoints in your API implementations. |
This is great! I didn't realise there was already support for pulling remote images in I've just tried @KyleAMathews suggestion above (with This is my const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
exports.onCreateNode = ({ node, boundActionCreators, store, cache }) => {
const { createNodeField, createNode } = boundActionCreators
if (node.internal.type === `ContentfulAsset`) {
createRemoteFileNode({
url: 'http:' + node.file.url,
store,
cache,
createNode
}).then((result) => {
console.log(result)
})
}
} This is working as I can see the newly generated File nodes in GraphQL and the remote images are being pulled down to my machine, but I'm not quite understanding how to "create links to them with createNodeField". How do I make these File nodes as a subset of the |
You'll want to add a link to the new file node using createNodeField({
node,
name: `linkToFile___NODE`,
value: remoteFile.id,
})
// The field value is now accessible at node.fields.linkToFile The important part is the name of the new field has the |
Thanks for the quick reply This is my exports.onCreateNode = ({ node, boundActionCreators, store, cache }) => {
const { createNodeField, createNode } = boundActionCreators
if (node.internal.type === `ContentfulAsset`) {
return new Promise((resolve, reject) => {
createRemoteFileNode({
url: 'http:' + node.file.url,
store,
cache,
createNode
}).then(result => {
console.log(result.id)
createNodeField({
node,
name: `linkToFile___NODE`,
value: result.id
})
resolve()
}).catch(reject)
})
}
} but am getting this error:
The
which appears to be right. Am I using |
Check what the value of node is when you create the node field. |
Running { id: 'c3yW5oX7VLWOQw0kUoeawKY',
parent: null,
children: [],
file:
{ url: '//images.contentful.com/hmylqvbzvbxb/3yW5oX7VLWOQw0kUoeawKY/e722bd7caeb2ced91760497f9ee08466/20170715-DSCF2224-Edit-cropped.jpg',
details: { size: 1944471, image: [Object] },
fileName: '20170715-DSCF2224-Edit-cropped.jpg',
contentType: 'image/jpeg' },
title: 'Portrait',
description: '',
node_locale: 'en-AU',
internal:
{ type: 'ContentfulAsset',
contentDigest: '007eea3e43ba39629d71d11f0379548d',
owner: 'gatsby-source-contentful' }
} |
The error suggests you're somehow adding the field to a random other node. |
@elliotschultz did you fix this one? I'm also getting
when trying to create a link between nodes. |
Also getting the same error:
|
Did someone succeeded implementing const crypto = require('crypto')
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
const createContentDigest = obj =>
crypto
.createHash(`md5`)
.update(JSON.stringify(obj))
.digest(`hex`)
exports.onCreateNode = async ({ node, boundActionCreators, store, cache }) => {
const {
createNode,
createNodeField,
createParentChildLink,
} = boundActionCreators
if (node.internal.type !== `Asset`) {
return
}
const localImageNode = await createLocalImageNode({
url: 'https://static.pexels.com/photos/248797/pexels-photo-248797.jpeg',
parent: node.id,
store,
cache,
createNode,
})
createParentChildLink({
parent: node,
child: localImageNode,
})
}
const createLocalImageNode = async ({
url,
parent,
store,
cache,
createNode,
}) => {
const fileNode = await createRemoteFileNode({
url,
store,
cache,
createNode,
})
const localImageNode = {
id: `${parent} >>> LocalImage`,
url,
// expires: screenshotResponse.data.expires,
parent,
children: [],
internal: {
type: `LocalImage`,
},
localImageFile___NODE: fileNode.id,
}
localImageNode.internal.contentDigest = createContentDigest(localImageNode)
createNode(localImageNode)
return localImageNode
} The pain is it make kilometric queries query{
asset {
childLocalImage {
localImageFile {
childImageSharp {
sizes {
src
}
}
}
}
}
} |
@MarcCoet Awesome, worked for me. |
Due to the high volume of issues, we're closing out older ones without recent activity. Please open a new issue if you need help! |
Hi, first up great work. I've just completed my first site using Gatsby and it's awesome!
My first time using React and GraphQL and I've enjoyed it (normally use Angular1/Angular2).
My site is backed by the prismic headless CMS and uses this source plugin to get the data.
I'd now like to make the images responsive.
I'm aware that Gatsby has a responsive image plugin but it appears to work only with local images and mine are hosted remotely in the prismic CMS. In theory it would be possible to add multiple images in prismic and infer what to display where based on the data returned. But I'd rather this was all handled automagically and only one image needed to be added to prismic.
Question. Is there a way to handle remote images with the existing plugin - have I missed something?
I'm assuming not, in which case would the best thing be to:
The text was updated successfully, but these errors were encountered: