adding an option for the concurrency limit

This commit is contained in:
Tony Sullivan 2022-06-27 09:28:28 -05:00
parent 9315b75148
commit 8f83dee44f
2 changed files with 31 additions and 3 deletions

View file

@ -92,6 +92,28 @@ export default {
```
</details>
<details>
<summary><strong>config.throttle</strong></summary>
<br/>
By default the prefetch script will only prefetch one link at a time. This behavior can be changed in your `astro.config.*` file to increase the limit for concurrent downloads.
<br/>
```js
import prefetch from '@astrojs/prefetch';
export default {
// ...
integrations: [prefetch({
// Allow up to three links to be prefetched concurrently
throttle: 3
})],
}
```
</details>
## Examples
> Coming soon!

View file

@ -1,4 +1,4 @@
import throttle from 'throttles';
import throttles from 'throttles';
import requestIdleCallback from './requestIdleCallback.js';
const events = ['mouseenter', 'touchstart', 'focus'];
@ -63,10 +63,16 @@ export interface PrefetchOptions {
* @default "a[href][rel='prefetch']"
*/
selector?: string;
/**
* The number of pages that can be prefetched concurrently.
*
* @default 1
*/
throttle?: number;
}
export default function prefetch({ selector = 'a[href][rel="prefetch"]' }: PrefetchOptions) {
const [toAdd, isDone] = throttle();
export default function prefetch({ selector = 'a[href][rel="prefetch"]', throttle = 1 }: PrefetchOptions) {
const [toAdd, isDone] = throttles(throttle);
observer =
observer ||