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

e.preventDefault should block the network request in fetch event #51

Closed
wycats opened this issue May 24, 2013 · 27 comments
Closed

e.preventDefault should block the network request in fetch event #51

wycats opened this issue May 24, 2013 · 27 comments

Comments

@wycats
Copy link

wycats commented May 24, 2013

If the fetch handler calls e.preventDefault(), it should behave like normal DOM events and prevent the default behavior. In this case, the default behavior is the default network fetch.

@annevk
Copy link
Member

annevk commented Jun 4, 2013

So this results in a network error if nothing is done?

@alecf
Copy link
Contributor

alecf commented Aug 16, 2013

Not a network error - it just means that the object is free to respond later.

i.e. these two things should more or less be equivalent

  1. explicitly preventDefault():

    e.preventDefault();
    networkFetch(someURL).then(function(response) { e.respondWith(response); });

  2. return a promise through respondWith

    e.respondWith(new Promise(function(resolver) { resolver.resolve(networkFetch(someURL); }));

@alecf
Copy link
Contributor

alecf commented Sep 30, 2013

The only issue with pattern 1 above is that it more or less requires the "event" object to be used after the scope of the event has completed... I think this breaks some basic DOM Event assumptions and I'm reluctant to say we should support it.

I now think only pattern 2 should be allowed, and that preventDefault() should mostly be a no-op.

@annevk
Copy link
Member

annevk commented Sep 30, 2013

Yeah, similar to replying to the install event, you should probably always reply with a promise somehow.

@annevk
Copy link
Member

annevk commented Sep 30, 2013

If you have two listeners, should the respondWith method on the second listener throw if a promise is already registered?

@annevk
Copy link
Member

annevk commented Jan 24, 2014

No they would race.

I think everyone is agreed that what OP suggests is how this should work.

@annevk annevk closed this as completed Jan 24, 2014
@tabatkins
Copy link
Member

Wait, I thought we discussed this on Wednesday, and calling .respondWith with a promise "claims" the response, so the second fetch handler wouldn't run at all.

@annevk
Copy link
Member

annevk commented Jan 24, 2014

It wasn't clear to me that respondWith would also invoke stopImmediatePropagation.

@KenjiBaheux
Copy link
Collaborator

Was there any conclusion reached in the meantime?

@annevk
Copy link
Member

annevk commented Aug 5, 2014

I don't remember anything.

@jakearchibald
Copy link
Contributor

  • You have to call event.respondWith() synchronously, it should throw otherwise
  • Calling event.preventDefault() without calling event.respondWith() in any invoked listener will result in a network error
  • event.respondWith() also invokes event. stopImmediatePropagation()
  • Calling event.respondWith() more than once should throw

@annevk
Copy link
Member

annevk commented Aug 5, 2014

The current specification seems broken. E.g. "wait to respond flag" should not unset. Why would it be?

@jungkees
Copy link
Collaborator

jungkees commented Aug 5, 2014

  • respondWith(r) step3 waits until r settles; wait to respond flag is unset after that happens
  • [[HandleFetch]] step14.2.3.2 waits until wait to respond flag is unset.

@annevk
Copy link
Member

annevk commented Aug 5, 2014

At that point respondWith would no longer throw. Bug?

@jungkees
Copy link
Collaborator

jungkees commented Aug 5, 2014

Will have respondWith(r) throw when it sets the respond-with error flag in step 4 and 5 of the algorithm.

@jungkees
Copy link
Collaborator

jungkees commented Aug 5, 2014

Addressed that point: f60dfca

@annevk
Copy link
Member

annevk commented Aug 5, 2014

I'm not sure what you just did makes sense or addresses my point. My point was that if respondWith can only be invoked once, the current design does not accomplish that.

@jungkees
Copy link
Collaborator

jungkees commented Aug 5, 2014

FetchEvent's wait to respond flag is initially unset when it's first fired. respondWith(r)'s step1 is checking whether the event's wait to respond flag is already set and throws, if so.

@annevk
Copy link
Member

annevk commented Aug 5, 2014

Yes, but you unset it as I pointed out...

@jungkees
Copy link
Collaborator

jungkees commented Aug 5, 2014

Oh you're right. Will fix it tomorrow. Thanks for spotting.

@jungkees
Copy link
Collaborator

jungkees commented Aug 6, 2014

Fixed: dfa1536. Thanks.

@KenjiBaheux
Copy link
Collaborator

Tracked at crbug.com/410699 for Blink.

@mfalken
Copy link
Member

mfalken commented Nov 13, 2014

I don't see where this part from Jake's comment is spec'd:
-You have to call event.respondWith() synchronously, it should throw otherwise

@mfalken
Copy link
Member

mfalken commented Nov 14, 2014

Unless I'm missing something, this bug should be re-opened. The commits seem to just address calling respondWith() multiple times, whereas the original bug is about preventDefault().

Blink plans to implement the points in Jake's comment: #51 (comment)

@annevk annevk reopened this Nov 14, 2014
@jungkees
Copy link
Collaborator

The rest of the points made in #51 (comment) have been addressed: f022c3a.

@mfalken
Copy link
Member

mfalken commented Nov 17, 2014

Thanks! Looks good to me.

@jungkees
Copy link
Collaborator

Closing.

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

No branches or pull requests

8 participants