Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
Add useExtendedUniqueKey option to Request
Ondra Urbanc301b76775f1
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e273571e0..fb14ccf9a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -13,6 +13,10 @@ NEXTto add requests to the queue from pure JavaScript navigations, form submissions etc.- The `RequestQueue.handledCount()` function has been resurrected from deprecation,in order to have compatible interface with `RequestList`.+- Add `useExtendedUniqueKey` option to `Request` constructor to include `method` and `payload`+ in the `Request`'s computed `uniqueKey`.+- Updated Puppeteer to 1.18.0+0.14.15 / 2019-05-31====================diff --git a/docs/api/Request.md b/docs/api/Request.mdindex 7f3747720..1c9c47180 100644--- a/docs/api/Request.md+++ b/docs/api/Request.md@@ -139,12 +139,13 @@ const foo = request.userData.foo;<tr><td colspan="3"><p>A unique key identifying the request.Two requests with the same <code>uniqueKey</code> are considered as pointing to the same URL.</p>-<p>If <code>uniqueKey</code> is not provided, then for GET requests it is automatically generated by normalizing the URL.+<p>If <code>uniqueKey</code> is not provided, then it is automatically generated by normalizing the URL.For example, the URL of <code>HTTP://www.EXAMPLE.com/something/</code> will produce the <code>uniqueKey</code>of <code>http://www.example.com/something</code>.</p><p>The <code>keepUrlFragment</code> option determines whether URL hash fragment is included in the <code>uniqueKey</code> or not.</p>-<p>For other than GET requests, method and payload are included in the generated <code>uniqueKey</code>,-producing a <code>uniqueKey</code> in the following format: <code>METHOD(payloadHash):normalizedUrl</code>.</p>+<p>The <code>useExtendedUniqueKey</code> options determines whether method and payload are included in the <code>uniqueKey</code>,+producing a <code>uniqueKey</code> in the following format: <code>METHOD(payloadHash):normalizedUrl</code>. This is useful+when requests point to the same URL, but with different methods and payloads. For example: form submits.</p><p>Pass an arbitrary non-empty text value to the <code>uniqueKey</code> propertyto override the default behavior and specify which URLs shall be considered equal.</p></td></tr><tr>@@ -178,6 +179,13 @@ to override the default behavior and specify which URLs shall be considered equaFor example, this causes the <code>http://www.example.com#foo</code> and <code>http://www.example.com#bar</code> URLsto have the same <code>uniqueKey</code> of <code>http://www.example.com</code> and thus the URLs are considered equal.Note that this option only has an effect if <code>uniqueKey</code> is not set.</p>+</td></tr><tr>+<td><code>[options.useExtendedUniqueKey]</code></td><td><code>Boolean</code></td><td><code>false</code></td>+</tr>+<tr>+<td colspan="3"><p>If <code>true</code> then the <code>uniqueKey</code> is computed not only from the URL, but also from the method and payload+ properties. This is useful when making requests to the same URL that are differentiated by method+ or payload, such as form submit navigations in browsers.</p></td></tr></tbody></table><a name="Request+pushErrorMessage"></a>diff --git a/src/request.js b/src/request.jsindex 21575b64e..31385c04f 100644--- a/src/request.js+++ b/src/request.js@@ -5,10 +5,18 @@ import { checkParamOrThrow } from 'apify-client/build/utils';import { normalizeUrl } from 'apify-shared/utilities';import log from 'apify-shared/log';-export function computeUniqueKey({ url, method, payload, keepUrlFragment }) {+export function computeUniqueKey({ url, method, payload, keepUrlFragment, useExtendedUniqueKey }) {const normalizedMethod = method.toUpperCase();- const normalizedUrl = normalizeUrl(url, keepUrlFragment);- if (normalizedMethod === 'GET') return normalizedUrl;+ const normalizedUrl = normalizeUrl(url, keepUrlFragment) || url; // It returns null when url is invalid, causing weird errors.+ if (!useExtendedUniqueKey) {+ if (normalizedMethod !== 'GET' && payload) {+ // Using log.deprecated to log only once. We should add log.once or some such.+ log.deprecated(`We've encountered a ${normalizedMethod} Request with a payload. `+ + 'This is fine. Just letting you know that if your requests point to the same URL '+ + 'and differ only in method and payload, you should see the "useExtendedUniqueKey" option of Request constructor.');+ }+ return normalizedUrl;+ }const payloadHash = payload ? hashPayload(payload) : '';return `${normalizedMethod}(${payloadHash}):${normalizedUrl}`;}@@ -54,14 +62,15 @@ export function hashPayload(payload) {* @param {String} [options.uniqueKey] A unique key identifying the request.* Two requests with the same `uniqueKey` are considered as pointing to the same URL.*- * If `uniqueKey` is not provided, then for GET requests it is automatically generated by normalizing the URL.+ * If `uniqueKey` is not provided, then it is automatically generated by normalizing the URL.* For example, the URL of `HTTP://www.EXAMPLE.com/something/` will produce the `uniqueKey`* of `http://www.example.com/something`.** The `keepUrlFragment` option determines whether URL hash fragment is included in the `uniqueKey` or not.*- * For other than GET requests, method and payload are included in the generated `uniqueKey`,- * producing a `uniqueKey` in the following format: `METHOD(payloadHash):normalizedUrl`.+ * The `useExtendedUniqueKey` options determines whether method and payload are included in the `uniqueKey`,+ * producing a `uniqueKey` in the following format: `METHOD(payloadHash):normalizedUrl`. This is useful+ * when requests point to the same URL, but with different methods and payloads. For example: form submits.** Pass an arbitrary non-empty text value to the `uniqueKey` property* to override the default behavior and specify which URLs shall be considered equal.@@ -84,6 +93,10 @@ export function hashPayload(payload) {* For example, this causes the `http://www.example.com#foo` and `http://www.example.com#bar` URLs* to have the same `uniqueKey` of `http://www.example.com` and thus the URLs are considered equal.* Note that this option only has an effect if `uniqueKey` is not set.+ * @param {Boolean} [options.useExtendedUniqueKey=false]+ * If `true` then the `uniqueKey` is computed not only from the URL, but also from the method and payload+ * properties. This is useful when making requests to the same URL that are differentiated by method+ * or payload, such as form submit navigations in browsers.** @property {String} id* Request ID@@ -133,8 +146,9 @@ class Request {errorMessages = null,headers = {},userData = {},- keepUrlFragment = false,handledAt = null,+ keepUrlFragment = false,+ useExtendedUniqueKey = false,} = options;@@ -150,6 +164,8 @@ class Request {checkParamOrThrow(headers, 'headers', 'Object');checkParamOrThrow(userData, 'userData', 'Object');checkParamOrThrow(handledAt, 'handledAt', 'Maybe String | Date');+ checkParamOrThrow(keepUrlFragment, 'keepUrlFragment', 'Boolean');+ checkParamOrThrow(useExtendedUniqueKey, 'useExtendedUniqueKey', 'Boolean');if (method === 'GET' && payload) throw new Error('Request with GET method cannot have a payload.');@@ -158,8 +174,7 @@ class Request {this.id = id;this.url = url;this.loadedUrl = loadedUrl;- // NOTE: If URL is invalid, computeUniqueKey() returns null which was causing weird errors- this.uniqueKey = uniqueKey || computeUniqueKey({ url, method, payload, keepUrlFragment }) || url;+ this.uniqueKey = uniqueKey || computeUniqueKey({ url, method, payload, keepUrlFragment, useExtendedUniqueKey });this.method = method;this.payload = payload;this.noRetry = noRetry;diff --git a/test/request.js b/test/request.jsindex d2b9d4dae..5fa95ab45 100644--- a/test/request.js+++ b/test/request.js@@ -24,7 +24,7 @@ describe('Apify.Request', () => {const payload = JSON.stringify({ foo: 'bar' });const payloadHash = hashPayload(payload);const normalizedUrl = normalizeUrl(url);- const request = new Apify.Request({ url, method: 'post', payload });+ const request = new Apify.Request({ url, method: 'post', payload, useExtendedUniqueKey: true });const uniqueKey = `POST(${payloadHash}):${normalizedUrl}`;