-
Notifications
You must be signed in to change notification settings - Fork 59
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
Flash messages not deleted when added in context through the onPreResponse extension point #113
Comments
What is happening here is that your You might try stating that your extension has a server.ext({
type: 'onPreResponse',
method: (request, reply) => {
const res = request.response;
if (res.variety === 'view') {
if (!res.source.context) res.source.context = {};
const flashMessages = {
success: request.yar.flash('success')
};
res.source.context.flash = flashMessages;
}
reply.continue();
},
options: {
before: ['yar']
}
}); Another point to note is that if the route that sets your flash also renders a view then it will be flushed on that route rather than the next HTTP request, which may or may not be what you want but you should be aware of it. Typically I've seen flash messages set before a redirect to another route. To answer your question about what is different between the extension and the handler approach we need to look at the flow in your ext approach. So here's what's happening:
The difference with the example where you get the flash value in the handler is just the order of steps above. You can swap 4 and 5 around. In this case the flash value is correctly cleared before the session is stored in the cookie. |
OK, so if I got you right, I have to fetch the flash value before Yar's I just updated my Many thanks @mtharrison |
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions. |
I am creating flash messages from some handlers using this code:
Then in my handlebars layout, I add this:
{{#if flash.success}}<div class="alert alert-success">{{flash.success}}</div>{{/if}}
What is missing is the code that stores the flash messages in the flash template variable.
If for example I want to display the previous flash success message in the '/' route, I get it working when I do this directly in the route handler:
The problems start when I want to do it in
onPreResponse
extension point. See code below:So in my handler I can just do this
reply.view('index')
, and the flash template variable will be automatically set in theonPreResponse
extension point.The problem is that the flash message are not deleted, they are accumulated. The first time it outputs 'Resource created !', the second time it outputs 'Resource created !Resource created !' and so on.
I am unable to find what is wrong here. The only difference between the two methods is the time when I add the flash messages in context. I the first method, the working one, I do it right in the route handler; in the second method, I do it in the
onPreResponse
extension point. What is yar doing differently between those two moments ?Please help
The text was updated successfully, but these errors were encountered: