Skip to content

Commit dfadada

Browse files
authored
Evict invalid stylesheets from cache (#7929)
1 parent 23b61ea commit dfadada

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

panel/reactive.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -224,16 +224,16 @@ def _process_param_change(self, msg: dict[str, Any]) -> dict[str, Any]:
224224
]
225225
stylesheets += properties['stylesheets']
226226
wrapped = []
227+
if state.curdoc:
228+
css_cache = state._stylesheets.get(state.curdoc, {})
229+
else:
230+
css_cache = {}
227231
for stylesheet in stylesheets:
228232
if isinstance(stylesheet, str) and (stylesheet.split('?')[0].endswith('.css') or stylesheet.startswith('http')):
229-
if state.curdoc:
230-
cache = state._stylesheets.get(state.curdoc, {})
231-
else:
232-
cache = {}
233-
if stylesheet in cache:
234-
stylesheet = cache[stylesheet]
233+
if stylesheet in css_cache:
234+
stylesheet = css_cache[stylesheet]
235235
else:
236-
cache[stylesheet] = stylesheet = ImportedStyleSheet(url=stylesheet)
236+
css_cache[stylesheet] = stylesheet = ImportedStyleSheet(url=stylesheet)
237237
wrapped.append(stylesheet)
238238
properties['stylesheets'] = wrapped
239239
return properties
@@ -671,9 +671,9 @@ def _get_properties(self, doc: Document | None) -> dict[str, Any]:
671671
if 'stylesheets' not in properties:
672672
return properties
673673
if doc:
674-
state._stylesheets[doc] = cache = state._stylesheets.get(doc, {})
674+
state._stylesheets[doc] = css_cache = state._stylesheets.get(doc, {})
675675
else:
676-
cache = {}
676+
css_cache = {}
677677
if doc and 'dist_url' in doc._template_variables:
678678
dist_url = doc._template_variables['dist_url']
679679
else:
@@ -682,10 +682,20 @@ def _get_properties(self, doc: Document | None) -> dict[str, Any]:
682682
for stylesheet in properties['stylesheets']:
683683
if isinstance(stylesheet, ImportedStyleSheet):
684684
url = str(stylesheet.url)
685-
if url in cache:
686-
stylesheet = cache[url]
685+
if url in css_cache:
686+
cached = css_cache[url]
687+
# Confirm if stylesheet is valid, sometimes
688+
# the URL is seemingly set to None so we
689+
# replace the cached stylesheet if there is
690+
# a unset property error
691+
try:
692+
cached.url # noqa
693+
except Exception:
694+
css_cache[url] = stylesheet
695+
else:
696+
stylesheet = cached
687697
else:
688-
cache[url] = stylesheet
698+
css_cache[url] = stylesheet
689699
patch_stylesheet(stylesheet, dist_url)
690700
stylesheets.append(stylesheet)
691701
properties['stylesheets'] = stylesheets

0 commit comments

Comments
 (0)