Skip to content

Commit

Permalink
Redirect before handling a response to prevent @entangle from changin…
Browse files Browse the repository at this point in the history
…g before a redirect.
  • Loading branch information
calebporzio committed Oct 7, 2020
1 parent ea9660b commit 1d68dda
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dist/livewire.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/livewire.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/manifest.json
@@ -1 +1 @@
{"/livewire.js":"/livewire.js?id=273e2852331ec0ca16bf"}
{"/livewire.js":"/livewire.js?id=113e213167e044b8bb85"}
8 changes: 4 additions & 4 deletions js/component/index.js
Expand Up @@ -250,17 +250,17 @@ export default class Component {
handleResponse(message) {
let response = message.response

this.updateServerMemoFromResponseAndMergeBackIntoResponse(message)

store.callHook('message.received', message, this)

// This means "$this->redirect()" was called in the component. let's just bail and redirect.
if (response.effects.redirect) {
this.redirect(response.effects.redirect)

return
}

this.updateServerMemoFromResponseAndMergeBackIntoResponse(message)

store.callHook('message.received', message, this)

if (response.effects.html) {
// If we get HTML from the server, store it for the next time we might not.
this.lastFreshHtml = response.effects.html
Expand Down
9 changes: 9 additions & 0 deletions tests/Browser/Redirects/Component.php
Expand Up @@ -7,6 +7,8 @@

class Component extends BaseComponent
{
public $message = 'foo';

public function flashMessage()
{
session()->flash('message', 'some-message');
Expand All @@ -19,6 +21,13 @@ public function redirectWithFlash()
return $this->redirect('/livewire-dusk/Tests%5CBrowser%5CRedirects%5CComponent');
}

public function redirectPage()
{
$this->message = 'bar';

return $this->redirect('/livewire-dusk/Tests%5CBrowser%5CRedirects%5CComponent');
}

public function render()
{
return View::file(__DIR__.'/view.blade.php');
Expand Down
16 changes: 15 additions & 1 deletion tests/Browser/Redirects/Test.php
Expand Up @@ -24,7 +24,21 @@ public function test()
->click('@redirect-with-flash')->waitForReload()
->assertPresent('@flash.message')
->waitForLivewire()->click('@refresh')
->assertNotPresent('@flash.message');
->assertNotPresent('@flash.message')

/**
* Livewire response is not handled if redirecting.
*/
->refresh()
->assertSeeIn('@redirect.blade.output', 'foo')
->assertSeeIn('@redirect.alpine.output', 'foo')
->runScript('window.addEventListener("beforeunload", e => { e.preventDefault(); e.returnValue = ""; });')
->click('@redirect.button')
->pause(500)
->dismissDialog()
->assertSeeIn('@redirect.blade.output', 'foo')
->assertSeeIn('@redirect.alpine.output', 'foo')
;
});
}
}
4 changes: 4 additions & 0 deletions tests/Browser/Redirects/view.blade.php
Expand Up @@ -3,6 +3,10 @@
<button wire:click="flashMessage" dusk="flash">Flash</button>
<button wire:click="redirectWithFlash" dusk="redirect-with-flash">Redirect With Flash</button>

<button wire:click="redirectPage" dusk="redirect.button">Redirect Page</button>
<span dusk="redirect.blade.output">{{ $message }}</span>
<span x-data="{ message: @entangle('message') }" x-text="message" dusk="redirect.alpine.output"></span>

<div>
@if (session()->has('message'))
<h1 dusk="flash.message">{{ session()->get('message') }}</h1>
Expand Down

0 comments on commit 1d68dda

Please sign in to comment.