Skip to content

Commit

Permalink
🐛 batched-xhr-impl: use absolute url as key for batchFetch (ampprojec…
Browse files Browse the repository at this point in the history
…t#26583)

* 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?
  • Loading branch information
samouri authored Feb 4, 2020
1 parent a82e99b commit bb397cd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/service/batched-xhr-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/unit/test-batched-xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit bb397cd

Please sign in to comment.