Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 1 commit into from
Oct 4, 2018

Conversation

andrerom
Copy link
Contributor

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

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
@sebastianblum
Copy link

In the symfony docs, both can be find:
What it the symfony best practise?

  • {% if app.request.hasPreviousSession %}
  • {% if app.session.started %}

https://symfony.com/blog/new-in-symfony-3-3-improved-flash-messages Normally symfony should do it automatically or?

@andrerom
Copy link
Contributor Author

andrerom commented Sep 30, 2018

@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.

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Sep 30, 2018

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).

@andrerom
Copy link
Contributor Author

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:
https://foshttpcachebundle.readthedocs.io/en/latest/features/helpers/flash-message.html

@nicolas-grekas
Copy link
Member

Let's display flashes only when the verb is POST!

@javiereguiluz
Copy link
Member

Thank you André.

@javiereguiluz javiereguiluz merged commit 2362ac8 into symfony:master Oct 4, 2018
javiereguiluz added a commit that referenced this pull request Oct 4, 2018
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
@nicolas-grekas
Copy link
Member

@javiereguiluz sorry but this is wrong as explained...

@javiereguiluz
Copy link
Member

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!

@andrerom andrerom deleted the fix_session_start branch October 4, 2018 13:17
@andrerom
Copy link
Contributor Author

andrerom commented Oct 4, 2018

Sure, but docs should probably be updated here, and demo should follow best practice in docs right?
In other words imho this merge is correct as long as doc is like it is :P

@javiereguiluz
Copy link
Member

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!

@nicolas-grekas
Copy link
Member

replace if app.request.hasPreviousSession by if app.request.method == 'POST' yes

@javiereguiluz
Copy link
Member

Thanks Nico! See #863.

javiereguiluz added a commit that referenced this pull request Oct 4, 2018
This PR was merged into the master branch.

Discussion
----------

Fixed the handling of flash messages

Fixes the #859 merge.

Commits
-------

e6349dc Fixed the handling of flash messages
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Oct 8, 2018
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
@stof
Copy link
Member

stof commented Oct 10, 2018

@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)

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Oct 10, 2018

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.
Using JavaScript, we can fix the issue but we need to completely redesign how flashes work:
Instead of making the POST request trigger a 302, we would make it a 200 with a meta redirection. Just before the meta tag, we would dump a script one that would put the flashes in local storage. Then the header of all pages would contain JS to read that and display them.

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

Successfully merging this pull request may close these issues.

5 participants