Skip to content
This repository has been archived by the owner on Sep 30, 2022. It is now read-only.

Commit

Permalink
* Make url_for subdomain serving be through an explicit template tag …
Browse files Browse the repository at this point in the history
…parameter, so we don't use random subdomains (as they do not provide performance improvements).
  • Loading branch information
dannya committed Jan 11, 2014
1 parent 132ef78 commit 6899731
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
25 changes: 17 additions & 8 deletions ext/flask/helpers.py
Expand Up @@ -290,6 +290,9 @@ def external_url_handler(error, endpoint, **values):
'the SERVER_NAME config variable.')
external = values.pop('_external', True)

# use a subdomain?
subdomain = values.pop('subdomain', None)

anchor = values.pop('_anchor', None)
method = values.pop('_method', None)
scheme = values.pop('_scheme', None)
Expand All @@ -314,21 +317,27 @@ def external_url_handler(error, endpoint, **values):
if anchor is not None:
rv += '#' + url_quote(anchor)


# make live environment modifications
if not current_app.config['DEBUG'] or current_app.config['LIVE']:
# use minified files
is_live = ('LIVE' in current_app.config) and current_app.config['LIVE']

if not current_app.config['DEBUG'] or is_live:
# use minified files?
if current_app.config['STATIC_MINIFY_FILENAME']:
for key, value in current_app.config['STATIC_MINIFY_FILENAME'].items():
rv = rv.replace(key, value)

# CDN
if not current_app.config['DEBUG'] and current_app.config['STATIC_CDN']:
from random import choice
# use a subdomain?
if subdomain:
if current_app.config['LIVE'] and current_app.config['DEBUG']:
url = current_app.config['LIVE'][:-1]
else:
url = request.url[:-1]

rv = request.url.replace(
rv = url.replace(
'://',
'://{0}.'.format(choice(current_app.config['STATIC_CDN'])),
)
'://{0}.'.format(subdomain),
) + rv

return rv

Expand Down
2 changes: 0 additions & 2 deletions init.py
Expand Up @@ -15,8 +15,6 @@


app = Flask(__name__)
app.config["LIVE"] = True
app.config["STATIC_CDN"] = ["s0", "s1", "s2", "s3", "s4", "s5"]
app.config["STATIC_MINIFY_FILENAME"] = {
".js": ".min.js",
".css": ".min.css",
Expand Down
6 changes: 3 additions & 3 deletions templates/base/structure.html
Expand Up @@ -8,7 +8,7 @@
<meta name="viewport" content="width=device-width" />
<meta name="google-site-verification" content="beTdAhx0raFrnhbZNxbSm063fSA6hDg5m81DHc9exU0" />

<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}" />
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css', subdomain='s0') }}" />
</head>

<body>
Expand All @@ -18,8 +18,8 @@

{% block footer %}{% endblock footer %}

<script src="{{ url_for('static', filename='js/shadowbox.js') }}"></script>
<script src="{{ url_for('static', filename='js/all.js') }}"></script>
<script src="{{ url_for('static', filename='js/shadowbox.js', subdomain='s1') }}"></script>
<script src="{{ url_for('static', filename='js/all.js', subdomain='s1') }}"></script>

<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
Expand Down
4 changes: 2 additions & 2 deletions templates/index.html
Expand Up @@ -142,8 +142,8 @@ <h2>
<ul>
{% for item in screenshots.elements %}
<li>
<a href="{{ url_for('static', filename='img/screenshots/%s/%s.png' % (screenshots.version, item.0)) }}" target="_blank">
<img src="{{ url_for('static', filename='img/screenshots/%s/thumbnails/%s.png' % (screenshots.version, item.0)) }}" alt="" width="300" />
<a href="{{ url_for('static', filename='img/screenshots/{0}/{1}.png'.format(screenshots.version, item.0), subdomain='s{0}'.format((loop.index % 2) + 2)) }}" target="_blank">
<img src="{{ url_for('static', filename='img/screenshots/{0}/thumbnails/{1}.png'.format(screenshots.version, item.0), subdomain='s{0}'.format((loop.index % 2) + 2)) }}" alt="" width="300" />
</a>

<h3>{{ item.1 }}</h3>
Expand Down

0 comments on commit 6899731

Please sign in to comment.