Skip to content

Instantly share code, notes, and snippets.

@ascheink
Created January 17, 2011 23:51
Show Gist options
  • Save ascheink/783724 to your computer and use it in GitHub Desktop.
Save ascheink/783724 to your computer and use it in GitHub Desktop.
Try ESI's locally
module ApplicationHelper
USE_AJAX_FOR_ESIS = false
def my_render_esi(path)
response.headers['X-RUN-ESI'] = 'true'
if Rails.env.development?
if USE_AJAX_FOR_ESIS
div_id = Digest::MD5.hexdigest(path + rand.to_s)
<<-END
<div id="#{div_id}"></div>
<script type="text/javascript">
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "#{path}", false);
xmlhttp.send();
document.getElementById("#{div_id}").innerHTML = xmlhttp.responseText;
</script>
END
else
begin
require 'net/http'
url = URI.parse('http://localhost.nytimes.com:3001' + path)
req = Net::HTTP::Get.new(path)
res = Net::HTTP.start(url.host, url.port) { |http| http.request(req) }
res.body
rescue Errno::ECONNREFUSED => e
<<-END
<div style="font-family: sans-serif; font-size: large; background-color: red; color: white; font-weight: bold; padding: 2em;">
To test ESIs locally, start a second server:
<pre>rails server -p 3001</pre>
</div
END
end
end
else
'<esi:include src="' + path + '" />'
end.html_safe
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment