|
132 | 132 | {% if udf %}
|
133 | 133 | <script>
|
134 | 134 | document.addEventListener('DOMContentLoaded', function() {
|
135 |
| - function fetchAndUpdateStatus() { |
136 |
| - // Get cached values |
137 |
| - let cachedSiteName = getCookie('siteNameCache'); |
138 |
| - let cachedState = getCookie('stateCache'); |
139 |
| - |
140 |
| - // Update the page with cached values if they exist |
141 |
| - if (cachedSiteName && cachedState) { |
142 |
| - document.getElementById('siteName').textContent = cachedSiteName; |
143 |
| - document.getElementById('siteState').textContent = cachedState; |
144 |
| - } else { |
145 |
| - // Fetch from server if no valid cache exists |
146 |
| - fetch('/_ce_status') |
147 |
| - .then(response => { |
148 |
| - if (!response.ok) { |
149 |
| - throw new Error('Network response was not ok'); |
150 |
| - } |
151 |
| - return response.json(); |
152 |
| - }) |
153 |
| - .then(data => { |
154 |
| - if (!data.err) { |
155 |
| - document.getElementById('siteName').textContent = cachedSiteName; |
156 |
| - document.getElementById('siteState').textContent = cachedState; |
157 |
| - // Cache the site name and state in cookies for 1 day |
158 |
| - setCookie('siteNameCache', data.site_name, 1); |
159 |
| - setCookie('stateCache', data.state, 1); |
160 |
| - } else { |
161 |
| - // Handle potential errors in data retrieval |
162 |
| - document.getElementById('statusText').textContent = 'Error fetching status'; |
163 |
| - } |
164 |
| - }) |
165 |
| - .catch(error => { |
166 |
| - console.error('There was a problem with the fetch operation:', error); |
167 |
| - document.getElementById('siteName').textContent = `Error`; |
168 |
| - document.getElementById('siteState').textContent = `Error`; |
169 |
| - }); |
170 |
| - } |
171 |
| - } |
172 |
| - |
173 |
| - // Initial fetch on page load |
174 |
| - fetchAndUpdateStatus(); |
175 |
| - |
176 |
| - // Re-fetch every 20 seconds |
177 |
| - setInterval(() => { |
178 |
| - // Expire the cookie explicitly by setting days to 0 |
179 |
| - setCookie('siteNameCache', '', 0); |
180 |
| - setCookie('stateCache', '', 0); |
181 |
| - fetchAndUpdateStatus(); |
182 |
| - }, 20000); |
| 135 | + function fetchAndUpdateStatus() { |
| 136 | + fetch('/_ce_status') |
| 137 | + .then(response => { |
| 138 | + if (!response.ok) { |
| 139 | + throw new Error('Network response was not ok'); |
| 140 | + } |
| 141 | + return response.json(); |
| 142 | + }) |
| 143 | + .then(data => { |
| 144 | + if (!data.err) { |
| 145 | + document.getElementById('statusText').textContent = `Site Name: ${data.site_name}, State: ${data.state}`; |
| 146 | + |
| 147 | + // Expire the old cookies just before setting new ones |
| 148 | + setCookie('siteNameCache', '', 0); |
| 149 | + setCookie('stateCache', '', 0); |
| 150 | + |
| 151 | + // Set new cookies |
| 152 | + setCookie('siteNameCache', data.site_name, 1); |
| 153 | + setCookie('stateCache', data.state, 1); |
| 154 | + } else { |
| 155 | + // Handle potential errors in data retrieval |
| 156 | + document.getElementById('statusText').textContent = 'Error fetching status'; |
| 157 | + } |
| 158 | + }) |
| 159 | + .catch(error => { |
| 160 | + console.error('There was a problem with the fetch operation:', error); |
| 161 | + document.getElementById('statusText').textContent = 'Error'; |
| 162 | + }); |
| 163 | + } |
| 164 | + |
| 165 | + // Initial fetch on page load |
| 166 | + fetchAndUpdateStatus(); |
| 167 | + |
| 168 | + // Re-fetch every 20 seconds |
| 169 | + setInterval(fetchAndUpdateStatus, 20000); |
183 | 170 | });
|
184 | 171 | </script>
|
185 | 172 |
|
|
0 commit comments