Prebaked
Integration

Replit

Last reviewed

On Replit, add Prebaked to the Node server that serves your site's HTML, not to your API server. Use the official prerender-node middleware with the time limit and fallback from the Express guide, keep your token in Replit Secrets, and deploy on Autoscale or Reserved VM. Search engines, AI crawlers and link preview bots then get the rendered page; visitors get your app as before.

How it works

Replit full-stack apps often run two servers: a small Node server (for example server.mjs on the plain http module) that serves the built HTML and assets, and a separate Express service for the API. Crawlers ask for pages, and pages come from the HTML server. The middleware goes there, in front of your static files and the single-page app fallback that answers every route with index.html. For a crawler it fetches the rendered page from Prebaked; every other request goes on to your files unchanged.

Prerequisites

  • A Prebaked account with your domain added and verified.
  • Your domain token from the domain's Setup page.
  • A Replit app with a Node server that serves your HTML.
  • An Autoscale or Reserved VM deployment. A Static deployment runs no server code, so use the Cloudflare Worker instead.

Step 1: Find the server that serves your HTML

Open the file that starts your web server and look for the code that sends index.html. In a typical Replit full-stack app this is a file like server.mjs or server/index.ts, not the API routes. If your API and your HTML come from the same Express app, use that app.

Step 2: Install prerender-node and the time limit

Run npm install prerender-node in the Replit Shell. Copy prerender-timeout.cjs from step 4 of the Express guide next to your server file. It stops a slow answer after 10 seconds and serves your own page.

Step 3: Mount the middleware before your static files

Call the middleware first for every request, before your static files and the SPA fallback. Set the afterRender fallback and add the AI crawlers that prerender-node 3.8.3 lacks, as in steps 3 to 5 of the Express guide. For a plain Node http server, the complete example looks like this:

withTimeout returns a connect-style (req, res, next) function, so it also works with a plain Node http server: call it for every request and serve your own page in next. A complete server.mjs for a built single-page app (for example a Vite dist/ folder), with the fallback hook, the extra crawlers from step 5 and the time limit:

// Plain Node server (no Express) for a built single-page app, e.g. a Vite `dist/` folder.
// Crawlers get the page rendered by Prebaked through the official prerender-node middleware,
// with a 10 s time limit (prerender-timeout.cjs); everyone else gets the files in `dist/`.
//
//   npm install prerender-node
//   PREBAKED_TOKEN=<your token> node server.mjs
//
// Needs prerender-timeout.cjs next to this file. Optional env: PORT (3000), DIST_DIR (dist).
import { createReadStream, statSync } from 'node:fs';
import { createServer } from 'node:http';
import { extname, join, normalize, resolve, sep } from 'node:path';
import prerender from 'prerender-node';
import { withTimeout } from './prerender-timeout.cjs';

const DIST = resolve(process.env.DIST_DIR ?? 'dist');
const PORT = Number(process.env.PORT ?? 3000);
const TYPES = {
  '.html': 'text/html; charset=utf-8',
  '.js': 'text/javascript; charset=utf-8',
  '.css': 'text/css; charset=utf-8',
  '.json': 'application/json',
  '.svg': 'image/svg+xml',
  '.png': 'image/png',
  '.jpg': 'image/jpeg',
  '.webp': 'image/webp',
  '.ico': 'image/x-icon',
  '.woff2': 'font/woff2',
  '.txt': 'text/plain; charset=utf-8',
  '.xml': 'application/xml',
};

prerender
  .set('prerenderServiceUrl', process.env.PREBAKED_SERVICE_URL ?? 'https://service.prebaked.io/')
  .set('prerenderToken', process.env.PREBAKED_TOKEN)
  // Prebaked errors serve your own page; only the rendered page's own status reaches crawlers.
  .set('afterRender', (err, req, prerenderRes) => {
    const status = prerenderRes?.statusCode ?? 0;
    const pageStatus = (status >= 200 && status < 400) || status === 404 || status === 410;
    if (err || !pageStatus) return { cancelRender: true };
  })
  .addUserAgents(['OAI-SearchBot', 'ClaudeBot', 'Claude-User', 'Claude-SearchBot', 'meta-externalagent']);

const prebaked = withTimeout(prerender, { timeoutMs: 10_000 });

/** A file inside DIST, or null. */
function fileFor(urlPath) {
  let path;
  try {
    path = normalize(join(DIST, decodeURIComponent(urlPath)));
  } catch {
    return null;
  }
  if (path !== DIST && !path.startsWith(DIST + sep)) return null;
  try {
    return statSync(path).isFile() ? path : null;
  } catch {
    return null;
  }
}

