From bb397cd9c6c6991ee946ebda1e2fc068a21b86de Mon Sep 17 00:00:00 2001 From: Jake Fried Date: Tue, 4 Feb 2020 12:56:00 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20batched-xhr-impl:=20use=20absolu?= =?UTF-8?q?te=20url=20as=20key=20for=20batchFetch=20(#26583)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * batched-xhr-impl: use absolute url as key for batchFetch * lint * getWindowOrigin --> getSourceOrigin * test for the originUrl behavior * fix lint * how does .only keep coming back? --- src/service/batched-xhr-impl.js | 8 ++++++-- test/unit/test-batched-xhr.js | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/service/batched-xhr-impl.js b/src/service/batched-xhr-impl.js index 6ab1a864b885..dcff638a222a 100644 --- a/src/service/batched-xhr-impl.js +++ b/src/service/batched-xhr-impl.js @@ -16,8 +16,8 @@ import {Xhr} from './xhr-impl'; import {getService, registerServiceBuilder} from '../service'; +import {getSourceOrigin, removeFragment, resolveRelativeUrl} from '../url'; import {map} from '../utils/object'; -import {removeFragment} from '../url'; /** * A wrapper around the Xhr service which batches the result of GET requests @@ -83,7 +83,11 @@ export class BatchedXhr extends Xhr { * @private */ getMapKey_(input, responseType) { - return removeFragment(input) + responseType; + const absoluteUrl = resolveRelativeUrl( + input, + getSourceOrigin(this.win.location) + ); + return removeFragment(absoluteUrl) + responseType; } } diff --git a/test/unit/test-batched-xhr.js b/test/unit/test-batched-xhr.js index 37406c49f814..162169f16841 100644 --- a/test/unit/test-batched-xhr.js +++ b/test/unit/test-batched-xhr.js @@ -52,6 +52,20 @@ describes.sandboxed('BatchedXhr', {}, env => { }); }); + it('should fetch once for a relative and absolute URL that point to the same location.', async () => { + const originUrl = 'https://testwebsite.com'; + const cdnUrl = + 'https://testwebsite-com.cdn.ampproject.org/v/s/testwebsite.com/hello-world'; + env.win.location.href = cdnUrl; + + await Promise.all([ + xhr.fetch(`${originUrl}/get?k=v1`), + xhr.fetch('/get?k=v1'), + ]); + + expect(fetchStub).to.be.calledOnce; + }); + it( 'should separately cache generic fetches with identical URLs' + 'but different "Accept" headers',