简体   繁体   中英

enabling rails page caching causes http header charset to disappears

I need charset to be utf-8, which seem to be the case by default. Recently I enabled page caching for a few static pages:

caches_page :about

The caching works fine, and I see the corresponding about.html and contact.html pages generated in my /public folder, except when the page renders, it's no longer in utf-8.

After googling for a bit I tried looking at the http headers with wget, before and after caching:

first time:

$wget --server-response http://localhost:3000/about

HTTP request sent, awaiting response... 
 1 HTTP/1.1 200 OK
 2 X-Ua-Compatible: IE=Edge
 3 Etag: "f7b0b4dea015140f3b5ad90c3a392bef"
 4 Connection: Keep-Alive
 5 Content-Type: text/html; charset=utf-8
 6 Date: Sun, 12 Jun 2011 03:44:22 GMT
 7 Server: WEBrick/1.3.1 (Ruby/1.8.7/2009-06-12)
 8 X-Runtime: 0.235347
 9 Content-Length: 5520
10 Cache-Control: max-age=0, private, must-revalidate

cached:

$wget --server-response http://localhost:3000/about

Resolving localhost... 127.0.0.1
Connecting to localhost[127.0.0.1]:3000... connected.
HTTP request sent, awaiting response... 
 1 HTTP/1.1 200 OK
 2 Last-Modified: Sun, 12 Jun 2011 03:34:42 GMT
 3 Connection: Keep-Alive
 4 Content-Type: text/html
 5 Date: Sun, 12 Jun 2011 03:39:53 GMT
 6 Server: WEBrick/1.3.1 (Ruby/1.8.7/2009-06-12)
 7 Content-Length: 5783

as a result the page displays in ISO-8859-1 and I get a bunch of garbled text. Does anyone know how I can prevent this undesirable result? Thank you.

The solution will depend on the server used.

When you use page cache, the servers reads the server directly, so the rails stack does not provide encoding information to the server. Then the server default apply.

If you're using apache with passenger, add to the configuration:

AddDefaultCharset UTF-8

If you need specific charsets, use a solution like the one in http://www.philsergi.com/2007/06/rails-page-caching-and-mime-types.html

<LocationMatch \/(rss)\/?>
    ForceType text/xml;charset=utf-8
</LocationMatch>
<LocationMatch \/(ical)\/?>
    ForceType text/calendar;charset=utf-8
</LocationMatch>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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