Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow firebase for 3p frames #25092

Merged
merged 7 commits into from Oct 18, 2019
Merged

Conversation

cathyxz
Copy link
Contributor

@cathyxz cathyxz commented Oct 17, 2019

Added a fortesting option to gulp firebase to enable testing ads on custom domains.

Legacy description

The following are outdated descriptions of this PR.

Take 2

This PR does the following:
1. Re-adds integration.js and f.js to the list of files that we prepend AMP_CONFIG to.
2. Adds code in firebase.js to add firebase to the list of allowed 3rd party urls and 3rd party frame regexes so that ads can work on firebase.

Filed:

Take 1

I wanted to make testing amp-ad work for firebase deployments. They're currently failing here:

amphtml/3p/integration.js

Lines 719 to 740 in 3e6457f

export function validateAllowedTypes(window, type, allowedTypes) {
const thirdPartyHost = parseUrlDeprecated(urls.thirdParty).hostname;
// Everything allowed in default iframe.
if (window.location.hostname == thirdPartyHost) {
return;
}
if (urls.thirdPartyFrameRegex.test(window.location.hostname)) {
return;
}
if (window.location.hostname == 'ads.localhost') {
return;
}
if (defaultAllowedTypesInCustomFrame.indexOf(type) != -1) {
return;
}
userAssert(
allowedTypes && allowedTypes.indexOf(type) != -1,
'Non-whitelisted 3p type for custom iframe: %s',
type
);
}

The example that I'm using is examples/amp-ads.amp.html.

We currently have a section in firebase.js that appends "thirdPartyUrl":location.origin to AMP_CONFIG. My initial approach here was just to add another modification that would append "thirdPartyFrameRegex": "^.*\.firebaseapp\.com$", which would enable it to pass this test:

