Skip to content

✨ Amp Actions: AMP.goBack action should call history.back#26585

Merged
samouri merged 12 commits intoampproject:masterfrom
samouri:goback-samaurijack
Jul 15, 2020
Merged

✨ Amp Actions: AMP.goBack action should call history.back#26585
samouri merged 12 commits intoampproject:masterfrom
samouri:goback-samaurijack

Conversation

@samouri
Copy link
Copy Markdown
Member

@samouri samouri commented Jan 31, 2020

Implements #5225

@samouri samouri marked this pull request as ready for review February 1, 2020 00:00
@samouri samouri requested a review from dreamofabear February 1, 2020 00:00
@samouri samouri self-assigned this Feb 1, 2020
@samouri samouri force-pushed the goback-samaurijack branch 2 times, most recently from 4b95ad5 to 966135b Compare February 3, 2020 21:48
@dreamofabear
Copy link
Copy Markdown

Not a huge fan of this name. Maybe we can parameterize goBack? #bikeshed

@dvoytenko
Copy link
Copy Markdown
Contributor

Not a huge fan of this name. Maybe we can parameterize goBack? #bikeshed

I mentioned this on the issue as well. But I believe in the current world, we could just extend goBack w/o any new parameters, as long as we have a user gesture.

@samouri
Copy link
Copy Markdown
Member Author

samouri commented Feb 6, 2020

Does that mean:

  • With user gesture: pop the same-doc history stack. If stack is empty call history.back
  • Without user gesture: only pop same-doc history stack

@dvoytenko
Copy link
Copy Markdown
Contributor

  • With user gesture: pop the same-doc history stack. If stack is empty call history.back
  • Without user gesture: only pop same-doc history stack

From what I can see: a history stack modification with a user gesture is very unambiguous for within- and cross-doc navigation (i.e. history stack pop). I'd even constraint all of them. But if it's "too late", I believe it would be reasonable to allow cross-doc navigation iff the user gesture is present.

@dreamofabear
Copy link
Copy Markdown

I think goBack currently already requires a user gesture per ActionTrust.

handleAmpTarget_(invocation) {
// All global `AMP` actions require default trust.
if (!invocation.satisfiesTrust(ActionTrust.DEFAULT)) {
return null;
}

Would any existing pages rely on the fact that goBack doesn't pop window history? I'm having trouble thinking of a good use case. If not, I suppose we could just treat this as a bug fix and forge ahead (probably worth an I2S though).

@dvoytenko
Copy link
Copy Markdown
Contributor

I think goBack currently already requires a user gesture per ActionTrust.

handleAmpTarget_(invocation) {
// All global `AMP` actions require default trust.
if (!invocation.satisfiesTrust(ActionTrust.DEFAULT)) {
return null;
}

Would any existing pages rely on the fact that goBack doesn't pop window history? I'm having trouble thinking of a good use case. If not, I suppose we could just treat this as a bug fix and forge ahead (probably worth an I2S though).

Does ActionTrust.DEFAULT cover only active gestures? If so - we can proceed with it then. If we have any worries w.r.t. backward compatibility, we can parametrize.

@samouri
Copy link
Copy Markdown
Member Author

samouri commented Feb 11, 2020

Does ActionTrust.DEFAULT cover only active gestures? If so - we can proceed with it then. If we have any worries w.r.t. backward compatibility, we can parametrize.

Yes! At least according to action-constants.js:

* Events that are triggered nearly immediately (up to a few seconds) after
* a user gesture with strong intent (e.g. tap or swipe) are "default trust".
*
* Actions that can modify the page's visual state (e.g. content jumping)
* should require "default trust". This is the default required trust level
* for actions.
*/
DEFAULT: 2,

I do still see this .goBack function called in a couple of places though, so I'll need to parameterize it to make sure its only used in places with at least default trust.

@samouri samouri changed the title ✨ Amp Actions: implement AMP.windowBack ✨ Amp Actions: AMP.goBack action should call history.back() Feb 11, 2020
@samouri samouri changed the title ✨ Amp Actions: AMP.goBack action should call history.back() ✨ Amp Actions: AMP.goBack action should call history.back Feb 11, 2020
@samouri
Copy link
Copy Markdown
Member Author

samouri commented Feb 14, 2020

I've made the updates, this should be ready for review

@samouri samouri force-pushed the goback-samaurijack branch from 1e75aec to 756cc60 Compare July 13, 2020 16:08
@samouri samouri force-pushed the goback-samaurijack branch from 756cc60 to 805651a Compare July 14, 2020 20:51
case 'goBack':
Services.historyForDoc(this.ampdoc).goBack();
Services.historyForDoc(this.ampdoc).goBack(
/*canPerforWindowBack*/ !!args && args['navigate']
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is args['navigate'] a boolean or a string? If string, what if I pass navigate=false?

Copy link
Copy Markdown
Member Author

@samouri samouri Jul 15, 2020

Choose a reason for hiding this comment

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

Great question! There is a full ParserTokenizer that will correctly discover the type as a boolean, so navigate=false works as expected.

I'm pretty sure the code that does this is:

// Boolean literal.
if (convertValues && (s == 'true' || s == 'false')) {
const value = s == 'true';
return {type: TokenType.LITERAL, value, index: newIndex};
}

I also verified this empirically since I don't understand the parser just yet

return this.enque_(() => {
if (this.stackIndex_ <= 0) {
// Nothing left to pop.
if (this.stackIndex_ <= 0 && !canPerformWindowBack) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

For HistoryBindingVirtual, do viewers face-plant if they receive a negative stackIndex param?

Copy link
Copy Markdown
Member Author

@samouri samouri Jul 15, 2020

Choose a reason for hiding this comment

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

The SRP Viewer won't face-plant. I believe it'll just drop the request as invalid

@dreamofabear
Copy link
Copy Markdown

Oh and remember to update documentation. :)

@samouri samouri merged commit 29dc250 into ampproject:master Jul 15, 2020
@samouri samouri deleted the goback-samaurijack branch July 15, 2020 19:15
mszylkowski pushed a commit to mszylkowski/amphtml that referenced this pull request Jul 22, 2020
…#26585)

* history-impl: parameterize goBack

* fixed test name

* always use the binding

* fix tests

* improve doc

* back to AMP.windowBack instead.

* restore canPerformWindowBack

* also test noarg

* update to parameterized func

* checktypes, use quoted prop

* will updates

* keep enthusiastic boolean conversion, constrain to ==true
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.

5 participants