Skip to content

, 7 min read

How to Deploy a Nuxt Portfolio on Cloudflare for Free

How I moved my Nuxt portfolio from GitHub Pages to Cloudflare Workers with D1 and R2, and the mistakes worth avoiding along the way.

Painted orange cloud swirling above green mountain peaks in a deep blue sky.

My portfolio used to be a static Nuxt site on GitHub Pages, with every project and service hardcoded in TypeScript files. Changing one line of text meant editing code and pushing a commit. I wanted a dashboard, a blog and a contact form, without paying for a server to run a personal site.

This is how I moved it to Cloudflare, including the mistakes that cost me time. The site you’re reading runs on this setup.

What we’re building

Each part of the site lives on one Cloudflare service:

  • Public pages: Workers static assets
  • Dashboard API and contact form: a Worker
  • Database: D1 (SQLite)
  • Images and files: R2
  • Spam protection: Turnstile

Why Cloudflare? I’d already bought my domain there, so DNS was in place. Vercel’s Hobby plan also didn’t fit, because its rules treat advertising a paid service as commercial use, and a portfolio that sells your services does exactly that.

Check the current limits before you rely on them, but at the time of writing the free plan is generous for a portfolio: unlimited static asset requests, 100,000 Worker requests a day, 5 GB of D1 storage and 10 GB of R2 storage with free egress.

The one decision that matters: pre-render your pages

The free Workers plan allows 10 ms of CPU per request. That’s tight for server-rendering a Vue app on every visit.

So don’t do that. Pre-render public pages at build time and serve them as static assets. Those requests never run your Worker at all, so they don’t count against the limit. Only your API routes run in the Worker.

The trade-off is that content changes need a rebuild. I solved that with a Publish changes button in my dashboard, covered below. On my site, edits go live in about 3 minutes.

Step 1: Configure Nuxt

Set the Nitro preset to cloudflare_module, which is the one Nitro recommends for Workers:

export default defineNuxtConfig({
  nitro: {
    preset: "cloudflare_module",
    cloudflare: { deployConfig: true, nodeCompat: true },
    prerender: {
      crawlLinks: true,
      routes: ["/", "/sitemap.xml"],
      autoSubfolderIndex: false,
      failOnError: true,
    },
  },
});

Two of those options are worth explaining:

  • autoSubfolderIndex: false writes /services.html instead of /services/index.html. Without it, every inner page redirects to a version with a trailing slash.
  • failOnError: true fails the build if a page can’t be rendered. A failed build leaves your last good version live, which beats publishing a broken page.

If you have a dashboard, make it client-only:

routeRules: {
  "/admin/**": { ssr: false, headers: { "X-Robots-Tag": "noindex, nofollow" } },
},

Step 2: Create the database and bucket

Install Wrangler, log in, and create both:

npm install -D wrangler
npx wrangler login
npx wrangler d1 create portfolio
npx wrangler r2 bucket create portfolio-media

Then add them to wrangler.jsonc. Use the database_id that the create command printed:

{
  "name": "portfolio",
  "compatibility_date": "2026-01-01",
  "compatibility_flags": ["nodejs_compat"],
  "d1_databases": [
    {
      "binding": "DB",
      "database_name": "portfolio",
      "database_id": "your-database-id",
      "migrations_dir": "server/database/migrations"
    }
  ],
  "r2_buckets": [{ "binding": "MEDIA", "bucket_name": "portfolio-media" }]
}

The database ID isn’t a secret, so it’s fine to commit this file.

Step 3: Read D1 during the build

This is the part most tutorials skip. The D1 binding only exists inside a running Worker, but pre-rendering happens in Node during your build, where that binding doesn’t exist.

My fix was to use the binding when it’s there, and fall back to D1’s HTTP API otherwise:

export function useDb(): Db {
  const binding = useEvent().context.cloudflare?.env?.DB;
  if (binding) return drizzle(binding, { schema });
  return (httpDb ??= createHttpDb());
}

createHttpDb() is a Drizzle sqlite-proxy client that posts each query to D1’s /raw endpoint. It uses an API token with read-only D1 permission, stored in the build environment. useEvent() needs nitro.experimental.asyncContext turned on.

