Skip to content

Commit 360194e

Browse files
committed
feat(plugin): pull over cache code from gatsby-cache plugin
1 parent ea4a065 commit 360194e

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

src/index.js

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
const path = require('path');
2+
3+
const getCacheDirs = (PUBLISH_DIR) => [
4+
PUBLISH_DIR,
5+
path.normalize(`${PUBLISH_DIR}/../.cache`),
6+
];
7+
18
// This is the main file for the Netlify Build plugin {{name}}.
29
// Please read the comments to learn more about the Netlify Build plugin syntax.
310
// Find more information in the Netlify documentation.
@@ -71,8 +78,29 @@ module.exports = {
7178
},
7279
}) {
7380
try {
74-
// Commands are printed in Netlify logs
75-
await run('echo', ['Hello world!\n'])
81+
// print a helpful message if the publish dir is misconfigured
82+
if (process.cwd() === PUBLISH_DIR) {
83+
build.failBuild(
84+
`Gatsby sites must publish the public directory, but your site’s publish directory is set to “${PUBLISH_DIR}”. Please set your publish directory to your Gatsby site’s public directory.`,
85+
);
86+
}
87+
88+
const cacheDirs = getCacheDirs(PUBLISH_DIR);
89+
90+
if (await cache.restore(cacheDirs)) {
91+
console.log('Found a Gatsby cache. We’re about to go FAST. ⚡️');
92+
} else {
93+
console.log('No Gatsby cache found. Building fresh.');
94+
}
95+
96+
// check gatsby for gatsby-plugin-netlify - log warning WHEN NOT
97+
98+
// copying netlify wrapper functions into function dir
99+
100+
// copy compiled gatsby functions into function dir
101+
102+
// add redirect
103+
76104
} catch (error) {
77105
// Report a user error
78106
build.failBuild('Error message', { error })
@@ -93,8 +121,19 @@ module.exports = {
93121
onPreBuild() {},
94122
// Build commands are executed
95123
onBuild() {},
124+
*/
125+
96126
// After Build commands are executed
97-
onPostBuild() {},
127+
onPostBuild({ constants: { PUBLISH_DIR }, utils: { cache } }) {
128+
const cacheDirs = getCacheDirs(PUBLISH_DIR);
129+
130+
if (await cache.save(cacheDirs)) {
131+
console.log('Stored the Gatsby cache to speed up future builds.');
132+
} else {
133+
console.log('No Gatsby build found.');
134+
}
135+
},
136+
/*
98137
// Runs on build success
99138
onSuccess() {},
100139
// Runs on build error

0 commit comments

Comments
 (0)