Veselin Stoyanov
Veselin Stoyanov

Reputation: 106

Gatsby .cache folder in AWS Amplify Console

Recently I built a Gatsby project and deployed it on AWS Amplify using the Amplify Console and connecting my Bitbucket repo. The project grew and now it has to build 37 pages with a ton of images. I am using ‘gatsby-source-custom-api’ for the data extraction which offers image processing using ‘gatsby-image’. It also offers a way to specify if the images was changed therefore if not changed it uses the .cache to get it already optimized by the ‘gatsby-image’. Now the issue I face is that the .cache folder is not available between builds in AWS Amplify. Can it be stored somewhere or is there another way to use it? Now it takes a good 13 minutes to build and the major part is from image processing.

Upvotes: 3

Views: 1197

Answers (2)

Kyle Boss
Kyle Boss

Reputation: 226

Try the following:

buildspec.yml

version: 0.2

phases:
  pre_build:
    commands:
      - mkdir -p /build-directory
      - cp -a ${CODEBUILD_SRC_DIR}/. /build-directory
      - cd /build-directory
      - yarn
  build:
    commands:
      - cd /build-directory
      - gatsby build
  post_build:
    commands:
      - cd /build-directory
      - cp -a /build-directory/. ${CODEBUILD_SRC_DIR}
cache:
  paths:
    - 'node_modules/**/*'
    - 'public/**/*'
    - '.cache/**/*'

As of this writing, there is not a tidy solution. Caching the entire .cache directory will ultimately throw the following error:

Failed to process image /codebuild/output/src***/src/project/.cache/gatsby-source-filesystem//.png Input file is missing

As you mentioned in the comments, Gatsby assumes that the absolute path to .cache is consistent between builds. This is not very compatible with CodeBuild since the build directory changes for each build. Because of this, Gatsby isn't able to locate the cached files and throws an error.

For this reason, creating a consistent path and bringing the artifacts there will solve the issue. When the build is done, be sure to bring the new artifacts to the CODEBUILD_SRC_DIR so that they can be cached for next time!

Upvotes: 1

Nikhil
Nikhil

Reputation: 753

You can store the .cache folder by defining it as part of the buildspec: https://docs.aws.amazon.com/amplify/latest/userguide/build-settings.html#yml-specification-syntax

Upvotes: 0

Related Questions