/** Static files from DIST; every other path gets index.html (SPA routes). */
function serveDist(req, res) {
  const urlPath = new URL(req.url, 'http://localhost').pathname;
  const file = fileFor(urlPath) ?? join(DIST, 'index.html');
  res.writeHead(200, { 'Content-Type': TYPES[extname(file)] ?? 'application/octet-stream' });
  if (req.method === 'HEAD') return res.end();
  createReadStream(file).pipe(res);
}

createServer((req, res) => {
  prebaked(req, res, (err) => {
    if (err) console.error(err);
    serveDist(req, res);
  });
}).listen(PORT, () => console.log(`http://localhost:${PORT}`));
npm install prerender-node
PREBAKED_TOKEN=<your token> node server.mjs

If your HTML server uses Express, mount the same middleware with app.use as shown in step 2 of the Express guide.

Step 4: Store the token in Production app secrets

The live site reads its secrets from the published app, not from the workspace. In Replit, open Publishing, then Adjust settings, then Production app secrets, and add PREBAKED_TOKEN with your domain token and PREBAKED_SERVICE_URL with https://service.prebaked.io/. Save, then publish again. Replit passes them to your server as environment variables, so it reads them from process.env.

Add the same two values to the workspace Secrets tool only if you want to test in the workspace preview or the Shell. The live site does not need them there.

Never put the token in your code, in a committed file or in the [env] section of .replit. A value in .replit overrides the secret, so a new or rotated token never reaches the live site, and every crawler request is rejected with 401. If your agent wrote it there, remove it.

Step 5: Deploy

Deploy as Autoscale or Reserved VM. Both run your server code, so the middleware runs for every request. Publish again after any change to the production app secrets.

Test the installation

Check that Prebaked answers a crawler on your live domain:

curl -sI -A "GPTBot" https://www.<your-domain>/ | grep -i x-prebaked
  • x-prebaked: hit: Prebaked answered from its cache.
  • x-prebaked: stale: Prebaked answered from an older cached copy and refreshes it in the background.
  • x-prebaked: rendered: Prebaked rendered the page for this request.
  • x-prebaked: origin: Prebaked fetched your page itself without rendering it, for example when the render took too long.
  • No output: your site answered directly. The middleware is not running, the user agent did not match, or the fallback served your page.

Then press Check installation on the domain's Setup page in the Prebaked dashboard. It shows the first and latest crawler request that reached Prebaked. You can also run the free checker on the same page.

Common errors

What you see Cause Fix
Crawlers still get the empty app shell The middleware is in the API server, not in the server that serves the HTML Move it to the HTML server (step 1)
No x-prebaked header on any page Your static files or the SPA fallback answer before the middleware runs Call the middleware first for every request (step 3)
No x-prebaked header after deploying The published app does not have PREBAKED_TOKEN in Production app secrets, or was not published again after adding it Add it to Production app secrets and publish again (steps 4 and 5)
Still no x-prebaked header after a new or rotated token The token is hard-coded in the [env] section of .replit or another file, which overrides the secret Remove it from the file, keep it only in Production app secrets, and publish again
Nothing changes on a Static deployment Static deployments run no server code Use the Cloudflare Worker, or switch to Autoscale or Reserved VM
Crawlers get a 401 or 403 error page Wrong token or unverified domain, and the afterRender fallback is missing Fix the token or verify the domain, and add the fallback from the Express guide

More help: Crawlers still see an empty page and 401 and 403 errors.

Removing it

Remove the middleware call from your server, delete prerender-timeout.cjs, run npm uninstall prerender-node, remove the secrets and deploy again.

FAQ

Does Prebaked work with a Static deployment on Replit?

Not through Replit itself. A Static deployment runs no server code, so the middleware cannot run. Put your domain behind Cloudflare and use the Cloudflare Worker, which works in front of any origin.

Why not add the middleware to my API server?

Crawlers request pages, and in a Replit full-stack app the pages come from the server that serves the HTML. The API server only answers data requests from your app, so a crawler never reaches middleware placed there.

Where do I put my Prebaked token on Replit?

In Replit Secrets, as PREBAKED_TOKEN. Your server reads it from process.env.PREBAKED_TOKEN. Keep it out of your code and your repository.

What happens if Prebaked is slow or down?

With the afterRender fallback and the withTimeout wrapper from the Express guide, an error or an answer slower than 10 seconds serves your normal page. Crawlers never see a Prebaked error.

Do visitors notice anything?

No. Only requests from known crawlers go to Prebaked. Visitors get your app from your server as before.