@@ -368,26 +368,24 @@ def subscribe_request_will_be_sent
368368 @page . on ( "Network.requestWillBeSent" ) do |params |
369369 request = Network ::Request . new ( params )
370370
371- # We can build exchange in two places, here on the event or when request
372- # is interrupted. So we have to be careful when to create new one. We
373- # create new exchange only if there's no with such id or there's but
374- # it's filled with request which means this one is new but has response
375- # for a redirect. So we assign response from the params to previous
376- # exchange and build new exchange to assign this request to it.
377- exchange = select ( request . id ) . last
378- exchange = build_exchange ( request . id ) unless exchange &.blank?
379-
380371 # On redirects Chrome doesn't change `requestId` and there's no
381372 # `Network.responseReceived` event for such request. If there's already
382373 # exchange object with this id then we got redirected and params has
383374 # `redirectResponse` key which contains the response.
384- if params [ "redirectResponse" ]
385- previous_exchange = select ( request . id ) [ -2 ]
375+ if params [ "redirectResponse" ] && ( previous_exchange = select ( request . id ) . last )
386376 response = Network ::Response . new ( @page , params )
387377 response . loaded = true
388378 previous_exchange . response = response
389379 end
390380
381+ # We can build exchange in two places, here on the event or when request
382+ # is interrupted. So we have to be careful when to create new one. We
383+ # create new exchange only if there's no with such id or there's but
384+ # it's filled with request which means this one is new but has response
385+ # for a redirect. So we assign response from the params to previous
386+ # exchange and build new exchange to assign this request to it.
387+ exchange = select ( request . id ) . last
388+ exchange = build_exchange ( request . id ) unless exchange &.blank?
391389 exchange . request = request
392390
393391 @exchange = exchange if exchange . navigation_request? ( @page . main_frame . id )
0 commit comments