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: Adding Images to a WordPress Site | Gatsby

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

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?

is there an update on this?

Hi @mat_jack1, we‘d like exactly the same, serving all assets locally without the need of a run-time dependency on datocms-assets.com.

Can we achieve this somehow already?

we know about this, and we are going to work together with Gatsby team to add this in our source plugin. We are probably going to revive this PR soon: feat: add Gatsby image cdn resolver by TylerBarnes · Pull Request #189 · datocms/gatsby-source-datocms · GitHub it wasn’t the right moment, but I think now it’s better time!