浏览器缓存和HTML / JSON路由的历史 [英] Browser caching and history with HTML/JSON route

查看:324
本文介绍了浏览器缓存和HTML / JSON路由的历史的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中有一个路径,只有一个URL根据请求中的Accepts标头做出不同的响应。这个想法是,如果在浏览器中输入URL,将会呈现一个HTML模板。如果我的客户端JavaScript请求的URL,JSON将被返回。这里是一个简单的例子(Python / Flask):

 #From http://flask.pocoo.org/snippets/45 / 
def requested_json():
'''确定是否请求了JSON。 '''
best = request.accept_mimetypes.best_match([MIME_JSON,MIME_HTML])
return best == MIME_JSON and request.accept_mimetypes [best]> request.accept_mimetypes [MIME_HTML]

@ app.route('/ assembly /< assembly_serial_no>,methods = ['GET'])
def assembly_get(assembly_serial_no):$ b $用于获取装配单元细节的HTML / JSON路线。 '''
assembly_model_id = g.pcb_db.get_assembly_model_id_for(assembly_serial_no)
如果不是assembly_model_id:
abort(404)
assembly_unit = g.pcb_db.get_assembly(assembly_serial_no)
如果requested_json():
返回jsonify(assembly_unit)
else:
return render_template('assembly.html',
assembly_unit = assembly_unit,

这个工作很好,直到遇到缓存/浏览器历史记录。如果用户以前在浏览器中加载了一个URL,但是最近对该URL的请求是由带有Accept:application / json的Javascript完成的,如果使用后退按钮返回到该URL,则浏览器具有JSON版本缓存,并显示,而不是HTML版本。

我可以区分JSON和HTML网址(例如,追加?json = True),但如果有更好的正确使用HTTP标头的方法,我想知道它是什么。

解决方案

您应该能够包括Vary:Accept头。



根据 http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.44


Vary字段值表示
完全确定的请求头字段的集合,而响应是新鲜的,缓存是否允许使用
回复后续请求,无需
重新验证



I have a route in my application with a single URL which responds differently based on the Accepts header in the request. The idea is that if the URL is entered in a browser, an HTML template will be rendered. If my client side Javascript requests that URL, JSON will be returned. Here is a simple example (Python / Flask):

# From http://flask.pocoo.org/snippets/45/
def requested_json():
    ''' Determine whether JSON was requested. '''
    best = request.accept_mimetypes.best_match([MIME_JSON, MIME_HTML])
    return best == MIME_JSON and request.accept_mimetypes[best] > request.accept_mimetypes[MIME_HTML]

@app.route('/assembly/<assembly_serial_no>', methods=['GET'])
def assembly_get(assembly_serial_no):
    ''' HTML/JSON route for getting the details of an assembly unit. '''
    assembly_model_id = g.pcb_db.get_assembly_model_id_for(assembly_serial_no)
    if not assembly_model_id:
        abort(404)
    assembly_unit = g.pcb_db.get_assembly(assembly_serial_no)        
    if requested_json():
        return jsonify(assembly_unit)
    else:
        return render_template('assembly.html',
            assembly_unit=assembly_unit,
        )

This has been working nicely, until I ran into a hitch with caching / browser history. If the user has loaded a URL in their browser previously, but the most recent request to that URL was done by the Javascript with Accept: application/json, if the back button is used to return to that URL, the browser has the JSON version cached, and displays that instead of the HTML version.

I could differentiate the JSON and HTML URLs (eg, append ?json=True), but if there is a better way to do this by correctly using HTTP headers, I'd like to know what it is.

解决方案

You should be able to include the Vary : Accept header for this.

According to http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.44:

The Vary field value indicates the set of request-header fields that fully determines, while the response is fresh, whether a cache is permitted to use the response to reply to a subsequent request without revalidation

这篇关于浏览器缓存和HTML / JSON路由的历史的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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