Skip to content

Commit

Permalink
fix: limit static download concurrency (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdempster authored Apr 19, 2021
1 parent 31bef1e commit bb4af1a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"ipx": "^0.6.0",
"is-https": "^3.0.2",
"node-fetch": "^2.6.1",
"p-limit": "^3.1.0",
"requrl": "^3.0.2",
"ufo": "^0.6.11",
"upath": "^2.0.1"
Expand Down
6 changes: 4 additions & 2 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { mkdirp } from 'fs-extra'
import { dirname, join, relative, resolve, extname } from 'upath'
import fetch from 'node-fetch'
import { joinURL, hasProtocol, parseURL } from 'ufo'
import pLimit from 'p-limit'
import { ModuleOptions, MapToStatic, ResolvedImage } from './types'
import { hash, logger } from './utils'

Expand All @@ -27,15 +28,16 @@ export function setupStaticGeneration (nuxt: any, options: ModuleOptions) {

nuxt.hook('generate:done', async () => {
const { dir: generateDir } = nuxt.options.generate
const limit = pLimit(8)
const downloads = Object.entries(staticImages).map(([url, name]) => {
if (!hasProtocol(url)) {
url = joinURL(options.internalUrl, url)
}
return downloadImage({
return limit(() => downloadImage({
url,
name,
outDir: resolve(generateDir, '_nuxt/image' /* TODO: staticImagesBase */)
})
}))
})
await Promise.all(downloads)
})
Expand Down
4 changes: 3 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8752,7 +8752,7 @@ p-limit@^2.0.0, p-limit@^2.2.0:
dependencies:
p-try "^2.0.0"

p-limit@^3.0.2:
p-limit@^3.0.2, p-limit@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-3.1.0.tgz#e1daccbe78d0d1388ca18c64fea38e3e57e3706b"
integrity sha512-TYOanM3wGwNGsZN2cVTYPArw454xnXj5qmWF1bEoAc4+cU/ol7GVh7odevjp1FNHduHc3KZMcFduxU5Xc6uJRQ==
Expand Down Expand Up @@ -12337,8 +12337,10 @@ watchpack@^1.7.4:
resolved "https://registry.yarnpkg.com/watchpack/-/watchpack-1.7.5.tgz#1267e6c55e0b9b5be44c2023aed5437a2c26c453"
integrity sha512-9P3MWk6SrKjHsGkLT2KHXdQ/9SNkyoJbabxnKOoJepsvJjJG8uYTR3yTPxPQvNDI3w4Nz1xnE0TLHK4RIVe/MQ==
dependencies:
chokidar "^3.4.1"
graceful-fs "^4.1.2"
neo-async "^2.5.0"
watchpack-chokidar2 "^2.0.1"
optionalDependencies:
chokidar "^3.4.1"
watchpack-chokidar2 "^2.0.1"
Expand Down

0 comments on commit bb4af1a

Please sign in to comment.