4

I have a simple PHP app with mod_pagespeed in Apache 2.4.

Looking at the PageSpeed module documentation (https://developers.google.com/speed/pagespeed/module/filter-head-add) it says this adds a <head/> tag to a document if it encounters a body tag.

Problem is, I'm making an AJAX request that returns an HTML table row, that I then append to a table.

The response before mod_pagespeed was:

<tr><td>Data1</td><td>Data2</td></tr>

I could just do a:

$('#myTable > tbody:last-child').append(data);

And it would work fine.

Now the response is:

<head/><tr><td>Data1</td><td>Data2</td></tr>

And it's breaking.

My question is, does this happen all the time? Do I need to remove the add_head directive? Is there another way to let mod_pagespeed know this is an AJAX request?

Mariano
  • 1,023
  • 1
  • 11
  • 19

1 Answers1

0

I think your response has a text/html content type, so mod_pagespeed assumes it's a HTML document with a missing head tag.

Try setting a different content type (like text/plain):

header("Content-Type: text/plain");

or something complete made-up like text/ajaxresponse

header("Content-Type: text/ajaxresponse");

which will be treated also as plain text in common browsers. Now mod_pagespeed doesn't treat your response as a complete HTML document anymore, so the head tag won't be inserted anymore.

A special content type header for "Ajax response" doesn't exist afaik, as the content type for Ajax request isn't fixed. If your response would be JSON, application/json could be used.

Reeno
  • 5,720
  • 11
  • 37
  • 50