在CDN中提供静态文件,而不是在生产中使用Flask [英] Serve static files from a CDN rather than Flask in production

查看:123
本文介绍了在CDN中提供静态文件,而不是在生产中使用Flask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Flask应用程序中,我通过开发环境中的应用程序提供静态资产,但是我想在生产中使用CDN。每个资产都被加载到一个名为 base.hmtl 的模板中,所以我猜最简单的解决方案是将一个变量传递给渲染函数,并在模板中使用它: p>

 < script src ={{STATIC_URL}} / js / main.js>< / script> 

正常情况下,它将是dev env中的空字符串,以及生产中的cdn url。我想避免将这个 STATIC_URL 变量传递给每个视图。我可以使它与

  @ bp.context_processor 
def set_static_path():
return dict STATIC_URL ='https://foo.bar.com')

但对我来说这似乎有点哈克。有没有更好的方法来解决这个问题?

解决方案

没有必要改变你如何链接到静态文件,你仍然可以使用 url_for('static',filename ='myfile.txt')。将默认的静态视图替换为重定向到CDN的静态视图。

  from urllib.parse import urljoin 
#or for python 2:from urlparse import urljoin $ b $ from flask import redirect
$ b @ app.endpoint('static')
def static(filename):
static_url = app.config.get('STATIC_URL')

if static_url:
return redirect(urljoin(static_url,filename))

return app.send_static_file文件名)

无论您是在开发机器还是生产环境,都要将






重定向(STORE_URL)配置了CDN的值,并且静态文件的请求将被重定向到那里。是相对便宜,并被浏览器记住。如果你的性能受到了他们的影响,你可以写一个直接链接到CDN的函数。

  @ app.template_global()
def static_url(filename):
static_url = app.config.get('STATIC_URL')

if static_url:
return urljoin (static_url,filename)

return url_for('static',filename = filename)



template_global 装饰器使所有模板都可用。使用它而不是 url_for 当你需要静态文件的url时。


In my Flask app I serve the static assets through the app in the dev env, but I'd like to use a CDN in production. Every asset is loaded in a template called base.hmtl, so I guess the easiest solution is to pass a variable to the render function and use it in the template like:

<script src="{{ STATIC_URL }}/js/main.js"></script>

Normally it would be an empty string in the dev env, and the cdn url in production. I'd like to avoid passing this STATIC_URL variable to every view. I could make it work with

@bp.context_processor
def set_static_path():
    return dict(STATIC_URL='https://foo.bar.com')

But for me this seems a little hacky. Is there a better way to solve this problem?

解决方案

There's no need to change how you link to static files, you can still use url_for('static', filename='myfile.txt'). Replace the default static view with one that redirects to the CDN if it is configured.

from urllib.parse import urljoin
# or for python 2: from urlparse import urljoin
from flask import redirect

@app.endpoint('static')
def static(filename):
    static_url = app.config.get('STATIC_URL')

    if static_url:
        return redirect(urljoin(static_url, filename))

    return app.send_static_file(filename)

Whether you're on a dev machine or production, set the STATIC_URL config value to the CDN and requests for static files will be redirected there.


Redirects are relatively cheap, and are remembered by browsers. If you get to the point where performance is meaningfully affected by them, you can write a function that links directly when using the CDN.

@app.template_global()
def static_url(filename):
    static_url = app.config.get('STATIC_URL')

    if static_url:
        return urljoin(static_url, filename)

    return url_for('static', filename=filename)

The template_global decorator makes the function available in all templates. Use it instead of url_for when you need urls for static files.

这篇关于在CDN中提供静态文件,而不是在生产中使用Flask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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