Skip to content

Route rules don't accept .env variables #4580

Closed

Description

@marcjulian

Environment

[email protected]
[email protected]

Backend with Nitro CLI setting up routeRules cors

// nitro.config.ts
import { defineConfig } from 'nitro';

export default defineConfig({
  serverDir: './server',
  routes: {
    '/api/auth/**': './server/utils/auth.ts'
  },
  routeRules: {
    '/api/auth/**': {
      cors: {
        origin: [process.env['APP_URL']!],
        // origin: (origin) => origin === process.env['APP_URL'],
        credentials: true,
      },
    },
  },
});

Reproduction

https://stackblitz.com/edit/github-6ej7nrwe?file=vite.config.ts

Following the better-auth nitro integration and the section CORS by replacing the hard coded origin URL with an env variable.

I am trying it out with better-auth and nitro in this repo: marcjulian/aba@645f78d

Describe the bug

nitro build fails during CI when the h3 routeRules uses process.env and .env is not provided in CI.
nitro build also fails when using origin: (origin) => boolean function.

This happens because the rules are compiled and must survive a JSON round-trip.

Hard coding the origin URL doesn't allow to host the nitro API in different environments (staging, production) where different URLs are being used.

Using a middleware for applying CORS is the current solution to use .env variables during runtime.

// server/middleware/cors.ts
import env from '#server/utils/env.ts';
import { defineMiddleware } from 'nitro';
import { handleCors } from 'nitro/h3';

export default defineMiddleware((event) => {
  // Only apply CORS to the API routes.
  if (!event.url.pathname.startsWith('/api/')) {
    return undefined;
  }

  const corsRes = handleCors(event, {
    origin: (origin) => origin === process.env['APP_URL'],
    credentials: true,
  });

  // If this was a preflight request, handleCors has sent the 204 response.
  // Return it to short-circuit further handling.
  if (corsRes !== false) {
    return corsRes;
  }

  // Otherwise, continue to the next middleware / route handler.
  return undefined;
});

Additional context

No response

Logs

origin: [process.env['APP_URL']!]

nitro build

✔ Generated public .output/public nitro 12:42:04 PM
[nitro 12:42:04 PM] ℹ Building server (builder: rolldown, preset: node-server, compatibility date: 2026-09-04)
Error: Build failed with 1 error:

[plugin nitro:virtual]
Error: [h3] rules: compiler: cors rule for /api/** has a non-JSON-serializable value at options.origin[0] (undefined) — compiled rule options must survive a JSON round-trip
at assertSerializableOptions (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/h3/dist/rules/compiler.mjs:100:8)
at assertSerializableOptions (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/h3/dist/rules/compiler.mjs:88:44)
at assertSerializableOptions (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/h3/dist/rules/compiler.mjs:95:4)
at file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/h3/dist/rules/compiler.mjs:38:3
at Array.map ()
at serializeRouteRuleEntries (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/h3/dist/rules/compiler.mjs:35:21)
at serializePreMergedRouteRules (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/h3/dist/rules/compiler.mjs:32:82)
at Object.serialize (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/h3/dist/rules/compiler.mjs:146:106)
at serializeData (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected]/node_modules/rou3/dist/compiler.mjs:243:69)
at compileFinalMatch (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected]/node_modules/rou3/dist/compiler.mjs:110:21)
at aggregateBindingErrorsIntoJsError (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected]/node_modules/rolldown/dist/shared/error-BP8lJHle.mjs:48:18)
at unwrapBindingResult (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected]/node_modules/rolldown/dist/shared/error-BP8lJHle.mjs:18:128)
at #build (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected]/node_modules/rolldown/dist/shared/rolldown-DfXO5K1m.mjs:133:34)
at async buildProduction (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected]/node_modules/nitro/dist/_build/rolldown.mjs:139:12)
at async Object.run (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected]/node_modules/nitro/dist/cli/_chunks/build.mjs:42:3)
at async runCommand (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected]/node_modules/nitro/dist/_libs/citty.mjs:280:47)
at async runCommand (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected]/node_modules/nitro/dist/_libs/citty.mjs:269:5)
at async runMain (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected]/node_modules/nitro/dist/_libs/citty.mjs:437:10) {
errors: [Getter/Setter]
}

origin: (origin) => origin === process.env['APP_URL']

nitro build

Runtime config option nitro.routeRules./api/**.cors.origin may not be able to be serialized.
✔ Generated public .output/public nitro 12:41:25 PM
ℹ Building server (builder: rolldown, preset: node-server, compatibility date: 2026-09-04) nitro 12:41:25 PM
Error: Build failed with 1 error:

[plugin nitro:virtual]
Error: [h3] rules: compiler: cors rule for /api/** has a non-JSON-serializable value at options.origin (function) — compiled rule options must survive a JSON round-trip
at assertSerializableOptions (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/h3/dist/rules/compiler.mjs:100:8)
at assertSerializableOptions (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/h3/dist/rules/compiler.mjs:95:4)
at file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/h3/dist/rules/compiler.mjs:38:3
at Array.map ()
at serializeRouteRuleEntries (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/h3/dist/rules/compiler.mjs:35:21)
at serializePreMergedRouteRules (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/h3/dist/rules/compiler.mjs:32:82)
at Object.serialize (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected][email protected][email protected][email protected]/node_modules/h3/dist/rules/compiler.mjs:146:106)
at serializeData (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected]/node_modules/rou3/dist/compiler.mjs:243:69)
at compileFinalMatch (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected]/node_modules/rou3/dist/compiler.mjs:110:21)
at file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected]/node_modules/rou3/dist/compiler.mjs:100:41
at aggregateBindingErrorsIntoJsError (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected]/node_modules/rolldown/dist/shared/error-BP8lJHle.mjs:48:18)
at unwrapBindingResult (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected]/node_modules/rolldown/dist/shared/error-BP8lJHle.mjs:18:128)
at #build (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected]/node_modules/rolldown/dist/shared/rolldown-DfXO5K1m.mjs:133:34)
at async buildProduction (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected]/node_modules/nitro/dist/_build/rolldown.mjs:139:12)
at async Object.run (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected]/node_modules/nitro/dist/cli/_chunks/build.mjs:42:3)
at async runCommand (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected]/node_modules/nitro/dist/_libs/citty.mjs:280:47)
at async runCommand (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected]/node_modules/nitro/dist/_libs/citty.mjs:269:5)
at async runMain (file:///Users/m/Dev/lab/aba/node_modules/.pnpm/[email protected]/node_modules/nitro/dist/_libs/citty.mjs:437:10) {
errors: [Getter/Setter]
}

Activity

  1. added
    pending triage
    on Sep 4, 2026
  2. added and removed
    pending triage
    on Sep 7, 2026
  3. pengzh1 commented on Sep 10, 2026

    @pengzh1
    Contributor

    Looking into this — will follow up with a fix + failing test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions