Skip to content

Commit 4127aa4

Browse files
Macaulay, DaneMacaulay, Dane
Macaulay, Dane
authored and
Macaulay, Dane
committed
add caching to static files
1 parent 6bc7cba commit 4127aa4

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

services/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from flask import Flask
22

3-
app = Flask(__name__, static_folder='../build', template_folder='../build')
3+
BUILD_DIR = 'build'
4+
build_path = '../{}'.format(BUILD_DIR)
5+
6+
app = Flask(__name__, static_folder=build_path)
7+
48
import services.static
59
import services.views
610

services/static.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
from services import app
2-
from flask import Flask, render_template
1+
from services import app, BUILD_DIR
2+
from flask import Flask, Response
3+
from werkzeug.datastructures import Headers
4+
5+
def load_index_file():
6+
index_file_path = '{}/index.html'.format(BUILD_DIR)
7+
with open(index_file_path, 'r', encoding='utf-8') as f:
8+
index_file = f.read()
9+
return index_file
10+
11+
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 31536000
12+
index_file = load_index_file()
313

414
@app.route('/', methods=['GET'])
515
def index():
6-
return render_template('index.html')
16+
return Response(index_file, mimetype='text/html',
17+
headers=Headers({'Cache-Control': 'max-age=60'}))
718

819
@app.route('/<path:path>')
920
def static_file(path):

0 commit comments

Comments
 (0)