Skip to content

Commit

Permalink
feat: remove concurrency
Browse files Browse the repository at this point in the history
until everything is thread safe or we have recipes for users to build in a thread-safe way, we
should make sure presta behaves in an obvious manner
  • Loading branch information
estrattonbailey committed May 8, 2021
1 parent 7d26793 commit 9ab4c69
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 79 deletions.
93 changes: 44 additions & 49 deletions lib/renderStaticEntries.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const fs = require('fs-extra')
const path = require('path')
const c = require('ansi-colors')
const PQueue = require('p-queue').default
const mime = require('mime-types')

const { debug } = require('./debug')
Expand All @@ -19,7 +18,6 @@ function renderStaticEntries (entries, config, options = {}) {
debug('renderStaticEntries', entries)

const allGeneratedFiles = []
const queue = new PQueue({ concurrency: 1 })

function onError (e, { location }) {
if (options.watch) {
Expand All @@ -28,9 +26,6 @@ function renderStaticEntries (entries, config, options = {}) {
` errors detected, pausing...`
)}\n`
)

// IMPORTANT, reset this for next pass
queue.clear()
} else {
log(`\n ${c.red('error')} ${location}\n\n > ${e.stack || e}\n`)

Expand All @@ -40,53 +35,53 @@ function renderStaticEntries (entries, config, options = {}) {
y({ allGeneratedFiles })
}

queue.on('idle', () => {
y({ allGeneratedFiles })
loadCache.clearAllMemory()
})

for (const entry of entries) {
queue.add(async () => {
try {
delete require.cache[entry]

const file = require(entry)
const paths = await file.getStaticPaths()

for (const url of paths) {
const time = timer()
const context = createContext({
path: url,
params: file.route ? getRouteParams(url, file.route) : {}
})

const response = normalizeResponse(await file.handler(context))
const type = response.headers['Content-Type']
const ext = type ? mime.extension(type) : 'html'
const filename = pathnameToFile(url, ext)

allGeneratedFiles.push(filename)

fs.outputFileSync(
path.join(config.output, OUTPUT_STATIC_DIR, filename),
response.body,
'utf-8'
)

formatLog({
color: 'green',
action: 'build',
meta: '• ' + time(),
description: url
})
}
} catch (e) {
const location = entry.replace(config.cwd, '')

onError(e, { location })
try {
delete require.cache[entry]

const file = require(entry)
const paths = await file.getStaticPaths()

for (const url of paths) {
const time = timer()
const context = createContext({
path: url,
params: file.route ? getRouteParams(url, file.route) : {}
})

const response = normalizeResponse(await file.handler(context))
const type = response.headers['Content-Type']
const ext = type ? mime.extension(type) : 'html'
const filename = pathnameToFile(url, ext)

allGeneratedFiles.push(filename)

fs.outputFileSync(
path.join(config.output, OUTPUT_STATIC_DIR, filename),
response.body,
'utf-8'
)

formatLog({
color: 'green',
action: 'build',
meta: '• ' + time(),
description: url
})
}
})

y({ allGeneratedFiles })
} catch (e) {
const location = entry.replace(config.cwd, '')

onError(e, { location })

break
}
}

// clear to prevent memory leak
loadCache.clearAllMemory()
})
}

Expand Down
29 changes: 0 additions & 29 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
"mime-types": "^2.1.30",
"module-alias": "^2.2.2",
"ms": "^2.1.2",
"p-queue": "^6.6.1",
"pocket.io": "^0.1.4",
"query-string": "^6.13.7",
"regexparam": "^1.3.0",
Expand Down

0 comments on commit 9ab4c69

Please sign in to comment.