Pulling assets (images) locally

Hi

I would like to pull images on build so that I can embed the static assets in my page markup, instead of making external calls to DatoCMS each time.

For me, retrieving content from an external network on the frontend has some security issues which we are a pain to unblock.

Is this currently possible?

Example: https://www.gatsbyjs.org/tutorial/wordpress-image-tutorial/#creating-graphql-queries-that-pull-in-images-from-wordpress

hey @endymion1818 I think we would need an addition to our source plugin, similar to what the Contentful source plugin is doing here: https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-source-contentful/src/download-contentful-assets.js

We are open for Pull Requests on this of course! Otherwise we’ll note this down and add it to our feature requests, unfortunately we cannot work on it right now :frowning:

No worries Mattias, thanks for the reply!

1 Like

Hi,

I was looking into the Contentful source plugin, trying to replicate what they do with downloading local assets. However, I ran into an issue where createNodeField does not create a field. There’s a workaround using createResolvers (see github issue below).

Given that createNodeField does not work, how exactly would you suggest I approach this feature? Is createResolvers the way to go? Another question: I like the fact that the Contentful source plugin caches the downloaded files (to avoid downloading the asset again). How exactly should I approach this when working with createResolvers?

Thanks!

Hello @gordan.ratkovic and welcome!

And yes, I think createResolvers is the right place for you to hook into, have a look at the Gatsby docs: https://github.com/datocms/gatsby-source-datocms#field-customisations

It’s not a workaround, I think it’s the officially way for doing that, so you should be safe! :slight_smile:

In createResolvers you should already have access to everything you get from Dato, so you should be able to dowload the pictures and add a field to the record with the locally saved picture. Does that make sense?