Skip to content

Commit

Permalink
ok let's try netlify cache folder
Browse files Browse the repository at this point in the history
  • Loading branch information
hanbyul-here committed Jun 5, 2019
1 parent c134b22 commit ef05421
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ yarn-error.log*

# Nuxt generate
dist
cache-mock
.nuxt_build

# Bulma build
.tmp
Expand Down
39 changes: 23 additions & 16 deletions cache-me.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
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')

var rsync = new Rsync()
const rsync = new Rsync()
.shell('ssh')
.flags('azq')
.source(BUILD_PATH)
.destination(CACHE_PATH);
.source(BUILD_PATH + '/')
.destination(CACHE_PATH)

function cacheFinalFiles() {
fs.copy((BUILD_PATH, CACHE_PATH)
.then(() => console.log('successssss'))
.catch(err => console.error(err))
export async function cacheFinalFiles() {
try {
await fs.copy(BUILD_PATH, CACHE_PATH)
} catch (e) {
console.log(e)
}
}

fs.ensureDir(CACHE_PATH)
.then(() => {
rsync.execute(function(error, code, cmd) {
// we're done
if(!error) {
fs.copy(CACHE_PATH, BUILD_PATH)
.then(() => cacheFinalFiles())
.catch(err => console.error(err))
export 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)
cacheFinalFiles()
}
})
})
} catch (err) {
// handle error
console.log(err)
}
}
2 changes: 1 addition & 1 deletion netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

[build]
publish = "dist"
command = "npm run incremental-build -- --baseURL $DEPLOY_PRIME_URL"
command = "npm run generate -- --baseURL $DEPLOY_PRIME_URL"

[context.master]
publish = "dist"
Expand Down
13 changes: 11 additions & 2 deletions nuxt.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { initialRoutes, additionalRoutes, initialTotalRoutes, additionalTotalRoutes } from './route-mock'
import { cacheAndCopy } from './cache-me'
const mockRoutes = require('./route-mock')
const { initialRoutes, additionalRoutes, initialTotalRoutes, additionalTotalRoutes } = mockRoutes

const routesToGenerate = process.env.INITIAL_BUILD === 'true' ? initialRoutes : additionalRoutes
const routesToGenerate = process.env.INITIAL_BUILD !== 'false' ? initialRoutes : additionalRoutes
const routesList = process.env.INITIAL_BUILD === 'true' ? initialTotalRoutes : additionalTotalRoutes
const isDev = process.env.DEPLOY_ENV === 'DEV'

Expand Down Expand Up @@ -110,5 +112,12 @@ module.exports = {
})
}
}
},
hooks: {
generate: {
async done() {
await cacheAndCopy()
}
}
}
}
16 changes: 0 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"version": "1.0.0",
"description": "Nuxt.js project",
"author": "Hanbyul Jo <[email protected]>",
"private": true,
"scripts": {
"initial-build": "INITIAL_BUILD=true nuxt generate",
"incremental-build": "INITIAL_BUILD=false nuxt generate --no-build; node cache-me;",
"incremental-build": "INITIAL_BUILD=false nuxt generate --no-build;",
"dev": "DEPLOY_ENV=DEV nuxt",
"build": "nuxt build",
"start": "nuxt start",
"clean-all": "rm -rf .nuxt; rm -rf dist;rm -rf .nuxt_build;",
"generate": "nuxt generate",
"generate-netlify": "DEPLOY_ENV=PRODUCTION nuxt generate",
"lint": "eslint --ext .js,.vue --ignore-path .gitignore .",
Expand All @@ -24,7 +24,6 @@
"devDependencies": {
"babel-eslint": "^10.0.1",
"babel-plugin-dynamic-import-node": "^2.2.0",
"cache-me-outside": "0.0.10",
"eslint": "^5.9.0",
"eslint-config-prettier": "^3.3.0",
"eslint-friendly-formatter": "^3.0.0",
Expand Down
15 changes: 11 additions & 4 deletions route-mock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const initialRoutes = [
const initialRoutes = [
{
route: 'subroute-a',
payload: {
Expand All @@ -13,7 +13,7 @@ export const initialRoutes = [
}
]

export const additionalRoutes = [
const additionalRoutes = [
{
route: 'subroute-c',
payload: {
Expand All @@ -22,5 +22,12 @@ export const additionalRoutes = [
}
]

export const initialTotalRoutes = initialRoutes
export const additionalTotalRoutes = initialRoutes.concat(additionalRoutes)
const initialTotalRoutes = initialRoutes
const additionalTotalRoutes = initialRoutes.concat(additionalRoutes)

module.exports = {
initialRoutes,
additionalRoutes,
initialTotalRoutes,
additionalTotalRoutes
}

0 comments on commit ef05421

Please sign in to comment.