Gatsby - build caching and image transformations

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

2 Likes