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.
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:
Copy
import getHtml, { fetchWithAxios, fetchWithNodeFetch, fetchWithStealthPuppeteer } from "waterfall-fetch";const url = "https://example.com";// Define a custom set of strategiesconst customSet = [fetchWithNodeFetch, fetchWithAxios, fetchWithStealthPuppeteer];// Use the custom setconst result = await getHtml(url, { set: customSet });console.log(result.html);console.log(result.strategy.name); // Logs the name of the successful strategy
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.