Category: Web Performance & SEO

  • Fixing Client-Side React SEO with Puppeteer and react-helmet-async

    Single-page React apps have a well-known SEO problem. A crawler that doesn’t execute JavaScript hits your site and sees this:

    <div id="root"></div>
    

    Nothing else. No title, no description, no content. Everything a search engine or a link-unfurling bot cares about only exists after React mounts, runs its components, and renders into that div. Some crawlers do execute JS now, but not reliably, not consistently, and not for every route on your site with the same timing guarantees. If SEO matters to you, you can’t leave that to chance.

    I ran into this directly while working on my portfolio site, guicoder.com. Here’s the two-part fix: react-helmet-async to manage per-route metadata, and a custom Puppeteer prerendering script to bake that metadata into static HTML at build time.

    Part one: per-route metadata with react-helmet-async

    By default, a React SPA has one <title> and one set of meta tags defined once in public/index.html. Every route shares them. That’s a problem if you want each page to describe itself accurately: your resume page and your samples page shouldn’t have identical titles and descriptions.

    react-helmet-async solves this by letting each route declare its own head content as part of its component tree. A page component might render something like:

    <Seo
      title="Samples | Guicoder"
      description="A collection of accessible React components and applications."
      canonical="https://guicoder.com/samples"
    />
    

    Internally, that component uses react-helmet-async‘s <Helmet> to inject the title, description, canonical link, and Open Graph tags into <head> at runtime. Navigate to a different route, and the tags update to match.

    This is a real improvement, but it only exists after JavaScript runs. It’s a runtime fix. If a crawler doesn’t execute your JS, or gives up before helmet’s effect fires, none of it matters.

    Part two: capturing the rendered output with Puppeteer

    This is where prerendering comes in. The idea: instead of hoping a crawler will run your JS correctly, run it yourself, once, at build time, and save the result as static HTML.

    The rough shape of the process:

    1. Build the app normally.
    2. Spin up a local static server serving the build output.
    3. Launch a headless browser (Puppeteer) and visit each route.
    4. Wait for React to mount and helmet to inject its tags.
    5. Grab the fully-rendered HTML from the page.
    6. Write that HTML to disk at the matching route path.

    The result is a set of static files like build/samples/index.html, build/resume/index.html, each containing the correct title, description, canonical URL, and OG tags for that specific route, already present in the markup a crawler receives on first load. Once the page’s own JS kicks in, React hydrates over that markup and the app behaves like a normal SPA from there.

    The part that’s easy to get wrong: timing

    The tricky detail is step 4. React mounting and helmet committing its tags don’t happen instantly, and they don’t happen in lockstep with typical “page ready” signals like Puppeteer’s networkidle0. If you snapshot too early, you capture the DOM before helmet has written anything route-specific, and every one of your prerendered pages ends up with the same default metadata baked in. That defeats the entire point.

    The fix is to wait for a specific, verifiable signal rather than a generic timeout. Checking for any non-empty meta description tag isn’t reliable if a static default value already exists in your HTML template; that check can pass before the route-specific value ever loads. A more precise approach is to wait for the canonical link tag to match the exact URL expected for that route:

    await page.waitForFunction(
        (expectedPath) => {
          const canonical = document.querySelector('link[rel="canonical"]');
          return !!canonical && canonical.href.endsWith(expectedPath);
        },
        {timeout: 10000},
        route,
    );
    

    This only proceeds once the canonical tag actually reflects the current route, which means helmet has committed its changes and the snapshot will be accurate. Only after that check passes does the script call page.content() and write the file.

    Why not just use an existing tool

    Tools like react-snap package this whole workflow, using Puppeteer under the hood with generic heuristics for detecting when a page is ready. They work fine for simple cases, but the heuristics aren’t aware of your app’s specific rendering pipeline or of exactly when helmet commits its changes. That mismatch is precisely the class of bug described above, and it’s easier to introduce than to catch. Writing the prerender script by hand costs more upfront, but it means the “is this page actually ready” check is built around something you control and can verify directly, rather than a guess made by a generic tool.

    The result

    Two pieces working together solve the problem neither solves alone:

    • react-helmet-async gives each route control over its own metadata at runtime.
    • Puppeteer prerendering captures that metadata into static HTML at build time, so it’s present before any JavaScript runs.

    A crawler hitting any route on the site now gets a complete, accurate <head> on the very first response. No JS execution required, no waiting, no guessing.

  • Modernizing SEO for React Portfolio Sites

    Dynamic Title and Meta Tags

    As a web developer, your portfolio site is often the first impression potential clients have of your work. But what good is a beautiful React portfolio if no one can find it? Recently, I upgraded my portfolio’s SEO implementation and learned some valuable lessons about dynamic meta tag management in client-side React applications.

    The Old Approach: Static HTML Limitations

    My original setup was basic:

    • Static index.html with generic meta tags
    • Same title and description for every page
    • No social media optimization
    • Limited search engine visibility

    This approach works for simple sites, but portfolio sites need page-specific SEO to properly showcase different sections like resume, projects, and services.

    React Helmet: The Deprecated Solution

    Initially, I considered React Helmet, the traditional go-to library for dynamic meta tags. However, research revealed it’s deprecated and no longer maintained. Many tutorials still recommend it without mentioning better alternatives exist.

    React 19 Document Metadata: Promising but Limited

    React 19 introduced native support for dynamic document metadata – the ability to render <title>, <meta>, and <link> tags anywhere in your component tree with React automatically hoisting them to the document head.

    The Problem: This feature requires server-side rendering to work in production. For client-side React apps, the meta tags remain in the component body instead of being moved to the document head, providing zero SEO benefit.

    The Real Solution: react-helmet-async

    For client-side React applications, react-helmet-async is the current best practice. It’s actively maintained and properly handles dynamic meta tags in production builds.

    Installation

    npm install react-helmet-async
    

    Note: If you’re using React 19, you may need to force install:

    npm install react-helmet-async --legacy-peer-deps
    

    Setup

    Wrap your app with the HelmetProvider:

    // main.tsx
    import {HelmetProvider} from 'react-helmet-async';
    
    createRoot(document.getElementById('root')!).render(
      <HelmetProvider>
        <App />
      </HelmetProvider>
    );
    

    Implementation

    Create a reusable SEO component:

    import {useLocation} from 'react-router-dom';
    import {Helmet} from 'react-helmet-async';
    
    const seoData = {
      '/': {
        title: 'Alan Werstler - Web Developer | Portfolio & Resume',
        description: 'Experienced React developer specializing in front-end development...',
        keywords: 'Alan Werstler, web developer, React developer, Seattle'
      },
      '/resume': {
        title: 'Resume - Alan Werstler | Web Developer',
        description: 'View my technical skills and professional experience...',
        keywords: 'Alan Werstler resume, web developer experience'
      }
      // Additional pages...
    };
    
    export function SEO() {
      const location = useLocation();
      const seo = seoData[location.pathname] || seoData['/'];
      
      return (
        <Helmet>
          <title>{seo.title}</title>
          <meta name="description" content={seo.description} />
          <meta name="keywords" content={seo.keywords} />
          
          {/* Open Graph for social sharing */}
          <meta property="og:title" content={seo.title} />
          <meta property="og:description" content={seo.description} />
          <meta property="og:url" content={`https://yourdomain.com${location.pathname}`} />
          
          {/* Structured data for search engines */}
          <script type="application/ld+json">
            {JSON.stringify({
              "@context": "https://schema.org",
              "@type": "Person",
              "name": "Your Name",
              "jobTitle": "Web Developer"
            })}
          </script>
        </Helmet>
      );
    }
    

    Add to your App component:

    function App() {
      return (
        <div>
          <SEO />
          <Routes>
            {/* your routes */}
          </Routes>
        </div>
      );
    }
    

    Troubleshooting: When Updates Don’t Work

    Some setups may experience issues where meta tags don’t update on route changes. If this happens, try forcing a re-render:

    import {useState, useEffect} from 'react';
    
    export function SEO() {
      const location = useLocation();
      const [key, setKey] = useState(0);
      
      useEffect(() => {
        setKey(prev => prev + 1);
      }, [location.pathname]);
    
      return (
        <Helmet key={key}>
          {/* your meta tags */}
        </Helmet>
      );
    }
    

    Comprehensive SEO Strategy

    The complete implementation includes:

    Dynamic Meta Tags

    • Route-specific titles and descriptions
    • Relevant keywords per page
    • Proper canonical URLs

    Social Media Optimization

    • Open Graph tags for Facebook/LinkedIn
    • Twitter Cards for rich previews
    • Professional profile images

    Structured Data

    • JSON-LD markup for search engines
    • Person/Organization schema
    • Local business data for geographic targeting

    Fallback Support

    • Enhanced index.html for JavaScript-disabled users
    • Progressive enhancement approach

    Enhanced index.html Fallbacks

    Keep comprehensive fallback meta tags in your public/index.html:

    <title>Your Name - Web Developer | Portfolio & Resume</title>
    <meta name="description" content="Fallback description for JavaScript-disabled users">
    <meta property="og:title" content="Your Portfolio">
    <meta property="og:description" content="Professional web developer portfolio">
    

    Results and Benefits

    After implementation:

    • Each page has targeted SEO metadata
    • Social sharing shows professional previews
    • Search engines properly index different sections
    • Better local SEO for geographic searches
    • Improved click-through rates from search results

    Key Takeaways

    1. Avoid React 19’s native metadata for client-side apps – it doesn’t work in production
    2. Use react-helmet-async as the current best practice
    3. Implement comprehensive fallbacks in your static HTML
    4. Test social sharing with Facebook/Twitter debugging tools
    5. Add structured data for rich search results
    6. Force re-renders if needed for problematic setups

    react-helmet-async Limitations

    While react-helmet-async works well, it has some limitations:

    • Still requires JavaScript execution (not ideal for all crawlers)
    • Social media scrapers may not execute JavaScript
    • Performance overhead from additional library
    • Potential synchronization issues in complex apps

    Alternative Approach: Per-Page Components

    For simple sites, consider placing Helmet directly in each page component:

    function HomePage() {
      return (
        <>
          <Helmet>
            <title>Home - Your Portfolio</title>
            <meta name="description" content="Welcome to my portfolio" />
          </Helmet>
          {/* page content */}
        </>
      );
    }
    

    This approach can be more reliable for route-based updates.

    Conclusion

    Dynamic SEO for client-side React applications requires careful tool selection. While React 19’s native document metadata looks promising, it’s only viable for SSR applications. For client-side portfolios, react-helmet-async remains the best solution, providing reliable meta tag management with proper fallback strategies.

    The investment in proper SEO implementation pays dividends in discoverability and professional presentation. Your portfolio represents your technical skills – make sure search engines and social media platforms can showcase it properly.