Cache provisional weekday timetables

This commit is contained in:
Edward Betts 2026-05-21 11:31:17 +01:00
parent 378d2484d0
commit bc7cb9cffa
6 changed files with 686 additions and 58 deletions

View file

@ -21,7 +21,7 @@
{% endif %}
</h2>
<p class="card-meta">
{{ travel_date_display }}{% if return_date %} to {{ return_date }}{% endif %}
{{ travel_date_display }}{% if return_date_display %} to {{ return_date_display }}{% endif %}
</p>
<div class="loading-panel" role="status" aria-live="polite">
<span class="spinner" aria-hidden="true"></span>
@ -37,6 +37,8 @@
<script>
(function() {
var attempts = 0;
function runScripts(root) {
root.querySelectorAll('script').forEach(function(oldScript) {
var script = document.createElement('script');
@ -49,27 +51,36 @@
});
}
fetch({{ full_results_url|tojson }}, {headers: {'X-Requested-With': 'fetch'}})
.then(function(response) {
if (!response.ok) throw new Error('Could not load results');
return response.text();
})
.then(function(html) {
var doc = new DOMParser().parseFromString(html, 'text/html');
document.title = doc.title;
var nextMain = doc.querySelector('main');
var currentMain = document.querySelector('main');
if (!nextMain || !currentMain) throw new Error('Results page was incomplete');
currentMain.innerHTML = nextMain.innerHTML;
runScripts(currentMain);
history.replaceState(null, '', window.location.href);
})
.catch(function() {
var panel = document.querySelector('.loading-panel');
if (panel) {
panel.innerHTML = '<div><strong>Could not load results</strong><p class="text-muted text-sm"><a href="{{ full_results_url }}">Try loading the full results page</a>.</p></div>';
}
});
function loadResults() {
attempts += 1;
fetch({{ full_results_url|tojson }}, {headers: {'X-Requested-With': 'fetch'}})
.then(function(response) {
if (!response.ok) throw new Error('Could not load results');
return response.text();
})
.then(function(html) {
var doc = new DOMParser().parseFromString(html, 'text/html');
document.title = doc.title;
var nextMain = doc.querySelector('main');
var currentMain = document.querySelector('main');
if (!nextMain || !currentMain) throw new Error('Results page was incomplete');
currentMain.innerHTML = nextMain.innerHTML;
runScripts(currentMain);
history.replaceState(null, '', window.location.href);
})
.catch(function() {
if (attempts < 3) {
window.setTimeout(loadResults, attempts * 2000);
return;
}
var panel = document.querySelector('.loading-panel');
if (panel) {
panel.innerHTML = '<div><strong>Could not load results</strong><p class="text-muted text-sm"><a href="{{ full_results_url }}">Try loading the full results page</a>.</p></div>';
}
});
}
loadResults();
})();
</script>