Implement Server-Sent Events for real-time updates and enhance API route handling. Added loading state and last update indicator in the home page. Improved folder and post detail fetching logic in the admin page. Added webhook notification on file changes.
Some checks failed
Deploy / build-and-deploy (push) Failing after 2s
Some checks failed
Deploy / build-and-deploy (push) Failing after 2s
This commit is contained in:
@@ -91,11 +91,60 @@ export default function PostPage({ params }: { params: { slug: string[] } }) {
|
||||
// Initial load
|
||||
loadPost();
|
||||
|
||||
// Set up polling for changes
|
||||
const interval = setInterval(loadPost, 2000);
|
||||
// Set up Server-Sent Events for real-time updates (optional)
|
||||
let eventSource: EventSource | null = null;
|
||||
let fallbackInterval: NodeJS.Timeout | null = null;
|
||||
|
||||
const setupSSE = () => {
|
||||
try {
|
||||
eventSource = new EventSource('/api/posts/stream');
|
||||
|
||||
eventSource.onmessage = (event) => {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
if (data.type === 'update' && data.slug === slugPath) {
|
||||
loadPost();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error parsing SSE data:', error);
|
||||
}
|
||||
};
|
||||
|
||||
// Cleanup
|
||||
return () => clearInterval(interval);
|
||||
eventSource.onerror = (error) => {
|
||||
console.error('SSE connection error:', error);
|
||||
if (eventSource) {
|
||||
eventSource.close();
|
||||
eventSource = null;
|
||||
}
|
||||
// Fallback to minimal polling if SSE fails
|
||||
fallbackInterval = setInterval(loadPost, 30000); // 30 seconds
|
||||
};
|
||||
|
||||
eventSource.onopen = () => {
|
||||
console.log('SSE connection established');
|
||||
// Clear any fallback interval if SSE is working
|
||||
if (fallbackInterval) {
|
||||
clearInterval(fallbackInterval);
|
||||
fallbackInterval = null;
|
||||
}
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Failed to establish SSE connection:', error);
|
||||
// Fallback to minimal polling if SSE is not supported
|
||||
fallbackInterval = setInterval(loadPost, 30000); // 30 seconds
|
||||
}
|
||||
};
|
||||
|
||||
setupSSE();
|
||||
|
||||
return () => {
|
||||
if (eventSource) {
|
||||
eventSource.close();
|
||||
}
|
||||
if (fallbackInterval) {
|
||||
clearInterval(fallbackInterval);
|
||||
}
|
||||
};
|
||||
}, [slugPath]);
|
||||
|
||||
// Enhanced anchor scrolling logic
|
||||
|
||||
Reference in New Issue
Block a user