-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix session starting issue on flash messages usage #859
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
Conversation
Use of flash messages triggers session start, which as of last year triggers Symfony to mark response as private. Fix the issue by checking if there is a session first as adviced on: https://symfony.com/doc/current/session/avoid_session_start.html
In the symfony docs, both can be find:
https://symfony.com/blog/new-in-symfony-3-3-improved-flash-messages Normally symfony should do it automatically or? |
@sebastianblum I think your question is more for @javiereguiluz or maybe rather @nicolas-grekas. I just picked this from the doc, as part of my talk Take your Http caching to the next level with xkey & Fastly at Symfony Live London, and symfony-demo just happens to follow what ever doc says is best practice. But that said I agree, the framework should not trigger session start on things like this, if it can avoid it. |
The area where flash messages are displayed is by nature not cacheable, as its content can vary per user. Having no flash messages doesn't mean the page is cacheable: caching it would mean that the empty-state can be cached - which would mean that flash messages could be hidden to users that should see them (since the cached version would be served.) The only way I can think of to fix this once for all is by using a separate request for flash messages (i.e. either XHR or IFRAME or ESI). |
True, that would probably be cleaner then how FosHttpCache works around this: |
Let's display flashes only when the verb is POST! |
Thank you André. |
This PR was merged into the master branch. Discussion ---------- Fix session starting issue on flash messages usage Use of flash messages triggers session start, which as of last year triggers Symfony to mark response as private, and not cached. Fix the issue by checking if there is a session first as advised on: https://symfony.com/doc/current/session/avoid_session_start.html Commits ------- 2362ac8 Fix session starting issue in symfony-demo on flash messages
@javiereguiluz sorry but this is wrong as explained... |
It looks correct according to Symfony Docs 😕 Please, someone send a new pull request to fix this merge. But please keep in mind that this is the Symfony Demo app ... so we cant't complicate it with things that are only relevant for apps with lots of traffic. For example we can't display the flashes with a sub-request and enable Varnish, etc. Thanks! |
Sure, but docs should probably be updated here, and demo should follow best practice in docs right? |
I'd like to make a new release of this app very soon. If someone shows me how to fix this merge, I can make the new PR myself. Thanks! |
replace |
Thanks Nico! See #863. |
This PR was submitted for the 4.1 branch but it was merged into the 3.4 branch instead (closes #10435). Discussion ---------- Improved the article about session flashes As discussed here symfony/demo#859 Not sure about the branch though. Commits ------- e6ce2f4 Improved the article about session flashes
@nicolas-grekas the use case of flash messages is precisely to display a message on a GET request after the redirection returned by the POST request. Displaying them only for POST requests make them useless (if you render some content instead of redirecting, you don't need to use the flash message system, as you can pass a variable to your template without storing it in the session) |
Sure, I forgot about the redirect. This means there are conflicting expectations here: you cannot cache a page and use the session at the same, even conditionally. |
Use of flash messages triggers session start, which as of last year
triggers Symfony to mark response as private, and not cached.
Fix the issue by checking if there is a session first as advised on:
https://symfony.com/doc/current/session/avoid_session_start.html