Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Cache images #919

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion gridsome.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Changes here require a server restart.
// To restart press CTRL + C in terminal and run `gridsome develop`

const fs = require("fs");
const fs = require("fs-extra");
const path = require("path");
const dayjs = require("dayjs");
const { imageType } = require("gridsome/lib/graphql/types/image");
Expand Down Expand Up @@ -275,6 +275,13 @@ module.exports = function (api) {
});
});

api.beforeBuild(async () => {
// Stage in cached images if they exist.
console.log("Stage in cached images from imageCacheDir")
console.log(`Moving from '${api.config.imageCacheDir}/ to '${api.config.imagesDir}'`)
moveFiles(`${api.config.imageCacheDir}/`, api.config.imagesDir);
});

api.afterBuild(async () => {
// Write all Platforms to /use/feed.json.
let outDir = path.join(__dirname, "dist", "use");
Expand All @@ -283,9 +290,29 @@ module.exports = function (api) {
fs.writeFile(feedPath, makePlatformsJson(platformsData), (error) => {
if (error) throw error;
});
// Copy out compiled images.
console.log("Cache images from build to imageCacheDir")
console.log(`Copying from '${api.config.imagesDir}' to '${api.config.imageCacheDir}/'`)
copyFiles(api.config.imagesDir, `${api.config.imageCacheDir}/`);
});
};

async function copyFiles(source, dest) {
try {
await fs.copy(source, dest);
} catch (err) {
console.error(err);
}
}

async function moveFiles(source, dest) {
try {
await fs.move(source, dest, {overwrite: true});
} catch (err) {
console.error(err);
}
}

function makePlatformsJson(platformsData) {
let platforms = platformsData.data.platforms.edges.map((edge) => {
// Massage fields a little for backward compatibility.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"bootstrap-vue": "^2.21.2",
"commander": "^8.2.0",
"dayjs": "^1.10.7",
"fs-extra": "^10.0.0",
"gridsome": "^0.7.0",
"jiti": "^1.12.0",
"node-watch": "^0.7.1",
Expand Down
9 changes: 9 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5269,6 +5269,15 @@ fs-constants@^1.0.0:
resolved "https://registry.yarnpkg.com/fs-constants/-/fs-constants-1.0.0.tgz#6be0de9be998ce16af8afc24497b9ee9b7ccd9ad"
integrity sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow==

fs-extra@^10.0.0:
version "10.0.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-10.0.0.tgz#9ff61b655dde53fb34a82df84bb214ce802e17c1"
integrity sha512-C5owb14u9eJwizKGdchcDUQeFtlSHHthBk8pbX9Vc1PFZrLombudjDnNns88aYslCyF6IY5SUw3Roz6xShcEIQ==
dependencies:
graceful-fs "^4.2.0"
jsonfile "^6.0.1"
universalify "^2.0.0"

fs-extra@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-6.0.1.tgz#8abc128f7946e310135ddc93b98bddb410e7a34b"
Expand Down