简体   繁体   English

Rails response_to处理XML但缓存HTML

[英]Rails respond_to processes XML but caches HTML

This might be a bit of a tricky one. 这可能有点棘手。 The 'root' page of my site is rendered by the posts#index action. 我网站的“根”页面由posts#index操作呈现。 To support rss and html, I have a respond_to block that looks like: 为了支持rss和html,我有一个response_to块,如下所示:

 respond_to do |wants|
  wants.html 
  wants.xml {
    render :layout => false;
    response.headers["Content-Type"] = "application/xml; charset=utf-8"
  }
end

I also have a 'caches_page' set on the index page. 我还在索引页面上设置了“ caches_page”。

If someone comes to the site in a browser and just requests "/", then they get served the html version of the page, and Rails also writes a cached page for index.html 如果有人在浏览器中访问该网站并只请求“ /”,那么他们将获得该页面的html版本,并且Rails还将为index.html编写一个缓存的页面。

There isn't really any way to request "/" with a format of XML, but if I hit "/posts.xml" it renders XML and caches posts.xml (similarly if I hit "/posts" or "/posts.html" it will cache posts.html). 实际上并没有任何方法可以请求XML格式的“ /”,但是如果我命中“ /posts.xml”,它将呈现XML并缓存posts.xml(类似地,如果我命中了“ / posts”或“ / posts”)。 html”将缓存posts.html)。 That all works just fine. 一切正常。

Now for the tricky bit. 现在为难点。 If something requests "/" but has an accept header like: 如果某项请求“ /”但具有一个接受标头,例如:

Accept: text/xml

Then Rails will process it as XML (probably correct), but CACHES it as html, destroying my cache. 然后,Rails会处理它作为XML(可能是正确的),但是缓存为HTML,破坏我的缓存。 The next visitor to the site will be forever server an html file that actually contains XML. 该站点的下一个访问者将是永远包含实际包含XML的html文件的服务器。 Here is the Rails log message proving this is happening: 这是Rails日志消息,证明了这种情况的发生:

Started GET "/" for 127.0.0.1 at 2010-11-30 20:47:27 +0000
  Processing by PostsController#index as XML
  Post Load (1.4ms)  SELECT "posts".* FROM "posts" WHERE ...
Rendered posts/index.xml.rxml (243.8ms)
Write page /..../index.html (0.6ms)
Completed 200 OK in 423ms (Views: 244.8ms | ActiveRecord: 1.4ms)

Is this a feature or a bug? 这是功能还是错误?

Better, has anyone any idea how to fix it so that it caches the file as .xml when it processes it as XML? 更好的是,没有人知道如何解决它,以便在将其作为XML处理时将其缓存为.xml吗?

Which version of Rails are you using? 您正在使用哪个版本的Rails? You might want to upgrade, if your on an older version. 如果您使用的是旧版本,则可能要升级。 (the .rxml extension is old afaik) (.rxml扩展名是旧的afaik)

That looks like a bug to me. 在我看来,这似乎是个虫子。 I remember having similar problems two years back (IE says it accepts XLS, but it doesn't mention HTML). 我记得两年前遇到过类似的问题(IE表示它接受XLS,但没有提到HTML)。

I believe it was fixed in recent versions. 我相信它在最新版本中已修复。

I used to work around it by modifying the HTTP_ACCEPT header for IE. 我曾经通过修改IE的HTTP_ACCEPT标头来解决此问题。 (digs up some old code) (找出一些旧代码)

request.env["HTTP_ACCEPT"] = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" if request.env["HTTP_USER_AGENT"].include?('MSIE')

But I wouldn't recommend doing something like this before you tried the more sane options. 但是我不建议您在尝试更合理的选择之前先做这样的事情。

If you change your request for XML to point at /.xml then I think the cache should differentiate between the HTML and XML as they will be different URLs. 如果/.xml XML的请求更改为指向/.xml那么我认为缓存应区分HTML和XML,因为它们将是不同的URL。

I had this same problem with a Backbone.js app. 我在Backbone.js应用程序中遇到了同样的问题。 If I navigated from / to /tasks then hit the back button Chrome would serve the cached HTML version of /tasks , instead of loading the JSON version. 如果我从/ /tasks导航到/tasks然后单击后退按钮,Chrome将提供/tasks的缓存HTML版本,而不是加载JSON版本。 The fix was to make sure my Backbone models were calling /tasks.js instead of just /tasks . 解决方法是确保我的Backbone模型正在调用/tasks.js而不是/tasks Rails 3.x routes take the format as an optional part at the end of the URL, which helps any caches between your browser and server differentiate between different formats if they aren't looking at the accept/content-type headers. Rails 3.x路由将格式作为URL末尾的可选部分,这有助于浏览器和服务器之间的所有缓存在不查看accept / content-type标头时区分不同格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM