-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Writing directly into the response writer does not trigger response before hook functions. #2545
Comments
You could use With In regards Here: Lines 54 to 65 in 584cb85
|
p.s. create your templates out of handler if they are not changing. That way you avoid overhead of parsing that template on each request // avoid parsing template in every request
tmpl := template.Must(template.New("htmlTemplate").Parse("Hello Bug!"))
e.GET("/bug", func(c echo.Context) error {
if err := tmpl.Execute(c.Response(), nil); err != nil {
c.Logger().Error(err)
return err
}
return nil
}) |
Thank you. Is saving it first to Which method between the "workaround" method and the solution you posted do you think is the "best practice"? |
Okay, you second argument to templates are most useful for cases when your output is dynamic. Although maybe you are using templates for convenience because they have ability to "render" subtemplates/fragments into single output. In that case templates are useful. |
The code was a minimal code to reproduce the bug. But I will render it to a buffer then use the built-in response function. |
You can not set headers after the response body (at least 1 byte) is sent to the client. This is because headers come before body in HTTP response. If your headers depend on the response body ( |
Issue Description
When we execute a template with html/template and write it directly into the response writer, before hooks on the response do not run.
Just to clarify, I am a beginner.
I would use the workaround method but I assume it uses more memory.
Is this an actual fixable and relevant bug?
Or is the "workaround" method the way to actually do it?
Checklist
Expected behaviour
Before hook should run when we write directly into the response writer.
Actual behaviour
Before hook does not run when we write directly into the response writer.
Steps to reproduce
Copy the code below and try the normal route, the bug route and the workaround route.
Working code to debug
Version/commit
v4.11.3
The text was updated successfully, but these errors were encountered: