Skip to content

Commit

Permalink
Update to only use fs cache during build
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Feb 2, 2020
1 parent 625463e commit ee76ca0
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
3 changes: 3 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ module.exports = {
// only compile build-rss in production server build
if (dev || !isServer) return cfg

// we're in build mode so enable shared caching for Notion data
process.env.USE_CACHE = 'true'

const originalEntry = cfg.entry
cfg.entry = async () => {
const entries = { ...(await originalEntry()) }
Expand Down
1 change: 1 addition & 0 deletions src/lib/build-rss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { postIsReady, getBlogLink } from './blog-helpers'

// must use weird syntax to bypass auto replacing of NODE_ENV
process.env['NODE' + '_ENV'] = 'production'
process.env.USE_CACHE = 'true'

// constants
const NOW = new Date().toJSON()
Expand Down
6 changes: 3 additions & 3 deletions src/lib/notion/getBlogIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { BLOG_INDEX_ID, BLOG_INDEX_CACHE } from './server-constants'

export default async function getBlogIndex(previews = true) {
let postsTable: any = null
const isProd = process.env.NODE_ENV === 'production'
const useCache = process.env.USE_CACHE === 'true'
const cacheFile = `${BLOG_INDEX_CACHE}${previews ? '_previews' : ''}`

if (isProd) {
if (useCache) {
try {
postsTable = JSON.parse(await readFile(cacheFile, 'utf8'))
} catch (_) {
Expand Down Expand Up @@ -77,7 +77,7 @@ export default async function getBlogIndex(previews = true) {
)
}

if (isProd) {
if (useCache) {
writeFile(cacheFile, JSON.stringify(postsTable), 'utf8').catch(() => {})
}
}
Expand Down

0 comments on commit ee76ca0

Please sign in to comment.