2

I want to disable caching in a CQ component and I have the following line in my jsp (documentation):

response.setHeader("Dispatcher", "no-cache");

If I insert the component in a page and load the page in an authoring instance everything works as expected and I get an HTTP header named Dispatcher with the content no-cache.

Now if I do the same on a publishing instance (same configuration with CQ_RUNMODE='publish' and same content) the component works but for setting the HTTP header.

Any idea on why the two instances could behave differently?

Update

I tried to set other headers and the instance behaves in the same way: in the authoring mode the headers are generated in the publishing mode not (same configuration but for the CQ_RUNMODE)

Update 2

I was trying to reduce my example by removing everything that is unnecessary from the page (layout, code for headers, footer, ...) and I noticed that after a certain size threshold my header is correctly generated.

In other words by removing stuff from the page (even simple HTML) I reach a certain point where the header appears (if the page is small enough).

Any idea on why CQ is only generating the header for very small pages?

Matteo
  • 14,696
  • 9
  • 68
  • 106
  • Perhaps this is too obvious a suggestion -- but is there a chance that your code to inject the header is wrapped inside some kind of `` test? – David Gorsline Jan 09 '13 at 22:25
  • @DavidGorsline Thanks for the suggestion. I checked the code and there is no condition. The statements right before the `setHeader` and after the `setHeader` are executed correctly. – Matteo Jan 10 '13 at 08:20
  • About your update 2, I doubt it's the page size that makes a difference - it's more likely when you remove a certain component (included script, etc.) that the headers work again. – Bertrand Delacretaz Jan 17 '13 at 08:41
  • @BertrandDelacretaz nope I just remove a simple HTML line (even a paragraph) and the header reappears. I also see errors about lost chunks. As soon as the page is smaller that an HTTP chunk it works. – Matteo Jan 17 '13 at 08:55

2 Answers2

4

If you're trying to set the header in a component far down on the page, you could be getting the issue that you're trying to write it after the response has been committed.

If you need to flag the page as not cached & you cannot avoid placing the code higher in the buffer, you could instead write in a check for this node type at the start of the JSP (using node.listChildren() for example), or provide a page property that let's the editor control if the page is cached or not.

Community
  • 1
  • 1
anotherdave
  • 6,656
  • 4
  • 34
  • 65
  • 1
    Thanks, it was indeed the problem. I sometimes was able to get the header as the page was small enough that the response was not yet committed when adding the header. The chunking problem is still there but's is unrelated. – Matteo Jan 27 '13 at 09:40
  • 1
    You can control the buffer size by adding buffer="x" to the jsp page directive if it's flushing the buffer too soon. Making it too big will potentially cause scalability issues. You'll need to do this on the first JSP called, i.e. the topmost component. – antonyh Jan 30 '13 at 16:21
2

You didn't indicate which version of CQ5 you're using - I just tested with a minimal JSP script on a CQ 5.5 GA publish instance, and the header is correctly set:

$ curl -u admin:admin http://localhost:4503/tmp/x.tidy.json
{
  "sling:resourceType": "x",
  ...
}

$ curl -u admin:admin http://localhost:4503/apps/x/x.jsp
<%
response.setHeader("Dispatcher","no-cache");
%>
Here's the content.

$ curl -D - -u admin:admin http://localhost:4503/tmp/x.html
HTTP/1.1 200 OK
Connection: Keep-Alive
...
Dispatcher: no-cache

Here's the content.

You might want to start with this minimal test and compare with what you're doing.

Bertrand Delacretaz
  • 6,100
  • 19
  • 24