Skip to content

Commit

Permalink
Merge pull request #794 from alesmraz/main
Browse files Browse the repository at this point in the history
🚀 feat: add config for remote images download delays
  • Loading branch information
dc7290 authored Mar 10, 2024
2 parents d96f271 + 4978490 commit f89f438
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
8 changes: 8 additions & 0 deletions docs/docs/03-Features/01-external-images.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,11 @@ Also, no downloading to local is performed.
src="https://next-export-optimize-images.vercel.app/og.png?width=3840"
/>
```

### `remoteImagesDownloadsDelay`

- Type: number

In case you need to download a large amount of images from an external CDN with a rate limit, this will add delays between downloading images.

effectively this will add `sleep` function between downloads.
6 changes: 6 additions & 0 deletions docs/docs/04-Configurations/01-basic-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,9 @@ You can directly specify the URL of an external image.
This is useful in cases where it is not known what images will be used for the build using variables, for example.

https://next-export-optimize-images.vercel.app/docs/Features/external-images#when-specifying-an-external-image-url-with-a-variable

### `remoteImagesDownloadsDelay`

- Type: number

In case you need to download a large amount of images from an external CDN with a rate limit, this will add delays between downloading images.
16 changes: 15 additions & 1 deletion src/cli/external-images/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@ import got from 'got'

import type { Manifest } from '../'

import type { Config } from './../../utils/getConfig'

type ExternalImagesDownloaderArgs = {
terse?: boolean
manifest: Manifest
destDir: string
remoteImagesDownloadsDelay?: Config['remoteImagesDownloadsDelay']
}

const externalImagesDownloader = async ({ terse = false, manifest, destDir }: ExternalImagesDownloaderArgs) => {
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))

const externalImagesDownloader = async ({
terse = false,
manifest,
destDir,
remoteImagesDownloadsDelay,
}: ExternalImagesDownloaderArgs) => {
if (!terse) {
// eslint-disable-next-line no-console
console.log('\n- Download external images -')
Expand All @@ -25,6 +35,10 @@ const externalImagesDownloader = async ({ terse = false, manifest, destDir }: Ex

if (downloadedImages.some((image) => image === externalUrl)) continue

if (remoteImagesDownloadsDelay) {
await sleep(remoteImagesDownloadsDelay)
}

promises.push(
(async () => {
downloadedImages.push(externalUrl)
Expand Down
11 changes: 8 additions & 3 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ export const optimizeImages = async ({
config.remoteImages === undefined
? []
: typeof config.remoteImages === 'function'
? await config.remoteImages()
: config.remoteImages
? await config.remoteImages()
: config.remoteImages
if (remoteImages.length > 0) {
const remoteImageList = new Set<string>()

Expand Down Expand Up @@ -216,7 +216,12 @@ export const optimizeImages = async ({
)
}
if (manifest.some(({ externalUrl }) => externalUrl !== undefined)) {
await externalImagesDownloader({ terse, manifest, destDir })
await externalImagesDownloader({
terse,
manifest,
destDir,
remoteImagesDownloadsDelay: config.remoteImagesDownloadsDelay,
})
}

const publicDir = path.resolve(cwd, 'public')
Expand Down
5 changes: 5 additions & 0 deletions src/utils/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ export type Config = {
* @type {string[] | (() => string[] | Promise<string[]>)}
*/
remoteImages?: string[] | (() => string[] | Promise<string[]>)

/**
* In case you need to download a large amount of images from an external CDN with a rate limit, this will add delays between downloading images.
*/
remoteImagesDownloadsDelay?: number
}

const getConfig = (): Config => {
Expand Down

0 comments on commit f89f438

Please sign in to comment.