Next.js
Render Engine With
Next Middleware
One middleware file sends crawlers to Render Engine. Your app routes and visitors stay exactly as they are.
Steps
Set It Up
Step By Step
- 01
Add The Middleware
Create middleware.ts at the project root, or in src, and paste the snippet.
- 02
Set Your API Key
Set RENDER_KEY in your environment rather than in the code.
- 03
Match Your Pages
Point the matcher at your pages only. Leave out api, _next and static files.
- 04
Check It Works
Deploy. After a real crawler visits, the page appears in the Pages tab. To check now, use Recache on a page in the Pages tab and wait until its Rendered time updates, then request that page with a crawler user agent: you get the rendered copy from the cache. A crawler user agent sent from your own machine never starts a fresh render, so a page with no cached copy still shows your normal page.
Copy The Rule
Replace YOUR_RENDER_KEY with your API key, or copy it from the Setup page in the app with your key filled in. Any failure falls through to your normal page.
// middleware.ts at the root of your Next.js app (Vercel or self-hosted). Set RENDER_KEY in your environment.
import { NextRequest, NextResponse } from 'next/server';
const CRAWLERS = /googlebot|bingbot|applebot|duckduckbot|yandexbot|baiduspider|oai-searchbot|chatgpt-user|gptbot|claude-searchbot|claude-user|claudebot|perplexity-user|perplexitybot|mistralai-user|duckassistbot|amazonbot|meta-externalagent|cohere-ai|youbot|facebookexternalhit|facebot|twitterbot|linkedinbot|pinterestbot|slackbot|discordbot|telegrambot|whatsapp/i;
const RENDER_ENGINE = 'https://api.lesuto.com/api/v2/render-engine/render';
const RENDER_KEY = process.env.RENDER_KEY ?? 'YOUR_RENDER_KEY';
const PASS = [301, 302, 303, 307, 308, 404, 410];
// The crawler's address. On Vercel the platform sets x-real-ip itself. Self-hosted, set
// RENDER_CLIENT_IP_HEADER to the header your own proxy overwrites (for example x-real-ip); without it
// none is sent, because any other header could come from the visitor.
function clientIp(request: NextRequest): string {
const header = process.env.VERCEL ? 'x-real-ip' : process.env.RENDER_CLIENT_IP_HEADER;
const value = header ? request.headers.get(header) || '' : '';
return value.split(',').pop()?.trim() || '';
}
export async function middleware(request: NextRequest) {
const ua = request.headers.get('user-agent') || '';
if (request.method !== 'GET' || !CRAWLERS.test(ua)) return NextResponse.next();
try {
const res = await fetch(RENDER_ENGINE, {
headers: {
'X-Render-Key': RENDER_KEY,
'X-Render-Url': request.url,
'X-Render-Client-IP': clientIp(request),
'User-Agent': ua,
},
redirect: 'manual',
signal: AbortSignal.timeout(18000),
});
// Only pages Render Engine answered carry X-Render-Cache; anything else gets your site.
if (res.headers.get('X-Render-Cache') && (res.ok || PASS.includes(res.status))) {
const headers = new Headers({ 'Content-Type': 'text/html; charset=utf-8', 'Cache-Control': 'private, max-age=0', 'CDN-Cache-Control': 'no-store', 'Vary': 'User-Agent' });
const location = res.headers.get('Location');
if (location) headers.set('Location', location);
return new NextResponse(res.body, { status: res.status, headers });
}
} catch {}
return NextResponse.next();
}
export const config = { matcher: ['/((?!api|_next|favicon.ico|.*\\..*).*)'] };Full Guide
Every detail, edge case and fix on the Lesuto docs.
Other Ways To Set Up
Pick the stack that matches your site, or start from the checklist.
Rendered Pages
In About 15 Minutes
Add one file, deploy, and check one page.