During the development of my personal portfolio with Astro, I ran into an interesting challenge: how to efficiently integrate my Dev.to and Hashnode posts without having to rebuild the site every time I post new content?
The problem seemed simple at first: display all my posts from both platforms on a single page. However, I encountered several challenges:
I created a serverless endpoint in Astro that combines the posts from both platforms:
export const GET: APIRoute = async () => { const [hashnodePosts, devtoPosts] = await Promise.all([ getHashnodePosts(), getDevToPosts() ]); const allPosts = [...hashnodePosts, ...devtoPosts] .sort((a, b) => new Date(b.rawDate).getTime() - new Date(a.rawDate).getTime() ); return new Response(JSON.stringify(allPosts), { headers: { 'Content-Type': 'application/json', 'Cache-Control': 'no-store, no-cache, must-revalidate' } }); };
The key is to request the maximum number of posts possible:
// Para Dev.to const params = new URLSearchParams({ username: 'goaqidev', per_page: '1000', // Máximo número de posts state: 'published' }); // Para Hashnode const query = ` query { publication(host: "goaqidev.hashnode.dev") { posts(first: 1000) { // Máximo número de posts edges { node { title brief // ...otros campos } } } } } `;
To ensure fresh content, I implemented an anti-cache strategy:
const timestamp = new Date().getTime(); const response = await fetch(`/api/posts.json?_=${timestamp}`, { headers: { 'Cache-Control': 'no-cache', 'Pragma': 'no-cache' } });
To keep the interface up to date, I created a React component that handles loading and updating posts:
import { useState, useEffect } from 'react'; function BlogPosts() { const [posts, setPosts] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { const fetchPosts = async () => { try { const timestamp = new Date().getTime(); const response = await fetch(`/api/posts.json?_=${timestamp}`); const data = await response.json(); setPosts(data); } catch (error) { console.error('Error fetching posts:', error); } finally { setLoading(false); } }; fetchPosts(); // Actualizar cada 5 minutos const interval = setInterval(fetchPosts, 5 * 60 * 1000); return () => clearInterval(interval); }, []); if (loading) return <div>Cargando posts...</div>; return ( <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4"> {posts.map(post => ( <article key={post.id} className="card"> <h2>{post.title}</h2> <p>{post.brief}</p> <a href={post.url}>Leer más</a> </article> ))} </div> ); }
I implemented a robust error handling system:
async function fetchPosts() { try { const response = await fetch('/api/posts.json'); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const posts = await response.json(); return posts; } catch (error) { console.error('Error fetching posts:', error); // Intentar cargar desde caché local si está disponible const cachedPosts = localStorage.getItem('blog_posts'); return cachedPosts ? JSON.parse(cachedPosts) : []; } }
To further improve performance, I implemented:
// Guardar posts en localStorage localStorage.setItem('blog_posts', JSON.stringify(posts)); // Cargar posts desde localStorage mientras se actualiza const cachedPosts = localStorage.getItem('blog_posts'); if (cachedPosts) { setPosts(JSON.parse(cachedPosts)); }
function PostImage({ src, alt }) { return ( <img loading="lazy" src={src} alt={alt} className="w-full h-48 object-cover" /> ); }
This solution has proven to be robust and efficient, allowing me to:
I plan to implement:
The above is the detailed content of Optimizing Blog API Integration: Lessons Learned with Dev.to and Hashnode. For more information, please follow other related articles on the PHP Chinese website!