Skip to content

Feature request: remove final from TurboStreamResponse #129

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

Closed
zmitic opened this issue Aug 4, 2021 · 1 comment
Closed

Feature request: remove final from TurboStreamResponse #129

zmitic opened this issue Aug 4, 2021 · 1 comment

Comments

@zmitic
Copy link

zmitic commented Aug 4, 2021

If final is removed, it would be easy to stream, redirect and force reload of frames in single response.

Example:

// some controller
return new TurboResponse(
    redirectUrl: $this->generateUrl('basket'),
    reloadFrames: ['shopping-cart-frame', 'some-other-frame'],  // ids of frames
);

and some glue js:

app.js
// listen for turbo events
document.addEventListener('turbo:before-fetch-response', (event) => {
  let redirectUrl = event.detail.fetchResponse.header('Turbo-Redirect');
  if (redirectUrl) {
    Turbo.visit(redirectUrl);
  }

  let framesToReload = event.detail.fetchResponse.header('Turbo-Reload-Frame');
  if (framesToReload) {
    framesToReload.trim().split(/\s*,\s*/).forEach((name) => {
      document.getElementById(name)?.reload();
    });
  }
});

and custom response class:

App\Response\TurboResponse
class TurboResponse extends TurboStreamResponse
{
    public function __construct(?string $redirectUrl = null, array $reloadFrames = [], ?string $content = null)
    {
        parent::__construct($content);
        if ($redirectUrl) {
            $this->headers->set('turbo-redirect', $redirectUrl);
        }
        foreach ($reloadFrames as $id) {
            $this->headers->set('turbo-reload-frame', $id, false);
        }
    }
}
@chalasr
Copy link
Member

chalasr commented Jan 17, 2022

The TurboStreamResponse class has been removed in #217.

@chalasr chalasr closed this as completed Jan 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants