bungle / lua-resty-template

Templating Engine (HTML) for Lua and OpenResty.
BSD 3-Clause "New" or "Revised" License
909 stars 204 forks source link

set the content length #56

Closed karlseguin closed 1 year ago

karlseguin commented 1 year ago

I'm not sure if there's a reason not to do this? By setting the content length, we're no longer relying on chunked encoding which, all other things being equal, I think is a win.

bungle commented 1 year ago

@karlseguin, nice! let me think about this a bit. I think this originally worked outside OpenResty too, like normal Lua. So we may need to be careful if we want to keep that.

There is also this: https://github.com/bungle/lua-resty-template#templateprint

So one way is to monkeypatch to print. But perhaps good default should include the length. Thus I was thinking that perhaps changing this to include the header is better: https://github.com/bungle/lua-resty-template/blob/master/lib/resty/template.lua#L130

The content type is another thing that we may want to consider (perhaps headers as a whole).

karlseguin commented 1 year ago

I actually think you're right. I think this just needs to be documented.

I ended up adding a helper file in my own app that looks something like:

local print = ngx.print

local function render(file, layout)
  local t = template.new(file, layout)
  local view = t:process()
  ngx.header["Content-Length"] = #view
  print(view)
end