if (urls.thirdPartyFrameRegex.test(window.location.hostname)) {

However, by inspecting the output of urls in integration.js and urls in config.js I'm noticing a race condition. I'm observing that sometimes the urls object reflects the contents of the AMP_CONFIG appended to amp.js, and sometimes it does not (no localDev: "true", no thirdPartyUrl, no thirdPartyFrameRegex). It seems like integration.js is trying to access urls before config.js has had a chance to read from the AMP_CONFIG object. I'm wondering if there's a race condition somewhere.

I also noted that in enableLocalDev, there's the option to specify a TEST_HOST with env.AMP_TEST_HOST. Is there a documented way to use this to enable ads on a demo url?

function enableLocalDev(config, target, configJson) {
let LOCAL_DEV_AMP_CONFIG = {localDev: true};
const TESTING_HOST = process.env.AMP_TESTING_HOST;
if (typeof TESTING_HOST == 'string') {
const TESTING_HOST_FULL_URL = TESTING_HOST.match(/^https?:\/\//)
? TESTING_HOST
: 'http://' + TESTING_HOST;
const TESTING_HOST_NO_PROTOCOL = TESTING_HOST.replace(/^https?:\/\//, '');
LOCAL_DEV_AMP_CONFIG = Object.assign(LOCAL_DEV_AMP_CONFIG, {
thirdPartyUrl: TESTING_HOST_FULL_URL,
thirdPartyFrameHost: TESTING_HOST_NO_PROTOCOL,
thirdPartyFrameRegex: TESTING_HOST_NO_PROTOCOL,
});
log(
'Set',
cyan('TESTING_HOST'),
'to',
cyan(TESTING_HOST),
'in',
cyan(target)
);
}
return Object.assign(LOCAL_DEV_AMP_CONFIG, configJson);
}

@cathyxz
Copy link
Contributor Author

cathyxz commented Oct 17, 2019

Hi @rsimha and @lannka ! Apologies for the unconventional way of filing what is actually a feature request. I tried to make ads work on firebase demos by prepending a thirdPartyFrameRegex to AMP_CONFIG, but this isn't working. Do you have any idea why, or what alternative approaches I could perhaps look into?

@cathyxz cathyxz requested a review from lannka October 17, 2019 01:51
@rsimha
Copy link
Contributor

rsimha commented Oct 17, 2019

Hi @rsimha and @lannka ! Apologies for the unconventional way of filing what is actually a feature request.

Here's a high level question: What does gulp firebase do that the PR deploy solution introduced recently by @estherkim dos not do? I recognize that there may be good reasons for both solutions to exist, but it'd be good to eliminate duplication if they are functionally equivalent.

I tried to make ads work on firebase demos by prepending a thirdPartyFrameRegex to AMP_CONFIG, but this isn't working. Do you have any idea why, or what alternative approaches I could perhaps look into?

I think this is due to #24471, where @lannka removed the code that automatically adds AMP_CONFIG to integration.js (unminified) / f.js (minified). IIRC, adding the config to the 3p frame and the main runtime binary was undesirable. Perhaps it needs to be reinstated to support your use case?

@rsimha rsimha requested a review from estherkim October 17, 2019 18:18
@cathyxz
Copy link
Contributor Author

cathyxz commented Oct 17, 2019

I forgot about the PR deploy bot! Maybe we should update our documentation in TESTING.md to make the PR Deploy Bot more discoverable, so we can start deprecating the firebase demo deployment once it's gotten more usage across the board. We can start by adding a line referencing it under the Firebase documentation encouraging people to try it out instead.

@leafsy is currently using this for iterating on a bug bash. I guess the reality of our current use case is that we may push a lot of small updates to a demo, but not wish to have spam an in Review PR with a lot of small commits and force-pushes. If we start using PR deploy bot for this use case, our repo might begin to have a lot of demo Draft PRs that are not intended for merge.

I tried the PR Deploy Bot on this PR, and it seems to suffer from a different issue for ads. None of the ads display on: https://storage.googleapis.com/amp-test-website-1/amp_dist_65323/examples/ads.amp.html
@estherkim do you know what we'd need to do to make ads work on PR deploy bot?

@@ -75,6 +75,7 @@ const UNMINIFIED_TARGETS = [
'amp-inabox.js',
'amp-shadow.js',
'amp.js',
'integration.js',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a partial revert of #24471. I'm not sure why we removed it initially, but per offline discussion with @lannka, this seems ok to reinstate. Filed #25108 to track any follow up refactoring.

Copy link
Contributor

@rsimha rsimha Oct 17, 2019

Choose a reason for hiding this comment

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

@cathyxz / @lannka: Are we sure we don't want to add this for minified builds as well for consistency between gulp build and gulp dist? (The minified equivalent is f.js.)

I ask because several of our tests are able to switch between --compiled mode and regular mode, and I'm concerned that this might break some assumptions in code (not sure what they might be).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it. I'll add in f.js for consistency.

Copy link
Contributor

Choose a reason for hiding this comment

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

This list is now different from the one used by our internal release scripts. (See cl/269449844.)

@lannka @erwinmombay @jridgewell @danielrozenberg Do we want to change that list too? (I don't fully understand how AMP_CONFIG is used by the 3p frame.)

@cathyxz cathyxz changed the title [DO NOT MERGE] allow firebase for 3p frames allow firebase for 3p frames Oct 17, 2019
@cathyxz
Copy link
Contributor Author

cathyxz commented Oct 17, 2019

Updated the PR description. Could @estherkim, @rsimha and @lannka please take a look?

Copy link
Contributor

@rsimha rsimha left a comment

Choose a reason for hiding this comment

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

Couple comments. I'm approving to unblock.

const result = data.replace(
'self.AMP_CONFIG={',
'self.AMP_CONFIG={"thirdPartyUrl":location.origin,'
// eslint-disable-next-line prettier/prettier
'self.AMP_CONFIG={"thirdPartyUrl":location.origin,"thirdPartyFrameRegex": "^.*\.firebaseapp\.com$",'
Copy link
Contributor

@rsimha rsimha Oct 17, 2019

Choose a reason for hiding this comment

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

I believe you need a double-backslash here for a single-backslash to appear in the AMP_CONFIG that's written to file.

'self.AMP_CONFIG={"thirdPartyUrl":location.origin,"thirdPartyFrameRegex": "^.*\\.firebaseapp\\.com$",'

If that works, the eslint-disable-next-line prettier/prettier in the line above will no longer be necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Based on a new approach, I've actually removed this code altogether. Let me know if this approach works better.

@@ -75,6 +75,7 @@ const UNMINIFIED_TARGETS = [
'amp-inabox.js',
'amp-shadow.js',
'amp.js',
'integration.js',
Copy link
Contributor

Choose a reason for hiding this comment

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

This list is now different from the one used by our internal release scripts. (See cl/269449844.)

@lannka @erwinmombay @jridgewell @danielrozenberg Do we want to change that list too? (I don't fully understand how AMP_CONFIG is used by the 3p frame.)

@rcebulko rcebulko removed their request for review October 17, 2019 21:35
@cathyxz
Copy link
Contributor Author

cathyxz commented Oct 17, 2019

After some more digging, I found a way that does not need us to modify the prepend targets in our build process. We can just call enableLocalTesting from firebase.js. I've added code and documentation to that effect. @rsimha @lannka could you take a look? I think this might be a better approach that won't have these side effects.

Copy link
Contributor

@rsimha rsimha left a comment

Choose a reason for hiding this comment

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

I think this might be a better approach that won't have these side effects.

It certainly is! LGTM! (Can you update the description to reflect the new approach?)

@cathyxz cathyxz requested a review from mrjoro October 18, 2019 20:43
@cathyxz
Copy link
Contributor Author

cathyxz commented Oct 18, 2019

I think Owners Bot requires @mrjoro to approve the documentation changes. PTAL?

@cathyxz
Copy link
Contributor Author

cathyxz commented Oct 18, 2019

Updated the PR description and crossed out all the legacy stuff. Pending an LGTM from @mrjoro for documentation changes.

Copy link
Member

@mrjoro mrjoro left a comment

Choose a reason for hiding this comment

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

Doc changed look good!

@cathyxz cathyxz merged commit 4d7b2dd into ampproject:master Oct 18, 2019
@cathyxz cathyxz deleted the firebase/3pframe branch October 18, 2019 22:47
@cathyxz
Copy link
Contributor Author

cathyxz commented Oct 18, 2019

Thank you!

jeffjose pushed a commit to jeffjose/amphtml that referenced this pull request Oct 19, 2019
* allow firebase for 3p frames

* Prepend AMP_CONFIG to integration.js

* Add comments

* Add f.js for consistency

* localize changes to firebase only

* Wait for async function

* Add documentation
joshuarrrr pushed a commit to Parsely/amphtml that referenced this pull request Oct 22, 2019
* allow firebase for 3p frames

* Prepend AMP_CONFIG to integration.js

* Add comments

* Add f.js for consistency

* localize changes to firebase only

* Wait for async function

* Add documentation
micajuine-ho pushed a commit to micajuine-ho/amphtml that referenced this pull request Dec 27, 2019
* allow firebase for 3p frames

* Prepend AMP_CONFIG to integration.js

* Add comments

* Add f.js for consistency

* localize changes to firebase only

* Wait for async function

* Add documentation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants