Custom Strategy Sets
Learn how to define and use custom strategy sets in Waterfall-Fetch
Custom Strategy Sets
Waterfall-Fetch allows you to customize the order and selection of strategies used to fetch HTML content. This advanced feature gives you fine-grained control over the fetching process, allowing you to optimize for your specific use case.
Built-in Strategy Sets
By default, Waterfall-Fetch comes with two built-in strategy sets:
-
Cheap Set: Optimized for cost-effectiveness and speed
- Axios
- Node-fetch
- Puppeteer (as a last resort)
-
JS Set: Designed for JavaScript-heavy websites (slower, but more accurate)
- Puppeteer (with stealth mode)
- Axios (as a fallback)
- Node-fetch (as a final attempt)
Using Built-in Sets
To use a built-in set, you can specify it in the options
parameter of the getHtml
function:
Custom Strategy Sets
You can create your own custom strategy set by passing an array of strategy functions to the set
option. This allows you to prioritize strategies based on your specific needs.
To create a custom set:
- Import the strategy functions you want to use
- Create an array with your desired order of strategies
- Pass this array to the
set
option
Here’s an example:
Strategy Order and Fallback Behavior
The order of strategies in your custom set is crucial:
- Strategies are attempted in the order they appear in the array.
- If a strategy fails, Waterfall-Fetch moves to the next strategy in the array.
- If all strategies fail, Waterfall-Fetch will throw an error.
- You can include the same strategy multiple times in the array, which can be useful for retrying a strategy with different options or configurations.
By carefully crafting your custom strategy set, you can optimize the fetching process for your specific use case and improve the accuracy and efficiency of your HTML fetching.