Skip to content
This repository was archived by the owner on Jun 22, 2025. It is now read-only.

Commit 605fea5

Browse files
authored
Merge pull request #102 from tarehart/disable-cache
Adding a disable_cache flag to send no-store response headers.
2 parents 106617a + 7e5e7a1 commit 605fea5

5 files changed

Lines changed: 47 additions & 12 deletions

File tree

eel/__init__.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
'close_callback': None, # Callback for when all windows have closed
4040
'app_mode': True, # (Chrome specific option)
4141
'all_interfaces': False, # Allow bottle server to listen for connections on all interfaces
42+
'disable_cache': True, # Sets the no-store response header when serving assets
4243
}
4344

4445
# == Temporary (suppressable) error message to inform users of breaking API change for v1.0.0 ===
@@ -49,7 +50,7 @@
4950
To suppress this error, add 'suppress_error=True' to start() call.
5051
This option will be removed in future versions
5152
----------------------------------------------------------------------------------
52-
'''
53+
'''
5354
# ===============================================================================================
5455

5556
# Public functions
@@ -72,7 +73,7 @@ def decorator(function):
7273
return function
7374

7475

75-
def init(path, allowed_extensions=['.js', '.html', '.txt', '.htm',
76+
def init(path, allowed_extensions=['.js', '.html', '.txt', '.htm',
7677
'.xhtml', '.vue']):
7778
global root_path, _js_functions
7879
root_path = _get_real_path(path)
@@ -113,7 +114,7 @@ def start(*start_urls, **kwargs):
113114
if _start_args['suppress_error']:
114115
_start_args.update(kwargs['options'])
115116
else:
116-
raise RuntimeError(api_error_message)
117+
raise RuntimeError(api_error_message)
117118

118119
if _start_args['port'] == 0:
119120
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@@ -124,12 +125,13 @@ def start(*start_urls, **kwargs):
124125
if _start_args['jinja_templates'] != None:
125126
from jinja2 import Environment, FileSystemLoader, select_autoescape
126127
templates_path = os.path.join(root_path, _start_args['jinja_templates'])
127-
_start_args['jinja_env'] = Environment(loader=FileSystemLoader(templates_path),
128-
autoescape=select_autoescape(['html', 'xml']))
128+
_start_args['jinja_env'] = Environment(loader=FileSystemLoader(templates_path),
129+
autoescape=select_autoescape(['html', 'xml']))
130+
129131

130132
# Launch the browser to the starting URLs
131133
show(*start_urls)
132-
134+
133135
def run_lambda():
134136
if _start_args['all_interfaces'] == True:
135137
HOST = '0.0.0.0'
@@ -163,7 +165,7 @@ def spawn(function, *args, **kwargs):
163165

164166
@btl.route('/eel.js')
165167
def _eel():
166-
start_geometry = {'default': {'size': _start_args['size'],
168+
start_geometry = {'default': {'size': _start_args['size'],
167169
'position': _start_args['position']},
168170
'pages': _start_args['geometry']}
169171

@@ -172,25 +174,31 @@ def _eel():
172174
page = page.replace('/** _start_geometry **/',
173175
'_start_geometry: %s,' % _safe_json(start_geometry))
174176
btl.response.content_type = 'application/javascript'
177+
_set_response_headers(btl.response)
175178
return page
176179

177180

178181
@btl.route('/<path:path>')
179182
def _static(path):
183+
response = None
180184
if 'jinja_env' in _start_args and 'jinja_templates' in _start_args:
181185
template_prefix = _start_args['jinja_templates'] + '/'
182186
if path.startswith(template_prefix):
183187
n = len(template_prefix)
184188
template = _start_args['jinja_env'].get_template(path[n:])
185-
return template.render()
189+
response = btl.HTTPResponse(template.render())
190+
191+
if response is None:
192+
response = btl.static_file(path, root=root_path)
193+
194+
_set_response_headers(response)
195+
return response
186196

187-
return btl.static_file(path, root=root_path)
188-
189197

190198
@btl.get('/eel', apply=[wbs.websocket])
191199
def _websocket(ws):
192200
global _websockets
193-
201+
194202
for js_function in _js_functions:
195203
_import_js_function(js_function)
196204

@@ -232,7 +240,7 @@ def _process_message(message, ws):
232240
if 'call' in message:
233241
return_val = _exposed_functions[message['name']](*message['args'])
234242
_repeated_send(ws, _safe_json({ 'return': message['call'],
235-
'value': return_val }))
243+
'value': return_val }))
236244
elif 'return' in message:
237245
call_id = message['return']
238246
if call_id in _call_return_callbacks:
@@ -312,3 +320,8 @@ def _websocket_close(page):
312320
if len(_websockets) == 0:
313321
sys.exit()
314322

323+
324+
def _set_response_headers(response):
325+
if _start_args['disable_cache']:
326+
# https://stackoverflow.com/a/24748094/280852
327+
response.set_header('Cache-Control', 'no-store')
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import eel
2+
3+
# Set web files folder and optionally specify which file types to check for eel.expose()
4+
eel.init('web')
5+
6+
# disable_cache now defaults to True so this isn't strictly necessary. Set it to False to enable caching.
7+
eel.start('disable_cache.html', size=(300, 200), disable_cache=True) # Start
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Hello, World!</title>
5+
6+
<!-- Include eel.js - note this file doesn't exist in the 'web' directory -->
7+
<script type="text/javascript" src="/eel.js"></script>
8+
<script type="text/javascript" src="dont_cache_me.js"></script>
9+
</head>
10+
11+
<body>
12+
Cache Proof!
13+
</body>
14+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Check the network activity to see that this file isn't getting cached.");
14.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)