Step 4: Connect GitHub

In Workers & Pages, select Create application, choose GitHub, and pick your repository. Then use these settings:

  • Worker name: must match name in wrangler.jsonc
  • Build command: npm run build
  • Deploy command: npx wrangler deploy
  • Build variable: NODE_VERSION, set to the version you use locally

Your package.json needs a build script for this to work. If you’re coming from GitHub Pages, you may only have generate.

If your first build fails with Missing script: "build" even though the script exists, check the branch. Mine was building main while the work was on a feature branch, and pressing Retry just rebuilt the same commit. Change the production branch in Settings > Builds, then push a new commit.

Step 5: Put variables in the right place

This caught me out, because Cloudflare has two sections with almost the same name:

  • Build variables exist only during the build. My D1 token and public site keys go here.
  • Worker secrets exist at runtime. Anything your API routes read goes here.

I added my runtime secrets to the build section, and everything looked fine in the dashboard. The live Worker simply couldn’t see them. Setting them from the terminal avoids the confusion:

npx wrangler secret put NUXT_TURNSTILE_SECRET_KEY

Then confirm with npx wrangler secret list.

There’s a second trap in the code. On Workers, runtime config only sees those secrets if you pass it the event:

// Doesn't see Worker secrets
const { turnstileSecretKey } = useRuntimeConfig();

// Does
const { turnstileSecretKey } = useRuntimeConfig(event);

Step 6: Add a Publish button

In Settings > Builds > Deploy Hooks, create a hook for your production branch. It gives you a URL, and a POST request to that URL starts a build.

Store the URL as a Worker secret, because anyone who has it can trigger builds. Then call it from an admin-only route:

export default defineEventHandler(async (event) => {
  const { deployHookUrl } = useRuntimeConfig(event);
  await $fetch(deployHookUrl, { method: "POST" });
  return { ok: true };
});

Now saving in the dashboard is instant, and publishing rebuilds the site whenever you’re ready.

Step 7: Move your domain

If your domain is on GitHub Pages, the order matters. Remove it from GitHub last, not first. If you remove it while your DNS still points at GitHub, someone else could claim your domain on Pages during the gap.

  1. Merge your work to main, and point Workers Builds at it.
  2. Delete the four A records pointing at GitHub’s 185.199.x.x addresses. Leave your MX and TXT records alone.
  3. Straight after, add your domain under the Worker’s Settings > Domains & Routes > Custom domain.
  4. Turn on Always Use HTTPS, and add a redirect rule from www to your root domain.
  5. Update any OAuth app or Turnstile widget to use the new hostname.
  6. Once the new site works, remove the custom domain in your GitHub repo’s Pages settings.

Keep those four A records handy, because adding them back is your rollback.

If Turnstile shows error 110200, the hostname you’re on isn’t listed on the widget. That one’s a quick fix under Hostname Management.

Mistakes worth avoiding

Silent spam checks. My contact form had a hidden honeypot field and a minimum fill time, and it showed “Message Sent” even when it dropped a submission. My own test messages vanished without a single error. Browser autofill can fill a hidden field too, so real visitors could lose messages the same way. Once Turnstile was verifying every submission, I removed both checks.

Loading a whole icon set. With @nuxt/icon on a static site, icon names stored in a database aren’t found by the build scan, so they render blank. I query those names at build time and add them to the client bundle through the icon:clientBundleIcons hook.

Assuming backups exist. D1 restores up to 7 days back on the free plan, and R2 has no automatic backup. Run this occasionally, and keep your own copies of important images:

npx wrangler d1 export portfolio --remote --output=backup.sql

Forgetting workers.dev. After you’ve moved to your domain, set "workers_dev": false in wrangler.jsonc, so search engines don’t index two copies of your site.

Was it worth it?

Yes. I now have a dashboard, a blog and a working contact form on the free plan, and public pages load as static files. Editing no longer means opening a code editor.

The main cost is that 10 ms limit. A couple of my dashboard requests went over it while I was measuring, and they still succeeded, but I wouldn’t count on that every time. If it ever becomes a problem, the paid plan is $5 a month, and the public site isn’t affected either way.

Filed under Nuxt, Cloudflare, Deployment

All posts