Skip to content
This repository has been archived by the owner on Jun 15, 2021. It is now read-only.

Commit

Permalink
add cachePublic option to disable caching of public directory by defa…
Browse files Browse the repository at this point in the history
…ult. May fix #15
  • Loading branch information
axe312ger committed May 10, 2019
1 parent 89da4ff commit 1253a13
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,37 @@ These folders are cached by default:

## ⚙️ Configuration

### `cachePublic` - default: `false`

Caching the public directory on long term can result in a huge directory size which can break your netlify build. Enable this with caution.

```js
plugins: [
{
resolve: "gatsby-plugin-netlify-cache",
options: {
cachePublic: true
}
}
]
```

### `extraDirsToCache` - default: `[]`

If you need additionals directories to be cached, you can use the option `extraDirsToCache` to include one or multiple directories to Netlify cache:

```js
plugins: [
{
resolve: "gatsby-plugin-netlify-cache",
options: {
extraDirsToCache: ["extraDir", ".extraDotDir", "extra/dir"],
},
},
extraDirsToCache: [
"extraDir",
".extraDotDir",
"extra/dir"
]
}
}
]
```

Expand Down
23 changes: 17 additions & 6 deletions src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ const { resolve, relative } = require(`path`)

const { ensureDir, readdir, copy } = require(`fs-extra`)

async function calculateDirs(store, { extraDirsToCache = [] }) {
async function calculateDirs(
store,
{ extraDirsToCache = [], cachePublic = false }
) {
const program = store.getState().program
const rootDirectory = program.directory

const dirsToCache = [
resolve(rootDirectory, `public`),
cachePublic && resolve(rootDirectory, `public`),
resolve(rootDirectory, `.cache`),
...extraDirsToCache.map(dirToCache => resolve(rootDirectory, dirToCache))
].filter(Boolean)
Expand Down Expand Up @@ -39,15 +42,19 @@ function generateCacheDirectoryNames(rootDirectory, netlifyCacheDir, dirPath) {
return { cachePath, humanName }
}

exports.onPreInit = async function({ store }, { extraDirsToCache }) {
exports.onPreInit = async function(
{ store },
{ extraDirsToCache, cachePublic }
) {
if (!process.env.NETLIFY_BUILD_BASE) {
return
}

const { dirsToCache, netlifyCacheDir, rootDirectory } = await calculateDirs(
store,
{
extraDirsToCache
extraDirsToCache,
cachePublic
}
)

Expand Down Expand Up @@ -77,15 +84,19 @@ exports.onPreInit = async function({ store }, { extraDirsToCache }) {
console.log(`plugin-netlify-cache: Netlify cache restored`)
}

exports.onPostBuild = async function({ store }, { extraDirsToCache }) {
exports.onPostBuild = async function(
{ store },
{ extraDirsToCache, cachePublic }
) {
if (!process.env.NETLIFY_BUILD_BASE) {
return
}

const { dirsToCache, netlifyCacheDir, rootDirectory } = await calculateDirs(
store,
{
extraDirsToCache
extraDirsToCache,
cachePublic
}
)

Expand Down

0 comments on commit 1253a13

Please sign in to comment.