NodeJS / express:缓存和304状态码 [英] NodeJS/express: Cache and 304 status code

查看:1105
本文介绍了NodeJS / express:缓存和304状态码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我重新加载使用快递的网站时,我会使用Safari(而不是Chrome)找到一个空白页面,因为NodeJS服务器向我发送了一个304状态代码。 如何解决这个问题?



当然这也可能只是Safari的一个问题,但实际上它适用于所有其他网站,所以它也是我的NodeJS服务器上的一个问题。



为了生成页面,我在使用Jade与 res.render



更新:似乎出现此问题,因为Safari发送'cache-control ':'max-age = 0' on reload。



更新2:我现在有一个解决方法,但是有没有更好的解决方案?
解决方法:

  app.get('/:language('+ content.languageSelector +')/:page ',function(req,res)
{
//禁用内容文件缓存
res.header(Cache-Control,no-cache,no-store,must-revalidate );
res.header(Pragma,no-cache);
res.header(Expires,0);

//渲染东西...
}

更新3:
所以完整的代码部分目前是:

  app.get('/:language('+ content.languageSelector +')/:页面',pageHandle); 

函数pageHandle(req,res)
{
var language = req.params.language;
var thisPage = content.getPage req.params.page,language);

if(thisPage)
{
//禁用内容文件缓存
res.header(Cache-Control ,no-cache,no-store,must-revalidate);
res.header(Pragma,no-cache);
res.header(Expires,0);

res.render(thisPage.file +'_'+ language,{
thisPage:thisPage,
语言:language,
语言:content.languages,
导航:content.navigation,
footerNavigation:content.footerNavigation,
currentYear:new Date()。getFullYear()
});
}
else
{
error404Handling(req,res);
}
}


解决方案

解决方案:

  app.disable('etag'); 

如果你想要更多的控制,这里的替代解决方案:



http: /v>

When I reload a website made with express, I get a blank page with Safari (not with Chrome) because the NodeJS server sends me a 304 status code.

How to solve this?

Of course, this could also be just a problem of Safari, but actually it works on all other websites fine, so it has to be a problem on my NodeJS server, too.

To generate the pages, I'm using Jade with res.render.

Update: It seems like this problem occurs because Safari sends 'cache-control': 'max-age=0' on reload.

Update 2: I now have a workaround, but is there a better solution? Workaround:

app.get('/:language(' + content.languageSelector + ')/:page', function (req, res)
{
    // Disable caching for content files
    res.header("Cache-Control", "no-cache, no-store, must-revalidate");
    res.header("Pragma", "no-cache");
    res.header("Expires", 0);

    // rendering stuff here…
}

Update 3: So the complete code part is currently:

app.get('/:language(' + content.languageSelector + ')/:page', pageHandle);

function pageHandle (req, res)
{
    var language = req.params.language;
    var thisPage = content.getPage(req.params.page, language);

    if (thisPage)
    {
        // Disable caching for content files
        res.header("Cache-Control", "no-cache, no-store, must-revalidate");
        res.header("Pragma", "no-cache");
        res.header("Expires", 0);

        res.render(thisPage.file + '_' + language, {
            thisPage : thisPage,
            language: language,
            languages: content.languages,
            navigation: content.navigation,
            footerNavigation: content.footerNavigation,
            currentYear: new Date().getFullYear()
        });
    }
    else
    {
        error404Handling(req, res);
    }
}

解决方案

Easiest solution:

app.disable('etag');

Alternate solution here if you want more control:

http://vlasenko.org/2011/10/12/expressconnect-static-set-last-modified-to-now-to-avoid-304-not-modified/

这篇关于NodeJS / express:缓存和304状态码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