jlengstorf / netlify-plugin-gatsby-cache

185 stars 21 forks source link

feat: switch to new cache utils #3

Closed jlengstorf closed 4 years ago

jlengstorf commented 4 years ago

@ehmicky can you take a look at this and tell me what I'm doing wrong? As far as I can tell, I'm pointing to the right place, but I can't seem to get the cache util to find or save/restore directories.

Am I missing something obvious here?

log with output from the various console logs I've put in: https://app.netlify.com/sites/learnwithjason/deploys/5e3a43e79ad3d2000830e7e9

jlengstorf commented 4 years ago

if you want to try this, I've published it as netlify-plugin-gatsby-cache@test-cache-utils

DavidWells commented 4 years ago

It looks like this isn't working because the fs.existsSync is checking the build dir instead of where the cache lives.

Try this instead with cache.has


module.exports = () => {
  return {
    name: 'netlify-plugin-cache-test',
    async onPreBuild({ netlifyConfig, utils }) {
      const { cache } = utils
      const filePath = `${netlifyConfig.build.publish}/test-dir`

      const hasCache = await cache.has([ filePath ])

      if (hasCache) {
        console.log(`Found cache`)
        console.log(`Restore cache to ${filePath}`)
        await cache.restore([ filePath ])
        console.log('Loaded a previous cache. ⚡️')
      } else {
        console.log('No cache found. Do expensive thing')
      }
    },
    async onPostBuild({ netlifyConfig, utils }) {
      const { cache } = utils
      const filePath = `${netlifyConfig.build.publish}/test-dir`

      // Save the files to cache
      const hasItSaved = await cache.save([ filePath ])
      if (hasItSaved) {
        console.log('Cache saved for next run')
      } else {
        console.log('No changes detected, skipping save to speed things up')
      }

      // Verify the files are there in cache
      if (await cache.has(filePath)) {
        console.log(`Found cache ${filePath}`)
      } else {
        console.log('No cache dir!')
      }
    },
  }
}
ehmicky commented 4 years ago

The PR looks good to me. However it is not working as @jlengstorf points out due to a bug at https://github.com/netlify/build/issues/732

ehmicky commented 4 years ago

After fixing the bug above, I just tried this PR again against "Learn with Jason", and it correctly works. This looks good to merge, except for one debugging statement which might have been forgotten (see above comment). Great to see how much code is being simplified with the utility!

@jlengstorf Please let me know if there are any other things that don't work with the cache utility, or any DX or API points that could be improved :)

jlengstorf commented 4 years ago

@ehmicky it doesn't appear to be working on my repo — check #2 for log links

jlengstorf commented 4 years ago

disconnecting/reconnecting the repo seems to have fixed this, so... no idea what caused the problem, but it seems to be unrelated to this plugin