Skip to content

Dynamic Rendering for AI Bots: A Guide to Serving Complex JS Structures as Raw HTML

JavaScript kodları ve temiz HTML yapısının AI botu tarafından taranmasını temsil eden teknik illüstrasyon.

Imagine you have a modern React, Vue, or Angular application. Your site offers a seamless experience for users; however, when OpenAI's GPTBot or Perplexity's crawlers visit your site, they may not have the 'execution budget' required to run all your JavaScript files. The result? The AI bot encounters an empty <div id="app"></div> tag and cannot index your content. This significantly reduces the likelihood of your site appearing in AI-generated answers (AI Overviews).

The Short Answer: What is Dynamic Rendering for AI?

Dynamic rendering for AI is the process of checking the User-Agent information of an incoming request on the server side to distinguish whether the request is coming from an AI bot or a real user. If the request is from an AI bot, the server serves the raw HTML (static HTML) output generated by executing these files on its own (or via an intermediate layer) instead of sending complex JavaScript files. Users, meanwhile, continue to see the standard, interactive client-side application. This method maximizes content accuracy while reducing the bot's crawling cost to zero.

The JavaScript Dilemma of AI Bots

Although Googlebot has had JavaScript execution capabilities for years, next-generation AI bots (e.g., OAI-SearchBot, ClaudeBot) do not always have the same capacity. An AI bot must make sense of thousands of pages within seconds while crawling your site. JavaScript is a heavy operation; it consumes CPU and memory. Most AI bots focus on raw text using simple HTTP clients rather than using a 'headless browser'. Dynamic rendering ensures you provide these bots with exactly what they want: processed and readable text.

Technique 1: Using Middleware and Headless Browsers (Rendertron/Puppeteer)

This technique involves placing a 'render layer' between your server and the bot. Rendertron, one of the most popular tools (a Chrome-based rendering solution), intercepts the incoming request. If the User-Agent is an AI bot, Rendertron opens the page internally, executes the JavaScript, and caches the final HTML to deliver it to the bot.

The critical point to note here is the Vary: User-Agent HTTP header. This header informs CDNs and caching systems that the content may vary based on the type of browser requesting it. If you do not use this header, static HTML generated for a bot might be accidentally served to a real user, and your site's interactive features (buttons, forms) might not work.

Technique 2: Edge Computing and Instant Transformation with Cloudflare Workers

To reduce server load and minimize Time to First Byte (TTFB), you can perform dynamic rendering at the 'Edge'. Technologies like Cloudflare Workers intercept the request before it even reaches your server. It detects bots with a simple logical check.

For example, a Worker script checks the incoming request.headers.get('User-Agent') information. If it contains 'GPTBot' or 'PerplexityBot', it redirects the request to a pre-generated S3 bucket or a prerender service. This method allows you to serve HTML to AI bots at lightning speed without increasing your main server's CPU load. This process performed at the Edge usually adds a latency of 10-50ms, which is much faster than the bot trying to resolve the JavaScript on its own.

Technique 3: Hybrid SSR and Conditional Hydration

If you are using modern frameworks like Next.js or Nuxt.js, you can make dynamic rendering a part of your application architecture. In the Hybrid SSR approach, Server-Side Rendering (SSR) is performed for bots, while 'Static Site Generation' (SSG) or 'Client-Side Rendering' (CSR) can be preferred for users.

However, one must be careful about the risk of 'Cloaking'. The HTML content you provide to the AI bot must be semantically identical to the content you provide to the user. Only the technical presentation format (JS vs. HTML) should differ. If you serve different content to the bot than to the user, this can lead to your site being flagged as 'spam' in terms of both traditional SEO and AI reliability.

User-Agent Management: Which Bots Should You Target?

The core AI and search engine bots you should target in your dynamic rendering architecture are:

  • GPTBot & OAI-SearchBot: OpenAI's data collection and search agents.
  • PerplexityBot: Perplexity AI's real-time search engine bot.
  • ClaudeBot: Anthropic's data collection agent.
  • CCBot: Common Crawl (the foundational training data for many AI models).
  • Google-InspectionTool: Google's modern rendering test tool.

Technical Checklist

  • Create a regex library containing the bot list or use an up-to-date list (e.g., the isbot npm package).
  • Ensure the Vary: User-Agent header is sent on the server side.
  • Clean up <script> tags in the rendered HTML output to prevent the bot from re-executing JS.
  • Verify that loading="lazy" attributes in <img> tags do not cause issues for bots (i.e., ensure content does not arrive empty).
  • Optimize caching duration based on the crawl frequency of AI bots (usually 24-48 hours).
  • Check the size of the static HTML output; HTML files over 10MB may be truncated by some bots.
  • Verify that canonical tags are identical in both the JS and HTML versions.

Example Scenario: AI Bot Redirection on Nginx

The following Nginx configuration example checks if the incoming request is an AI bot and redirects the request to a render service (e.g., a Rendertron instance running on port 3000):

map $http_user_agent $is_ai_bot {
  default 0;
  "~*GPTBot|OAI-SearchBot|PerplexityBot|ClaudeBot|Applebot-Extended" 1;
}

server {
  listen 80;
  server_name your-site.com;

  location / {
    if ($is_ai_bot) {
      rewrite ^(.*)$ /render/$scheme://$host$1 break;
      proxy_pass http://localhost:3000;
    }
    try_files $uri $uri/ /index.html;
  }
}

In this setup, while normal users load the JS application via index.html, the identified AI bots receive the HTML content processed directly by the server.

Common Mistakes

  • Missing Meta Data: Missing <title> and meta description tags within the dynamically rendered HTML.
  • Infinite Loops: The render service (Puppeteer) mistaking its own request for a bot and trying to re-render it (must be bypassed via User-Agent check).
  • Slow Render Time: Slow server responses causing the bot to timeout.
  • Broken Links: Links created with JS during rendering remaining without an href attribute in the raw HTML.

Frequently asked questions

Is dynamic rendering a method prohibited by Google?

No, Google has defined dynamic rendering as an acceptable solution, especially for JavaScript-heavy sites. However, it is a requirement that the content served is identical to what the user sees (no cloaking).

Why don't we just use SSR for AI bots?

Migrating the entire site to an SSR (Server-Side Rendering) architecture can be costly and complex. Dynamic rendering allows you to make improvements specifically for bots without breaking an existing CSR (Client-Side) application.

Which AI bots should I block, and which should I allow?

If you want your site's content to appear in AI answers, you should allow agents like GPTBot and OAI-SearchBot and serve them optimized HTML. If you do not want your data to be used for model training, you can restrict them via robots.txt.

Scan your site for free