diff --git a/cache-me.js b/cache-me.js new file mode 100644 index 0000000..59cfeab --- /dev/null +++ b/cache-me.js @@ -0,0 +1,52 @@ + +const fs = require('fs-extra') +const path = require('path') +const Rsync = require('rsync') + +const isProduction = process.env.NODE_ENV === 'PRODUCTION' + +const CACHE_PATH = isProduction + ? path.join('/', 'opt', 'build', 'cache', 'nuxt_build') // Netlify cache path + : path.resolve(__dirname, '.nuxt_build') + +const BUILD_PATH = path.resolve(__dirname, 'dist') + +const rsync = new Rsync() + .shell('ssh') + .flags('azq') + .source(BUILD_PATH + '/') + .destination(CACHE_PATH) + +async function cacheFinalFiles() { + try { + await fs.copy(BUILD_PATH, CACHE_PATH) + await fs.move(path.resolve(__dirname, '.nuxt'), path.join(CACHE_PATH, '.nuxt')) + } catch (e) { + console.log(e) + } +} + +async function cacheAndCopy() { + try { + await fs.ensureDir(CACHE_PATH) + rsync.execute(async function(error, code, cmd) { + if (!error) { + await fs.copy(CACHE_PATH, BUILD_PATH) + await cacheFinalFiles() + console.log('Please tell me you are well cached.') + } else console.error('error') + }) + } catch (err) { + // handle error + console.log('cache and copy ') + console.log(err) + } +} + +async function putCacheBack() { + +} + +module.exports = { + cacheAndCopy +} diff --git a/move-cache-back.js b/move-cache-back.js new file mode 100644 index 0000000..be08dce --- /dev/null +++ b/move-cache-back.js @@ -0,0 +1,48 @@ +const fs = require('fs-extra') +const path = require('path') +const { exec } = require("child_process") + +const isProduction = process.env.NODE_ENV === 'PRODUCTION' + +const CACHE_PATH = isProduction + ? path.join('/', 'opt', 'build', 'cache', 'nuxt_build') // Netlify cache path + : path.resolve(__dirname, '.nuxt_build') + +const BUILD_PATH = path.resolve(__dirname, '.nuxt') + +async function putNuxtClientBack() { + const exists = await fs.pathExists(CACHE_PATH) + if (exists) { + console.log("cache found") + await fs.copy(CACHE_PATH, BUILD_PATH) + exec('nuxt generate --no-build', (error, stdout, stderr) => { + if (error) { + console.log(`error: ${error.message}`); + return; + } + if (stderr) { + console.log(`stderr: ${stderr}`); + return; + } + console.log(`stdout: ${stdout}`); + }); + } else { + console.log("no cache") + exec('npm run generate', (error, stdout, stderr) => { + if (error) { + console.log(`error: ${error.message}`); + return; + } + if (stderr) { + console.log(`stderr: ${stderr}`); + return; + } + console.log(`stdout: ${stdout}`); + }); + } +} + +;(async () => { + await putNuxtClientBack() + +})() diff --git a/nuxt.config.js b/nuxt.config.js index 66eaf5b..2c05644 100644 --- a/nuxt.config.js +++ b/nuxt.config.js @@ -2,6 +2,7 @@ import { promises as fs } from 'fs' import pkg from './package' +const { cacheAndCopy } = require('./cache-me.js') export default { target: 'static', @@ -19,9 +20,14 @@ export default { // build: { // before: storeAppLists // }, - // generate: { - // before: storeAppLists - // } + generate: { + // before: storeAppLists + async done() { + console.log('Starting Netlify Cache') + await cacheAndCopy() + console.log('Finished Netlify Cache') + } + } }, generate: { @@ -32,11 +38,74 @@ export default { 'assets' ] }, - routes() { - return fs.readFile('./static/nuxt-endpoints.json', 'utf-8') + async routes () { + + const additionalRoutes = [] + const oldRoutesJsonPath = './dist/nuxt-endpoints.json' + + const noOldRoutesFound = await fs.stat( './dist/index.html' ) + .then(stats => { + console.log( 'stats', stats ) + return false + }) + .catch(() => true) + + const freshRoutesList = await fs.readFile('./static/nuxt-endpoints.json', 'utf-8') .then( endpointsJson => { return JSON.parse(endpointsJson) }) + + if ( noOldRoutesFound ) { + console.log('No old routes found: Building all routes') + return freshRoutesList + } + console.log('Old routes found') + + // Get routes from previous build + const oldRoutesList = await fs.readFile(oldRoutesJsonPath, 'utf-8') + .then( endpointsJson => { + return JSON.parse(endpointsJson) + }) + + // if there are more old routes + // then rebuild from scratch + if ( oldRoutesList.length > freshRoutesList.length ) { + console.log('More old routes than current ones: Building all routes') + return freshRoutesList + } + console.log('oldRoutesList.length', oldRoutesList.length) + console.log('freshRoutesList.length', freshRoutesList.length) + + // Store old routes into set + // so we can quickly compare them + const oldRoutesSet = new Map( oldRoutesList.map( endpoint => [ endpoint.route, endpoint.payload ] ) ) + + // Look through fresh routes and store any that are new + for ( const routeFromFreshList of freshRoutesList ) { + // If the route in not in old routes + // then regenerate this route + if ( !oldRoutesSet.has( routeFromFreshList.route ) || JSON.stringify(oldRoutesSet.get( routeFromFreshList.route )) !== JSON.stringify(routeFromFreshList.payload) ) { + console.log( 'Found new route', routeFromFreshList.route ) + additionalRoutes.push( routeFromFreshList ) + + continue + } + + const payloadChanged = JSON.stringify(oldRoutesSet.get( routeFromFreshList.route )) !== JSON.stringify(routeFromFreshList.payload) + + // If the route payload changed + // then regenerate this route + if ( payloadChanged ) { + console.log( 'Route payload changed', routeFromFreshList.route ) + additionalRoutes.push( routeFromFreshList ) + + continue + } + } + + console.log( 'additionalRoutes', additionalRoutes.length ) + + return additionalRoutes } }, @@ -149,7 +218,7 @@ export default { ** Build configuration */ build: { - parallel: true, + // parallel: true, // hardSource: true, cache: true, html: { diff --git a/package.json b/package.json index fef4287..ee90ef4 100644 --- a/package.json +++ b/package.json @@ -8,9 +8,10 @@ "dev": "nuxt", "build": "nuxt build", "start": "nuxt start", - "generate": "npm run clone-readme && npm run build-lists && npm run generate-nuxt && npm run generate-eleventy", + "generate": "npm run clone-readme && npm run build-lists && npm run generate-nuxt-incremental && npm run generate-eleventy", "build-lists": "node -r esm build-lists.js", "generate-nuxt": "nuxt generate", + "generate-nuxt-incremental": "node move-cache-back.js", "generate-eleventy": "npm run generate-postcss && node -r esm node_modules/.bin/eleventy --quiet", "generate-postcss": "ENV=production postcss assets/css/tailwind.css --o static/tailwind.css", "dev-eleventy": "node -r esm node_modules/.bin/eleventy --quiet --watch --serve",