Gatsby - build caching and image transformations

I’ve just started using Render and it seems great! I have an issue with my Gatsby static site - i’m using gatsby-image to transform many images, and it takes a very long time. I’ve read cached images wouldn’t need to be regenerated if the .cache and public directories remained intact and when the GATSBY_EXPERIMENTAL_PAGE_BUILD_ON_DATA_CHANGES environment variable is set to true, but i’m getting a message:

We've detected that the Gatsby cache is incomplete (the .cache directory exists but the public directory does not). As a precaution, we're deleting your site's cache to ensure there's no stale data.

Is build caching in Gatsby in this way possible using Render?

Hello,

Sorry for the delay in responding. Render starts each build in a fresh environment, so there is no public dir present in the new build. A workaround would be to move the public dir out of the project dir so it will be added to the render cache, and then copy back to the project root on each new build. Note though, that the reason we don’t persist thinks like public is to ensure if you remove a file, it doesn’t get copied to the new build. At worst, you’d have to “Clear build cache & deploy” to remove the old public dir. Here is an example script:

#!/usr/bin/env bash

build_with_cache() {
  if [[ -d "$XDG_CACHE_HOME"/public ]]; then
    echo "Copying cached public dir"
    rsync -a "$XDG_CACHE_HOME"/public/ public
  else
    echo "No cached public dir found"
  fi

  echo "Building"

  gatsby build

  echo "Done, caching public dir"
  rsync -a public/ "$XDG_CACHE_HOME"/public
}

if [[ "$RENDER" ]]; then
  build_with_cache
else
  gatsby build
fi

Be sure to chmod u+x the file, and then replace the build command with ./cache.sh (for example if you name the script cache.sh

3 Likes

Thanks for this @Ralph ! Would the Render team be willing to add this to their docs as a fully-fledged guide?

Hi @karlhorky-upleveled ,

I think that’s a great idea. We’ve got a lot of docs we want to add, so I can’t add it right now, but I’ve added this to our list :slight_smile:

1 Like