Skip to content

Allow for fetch to not include __amp_source_origin so resources can be cached across pages#6637

Merged
lannka merged 10 commits intoampproject:masterfrom
google:a4a_fetch_wo_origin
Dec 15, 2016
Merged

Allow for fetch to not include __amp_source_origin so resources can be cached across pages#6637
lannka merged 10 commits intoampproject:masterfrom
google:a4a_fetch_wo_origin

Conversation

@keithwrightbos
Copy link
Copy Markdown
Contributor

Issue #6494: ensure that __amp_source_origin is not included on fetchJSON CORS requests for AMP validation public key fetches so that they can be cached across publisher pages.

if (url) {
return xhrFor(this.win).fetchJson(url, {mode: 'cors', method: 'GET'})
return xhrFor(this.win).fetchJson(
url, {mode: 'cors', method: 'GET', disableAmpSourceOrigin: true})
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reformat object literal to multiple lines. Should this be also credentials: 'omit'?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if (url) {
return xhrFor(this.win).fetchJson(url, {mode: 'cors', method: 'GET'})
return xhrFor(this.win).fetchJson(
url, {mode: 'cors', method: 'GET', disableAmpSourceOrigin: true})
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add docs for why the disableAmpSourceOrigin is set.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

input = this.getCorsUrl(this.win, input);
fetchAmpCors_(input, init = {}) {
// Do not append __amp_source_origin
if (!(!init.requireAmpResponseSourceOrigin &&
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, please clean up the boolean logic a little bit :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that! Done

@@ -36,6 +39,7 @@ import {isArray, isObject, isFormData} from '../types';
* headers: (!Object|undefined),
* method: (string|undefined),
* requireAmpResponseSourceOrigin: (boolean|undefined)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make requireAmpResponseSourceOrigin explicit on the caller side.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@keithwrightbos keithwrightbos changed the title A4a fetch wo origin Allow for fetch to not include __amp_source_origin so resources can be cached across pages Dec 14, 2016
@keithwrightbos
Copy link
Copy Markdown
Contributor Author

@cramforce PTAL

mode: 'cors',
method: 'GET',
requireAmpResponseSourceOrigin: false,
disableAmpSourceOrigin: true,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we call it ampCors: false?
Overall, disabling AMP CORS is the purpose, which requires 2 things:

  • Not sending the AMP_SOURCE_ORIGIN.
  • Not check response source origin.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to be as explicit as possible thus the word "disable". Technically this is still a CORS request, it just doesn't include the source origin parameter so ampCors would seem somewhat confusing to me. Will defer to @cramforce

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ampCors is more than CORS. The extra things it does are the 2 I listed. And I don't see use case for:

requireAmpResponseSourceOrigin: true,
disableAmpSourceOrigin: true,

On the other side, requireAmpResponseSourceOrigin only exists for backward compatibility. I hope some day we can get rid of it. So any new changes should not really depend on it .

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modified to use assert as Malte suggested

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think these 2 boolean names are confusing.
We should have the new boolean as ampCors, which defaults to true. In case it's false, it will override requireAmpResponseSourceOrigin to false.

As I mentioned, the requireAmpResponseSourceOrigin boolean exists only for backward compatibility. No new code should use it as false. They should instead go with ampCors: false.

@cramforce

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Convinced. Lets change it.

});

it('should not include __amp_source_origin if disableAmpSourceOrigin ' +
'true & requireAmpResponseSourceOrigin true', () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it a valid use case?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again this is about being explicit. Should somewhat inadvertently specify both but in conflict, the requireAmpResponseSourceOrigin should take precedent (which this verifies).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that should just be covered by dev().assert

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Copy Markdown
Member

@cramforce cramforce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets roll with this. One comment.

});

it('should not include __amp_source_origin if disableAmpSourceOrigin ' +
'true & requireAmpResponseSourceOrigin true', () => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that should just be covered by dev().assert

@keithwrightbos
Copy link
Copy Markdown
Contributor Author

@lannka @cramforce please merge. Thanks

@keithwrightbos
Copy link
Copy Markdown
Contributor Author

@lannka PTAL and merge if ok

@keithwrightbos
Copy link
Copy Markdown
Contributor Author

@lannka PTAL. Changed to use ampCors. Please merge if travis is green. Thx

// (requireAmpResponseSourceOrigin overrides disable).
dev().assert(
!(init.disableAmpSourceOrigin && init.requireAmpResponseSourceOrigin),
!(init.ampCors && init.requireAmpResponseSourceOrigin),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in case ampCors == false, let's just override the value of requireAmpResponseSourceOrigin to false.

@keithwrightbos
Copy link
Copy Markdown
Contributor Author

@lannka PTAL, changed so ampCors = false forces requireAmpResponseSourceOrigin to false

@lannka lannka merged commit a36160a into ampproject:master Dec 15, 2016
Lith pushed a commit to Lith/amphtml that referenced this pull request Dec 22, 2016
…e cached across pages (ampproject#6637)

* Initial commit

* Fix merge issues

* PR responses

* PR review

* PR responses

* PR responses
Lith pushed a commit to Lith/amphtml that referenced this pull request Dec 22, 2016
…e cached across pages (ampproject#6637)

* Initial commit

* Fix merge issues

* PR responses

* PR review

* PR responses

* PR responses
torch2424 pushed a commit to torch2424/amphtml that referenced this pull request Jan 3, 2017
…e cached across pages (ampproject#6637)

* Initial commit

* Fix merge issues

* PR responses

* PR review

* PR responses

* PR responses
@dandv
Copy link
Copy Markdown
Contributor

dandv commented Jun 9, 2017

Is disableAmpSourceOrigin documented somewhere?

@mkaraula
Copy link
Copy Markdown

mkaraula commented Aug 22, 2017

@dandv No its not.

@AndrewKGuan
Copy link
Copy Markdown

Hi,
can we use this flag to turn off __amp_source_origin on fetches now? If yes, please provide an example how to use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants