playground.shiny.space/ajax.html
Lucas 7a4fa4b312 Fix dark mode flash by inlining critical script
- Move dark mode detection to inline script in HTML head
- Prevents white flash in Firefox by applying theme before render
- Clean up redundant code in dark-mode.js
- Apply fix to all HTML pages for consistent behavior

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-06-14 04:09:18 +02:00

91 lines
No EOL
3.6 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AJAX Demo</title>
<link rel="stylesheet" href="styles.css">
<script>
// Apply dark mode immediately to prevent flash
if (localStorage.getItem('theme') === 'dark') {
document.documentElement.setAttribute('data-theme', 'dark');
}
</script>
</head>
<body>
<button class="dark-mode-toggle" onclick="toggleDarkMode()">🌙</button>
<h1>AJAX Demo</h1>
<p>This page demonstrates asynchronous loading. The UI loads immediately, then makes a slow API call in the background.</p>
<div class="section">
<h3>Slow Response Test</h3>
<p>Click the button to make a request to the slow endpoint (1 second delay):</p>
<button id="loadBtn" onclick="loadSlowResponse()">Load Slow Response</button>
<button id="clearBtn" onclick="clearResponse()">Clear</button>
<div id="responseField" class="response-field centered">
<span class="loading">Loading...</span>
</div>
<div id="timestamp" class="timestamp"></div>
<div class="info">
The page loads instantly and automatically starts loading the slow response. The API response takes ~1 second to arrive.
</div>
</div>
<div class="back-link">
<a href="index.html">← Back to Home</a>
</div>
<script>
let requestStartTime;
async function loadSlowResponse() {
const responseField = document.getElementById('responseField');
const loadBtn = document.getElementById('loadBtn');
const timestampDiv = document.getElementById('timestamp');
// Disable button and show loading state
loadBtn.disabled = true;
responseField.innerHTML = '<span class="loading">Loading... (this takes ~1 second)</span>';
requestStartTime = Date.now();
timestampDiv.innerHTML = `Request started at: ${new Date().toLocaleTimeString()}`;
try {
const response = await fetch('/api/slowtest');
const data = await response.text();
const elapsed = Date.now() - requestStartTime;
responseField.innerHTML = `<span class="response large">Response: ${data}</span>`;
timestampDiv.innerHTML = `Request completed at: ${new Date().toLocaleTimeString()} (took ${elapsed}ms)`;
} catch (error) {
responseField.innerHTML = '<span class="error">Error loading response</span>';
timestampDiv.innerHTML = `Error at: ${new Date().toLocaleTimeString()}`;
console.error('Error:', error);
} finally {
loadBtn.disabled = false;
}
}
function clearResponse() {
const responseField = document.getElementById('responseField');
const timestampDiv = document.getElementById('timestamp');
responseField.innerHTML = '<span class="loading">Click "Load Slow Response" to test the API</span>';
timestampDiv.innerHTML = '';
}
// Show that the page loaded immediately and start the slow request
document.addEventListener('DOMContentLoaded', function() {
console.log('Page loaded immediately at:', new Date().toLocaleTimeString());
loadSlowResponse();
});
</script>
<script src="dark-mode.js"></script>
</body>
</html>