Skip to content

[Turbo] Add Vary header for stream responses in docs #215

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
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Turbo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ Let's discover how to use Turbo Streams to enhance your [Symfony forms](https://
namespace App\Controller;

// ...
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -300,6 +301,10 @@ use App\Entity\Task;

class TaskController extends AbstractController
{
// The same URL will return a response with a different Content-Type HTTP header
// depending on the Accept request header.
// To prevent cache pollution issues, we must set the Vary response header.
#[Cache(vary: ['Accept'])]
Copy link
Member

Choose a reason for hiding this comment

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

but this is not needed actually, since the call to getPreferredFormat is nested in $form->isSubmitted() && $form->isValid(), which means POST, which means no-cache.

Copy link
Member Author

Choose a reason for hiding this comment

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

More or less but:

  1. Turbo Streams can be used with GET requests too, this adds an example of how to handle content negotiation properly
  2. It's a best practice anyway to set Vary regardless of the verb
  3. Even if it's not commonly implemented by cache servers, responses to POST requests can be cached (by the spec) and reused for subsequent GET requests for the same resource

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but this example is about a form. The example you added in #217 has return $response->setVary('Accept');, which is enough a hint to me.
Here, yes, POST can be cached, but ONLY if Cache-Control is sent. I think that if one does that (set the cc header), then it's their job to also add the Vary, and we don't have to tell everybody to add it.

public function new(Request $request): Response
{
$form = $this->createForm(TaskType::class, new Task());
Expand Down