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
Refactor experiments to use localStorage #22796
Refactor experiments to use localStorage #22796
Conversation
/to @jridgewell /cc @cramforce @molnarg |
src/experiments.js
Outdated
const tokens = experimentCookie ? experimentCookie.split(/\s*,\s*/g) : []; | ||
function getExperimentToggles(win) { | ||
const experimentsString = | ||
'localStorage' in win ? win.localStorage.getItem(LOCAL_STORAGE_KEY) : ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This probably needs to be try-block guarded.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks.
tools/experiments/experiments.js
Outdated
const button = redirect.querySelector('button'); | ||
const anchor = redirect.querySelector('a'); | ||
button.addEventListener('click', function() { | ||
const ampUrl = viewerToAmpUrl(input.value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should switch to folks just entering their domain and going from there instead of mentioning the AMP viewer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about removing the viewer mention but we detect and support both?
Copy/pasting a viewer URL avoids typing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That might suggest it would work in Safari (which it won't). I'd really go with simple here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, which part won't work in Safari?
Viewer URL detection is just "is the domain google.com?". We can even add support for Bing viewer here, though their experiments page is currently broken. :)
Anyways I removed it, but some mobile testing workflows will suffer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Safari, localStorage in a top-window (cache URL) isn't visible when the same origin loads in a viewer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL 🤦♂
Re: #22247 (comment)
@cramforce Done, and noted to switch to KV storage when available.
@aghassemi I looked into adding something to the AMP Validator Chrome extension but it doesn't seem like a great fit and of limited use since it's desktop only. I think the bookmarklet trick works on mobile and is almost as good. I can help write the code for it.
@kristoferbaxter Changed
|
This is going to be too painful for experiments that impact all pages during dogfood phase. When we were implementing
Integrating with a Chrome extension at least helps a bit when dogfooding on desktop. Is this really worth doing given the restrictions it creates? Is cookie tossing possible without our JS or CDN being compromised? |
Makes sense, I'll see if I can hack something up. |
@aghassemi Cookie will break anyway, when cdn.ampproject.org goes on the suffix list. If you want to do horizontal experiments going forward, it'll need either a viewer experiment or a chrome extension. |
What about @jridgewell's solution? Make the experiment page set http-only cookie and have serving infra amend the JS it serves with the right flags? |
Cookies will have the same scope as localStorage when we switch to having
cdn.ampproject.org on the suffix list.
…On Wed, Jun 12, 2019 at 7:53 PM Ali Ghassemi ***@***.***> wrote:
What about @jridgewell <https://github.com/jridgewell>'s solution? Make
the experiment page set http-only cookie and have serving infra amend the
JS it serves with the right flags?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#22796?email_source=notifications&email_token=AAAV4T4KKWNVEY5GZPEDWVDP2GZDDA5CNFSM4HXDX3Y2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXSLQOI#issuecomment-501528633>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAAV4T2XR4BHCKZCURDA3ZDP2GZDDANCNFSM4HXDX3YQ>
.
|
@cramforce So the actual suffix domain itself can't have its own cookie jar? Unless that's the case (which is surprising to me because domains like |
Huh true, public suffixes can set cookies for themselves despite what the spec implies: publicsuffix/list#461 We'll likely move to public suffix eventually but I wanted to postpone it for now since it takes O(months) for browsers to update (and then it only affects latest version). It also breaks canary opt-in for CDN pages which is an even bigger dev-ex pain. |
300a456
to
4f509e5
Compare
Extended suffixes may be able to set cookies for themselves (which would be cool, cause it would mean we would not lose the dev channel opt-in!!!) but they cannot set cookies that are visible to their children. |
So, could we please make the Experiments work similar to Dev/RC channel and have the server respond with patched JS? I understand this will require new changes on the serving infra, but being able to turn on an experiment for all AMP is really really valuable. |
We wouldn't lose opt-in for pages that use unversioned JS URLs, but we'd lose it for CDN pages which reads the parent domain cookie for RTV rewriting. So we'd need two cache serving changes:
Like I said, happy to pursue these changes (which may be non-trivial) as part of the move to public suffix list. However for short-term, I'd still like to get this in for immediate security benefit and to unblock an important launch. :) |
I don't agree with breaking a very useful feature to get things launched without doing the complete solution first, but that is not my call. |
Appreciate the candid feedback. It also pointed out a better long-term solution which is great. As a counterpoint, this seems like a (temporary) trade off of dev-ex for user experience:
|
Sorry I am holding a hard position on this, ability to opt-in to experiments across all AMP has saved us tremendously in the past to ensure we launch higher quality horizontal features such as auto lightbox, auto-sizes for Currently this is not a (temporary) trade off of dev-ex for user experience IMO since the "user experience" in question here is an unlaunched feature. It seems like a trade of dev-ex (which in this case is about quality assurance to ensure good UX anyway) for a quicker launch. |
The horizontal experiments only work on the AMP Cache, where they can also be turned on via URL param (and a Search experiment), so I think this is independent. |
Right now yes, the proposed approach will also fix that. You also know how much of a pain setting up a Search experiment is. Can we at least do an assessment of how much work serving infra needs to do to make experiments work like RC/Dev? I think we all agree with the long-term solution, question is whether the short-term solution is a good tradeoff. I see the trade-off as putting burden of work on Runtime's users to go through more work to setup experiments to test their code vs launch of |
This plugs an existing potential security hole for AMP Cache almost immediately, which is mostly independent of amp-script. Also, horizontals are still possible for experiments enabled in canary-config.json, and 0.5% of users is within norms e.g. for Search LEs. So the quantified dev-ex impact is "somewhat greater friction in low-volume alpha testing across many domains".
I'll start investigating this. But Chrome pulls PSL as part of release cycle (every 6 weeks) and Safari is possibly much longer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @aghassemi that this will be annoying for horizontal experiments. If we can get approval for a google.com cookie, I can make this much easier by using query params to enable-disable the cookie during the foo-com.cdn.ampproject.org
document request. But this won't really help origin pages.
if (!getMode().test) { | ||
user().warn( | ||
TAG, | ||
'"%s" experiment %s for the domain "%s". See: https://amp.dev/documentation/guides-and-tutorials/learn/experimental', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Enabled/Disabled experiment "foo" on domain "bar.com"
src/experiments.js
Outdated
const tokens = experimentCookie ? experimentCookie.split(/\s*,\s*/g) : []; | ||
function getExperimentToggles(win) { | ||
let experimentsString = ''; | ||
if ('localStorage' in win) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think even testing in
causes an error in Safari. Let's move this inside the try-catch.
src/experiments.js
Outdated
// Set explicit domain, so the cookie gets send to sub domains. | ||
domain: win.location.hostname, | ||
allowOnProxyOrigin: true, | ||
if ('localStorage' in win) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto.
if (host === 'cdn.ampproject.org') { | ||
const experimentsDesc = document.getElementById('experiments-desc'); | ||
experimentsDesc.setAttribute('hidden', ''); | ||
experimentsTable.setAttribute('hidden', ''); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we be displaying an error message?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a new message in the UI (see screenshot). Or did you mean dev console?
* Refactor experiments to use localStorage, not cookies. * Update experiments.html. * Fix lint. * Whitelist in presubmit check for localStorage. * Show redirect tool on cdn.ampproject.org instead of experiment toggles. * Use util functions where possible. * Add user warning to AMP.toggleExperiment. * Update experiments/README.md. * Fix tests. * Try/catch localStorage, support origin URLs. * Remove viewer URL support. * Move localStorage check inside try/catch.
* Refactor experiments to use localStorage, not cookies. * Update experiments.html. * Fix lint. * Whitelist in presubmit check for localStorage. * Show redirect tool on cdn.ampproject.org instead of experiment toggles. * Use util functions where possible. * Add user warning to AMP.toggleExperiment. * Update experiments/README.md. * Fix tests. * Try/catch localStorage, support origin URLs. * Remove viewer URL support. * Move localStorage check inside try/catch.
Summary
Caveats
It's no longer possible to enable experiments for multiple publishers on the AMP Experiments page.
For example, to enable experiments for pages with source origin
www.bbc.co.uk
, need to navigate tohttps://www-bbc-co-uk.cdn.ampproject.org/experiments.html
.Note that cookie usage was already unreliable on Safari due to ITP2 (#18889).
How to enable experiments
When testing on desktop or tethered mobile devices:
AMP.toggleExperiment('foo')
in dev console (or methods below).For testing on untethered mobile devices:
javascript:AMP.toggleExperiment('foo')
.[*] Due to Safari cache partitioning, experiments can only be enabled via
AMP.toggleExperiment
in dev tools on Safari.Screenshots
https://cdn.ampproject.org/experiments.html
https://www-nytimes-com.cdn.ampproject.org/experiments.html
Existing usages of localStorage