Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

Route Next eve service through Build Output (#413)

JJ Kasper2afed3b7d3d1
diff --git a/.changeset/quiet-next-services.md b/.changeset/quiet-next-services.md
new file mode 100644
index 000000000..efde0add5
--- /dev/null
+++ b/.changeset/quiet-next-services.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Update `withEve()` to generate Vercel Build Output service routes for eve instead of the legacy Next.js rewrite setup. The generated output now uses the stable `services` field and service routes, including in hosted Vercel builds where no local `.vercel/project.json` exists, so Vercel builds the eve service without Next.js rewrites.
diff --git a/apps/frameworks/next/README.md b/apps/frameworks/next/README.md
index 114b89c63..36ca7cea9 100644
--- a/apps/frameworks/next/README.md
+++ b/apps/frameworks/next/README.md
@@ -14,10 +14,9 @@ Set `EVE_BASE_URL` before starting Next.js to reuse an already-running eve
server instead of letting `withEve()` start one.
When a linked Vercel project is detected, `withEve()` writes generated
-`experimentalServices` to `.vercel/output/config.json` for Next.js at `/` and
-eve behind the private `/_eve_internal/eve` service prefix. Next.js rewrites
-the public eve endpoints to that private service so the eve index route is not
-exposed at the site root.
+`services` and `routes` to `.vercel/output/config.json`. The Next.js app stays
+the default app, while Vercel routes public eve endpoints directly to the eve
+service before filesystem routing.
For non-Vercel production hosts, set `EVE_NEXT_PRODUCTION_ORIGIN` to the public
origin that serves the eve service namespace before building the Next.js app.
diff --git a/apps/templates/web-chat-next/README.md b/apps/templates/web-chat-next/README.md
index 006316d58..9561a135b 100644
--- a/apps/templates/web-chat-next/README.md
+++ b/apps/templates/web-chat-next/README.md
@@ -22,11 +22,11 @@ server instead of letting `withEve()` start one:
EVE_BASE_URL=http://localhost:3000 pnpm --filter web-chat-next-template dev
```
-When a linked Vercel project is detected, `withEve()` writes generated
-`experimentalServices` to `.vercel/output/config.json` so Next.js deploys at
-`/` and the app-local eve agent runs behind the private
-`/_eve_internal/eve` service prefix, then rewrites public eve endpoints to that
-private service.
+In Vercel builds, or when a linked Vercel project is detected locally,
+`withEve()` writes generated `services` and `routes` to
+`.vercel/output/config.json`. The Next.js app stays the default app, while
+Vercel routes public eve endpoints directly to the app-local eve service before
+filesystem routing.
## Scaffold Source
diff --git a/docs/guides/frontend/nextjs.mdx b/docs/guides/frontend/nextjs.mdx
index 0ee3ec89d..2972efa51 100644
--- a/docs/guides/frontend/nextjs.mdx
+++ b/docs/guides/frontend/nextjs.mdx
@@ -34,12 +34,12 @@ export default withEve(nextConfig, {
All fields are optional.
-| Option | Type | Default | Purpose |
-| -------------------- | -------- | ---------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
-| `eveRoot` | `string` | Next.js app root | Path to the eve app root, relative to `process.cwd()` unless absolute. Set it when the agent lives outside the Next.js project. |
-| `eveBuildCommand` | `string` | `"eve build"` | Build command for the generated eve Vercel service. Use it when the eve service needs project-specific prework, without changing the Next.js build. |
-| `servicePrefix` | `string` | `"/_eve_internal/eve"` | Private Vercel route namespace for the eve service. Must match the eve service's mount in your Vercel Build Output config when you set it manually. |
-| `devServerTimeoutMs` | `number` | `180000` | Maximum time to wait for the eve development server to become available. |
+| Option | Type | Default | Purpose |
+| -------------------- | -------- | ---------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
+| `eveRoot` | `string` | Next.js app root | Path to the eve app root, relative to `process.cwd()` unless absolute. Set it when the agent lives outside the Next.js project. |
+| `eveBuildCommand` | `string` | `"eve build"` | Build command for the generated eve Vercel service. Use it when the eve service needs project-specific prework, without changing the Next.js build. |
+| `servicePrefix` | `string` | `"/_eve_internal/eve"` | Private route namespace for legacy manual Vercel service configs and non-Vercel production proxying. Generated Vercel configs use service routes instead. |
+| `devServerTimeoutMs` | `number` | `180000` | Maximum time to wait for the eve development server to become available. |
For slow cold starts, increase the development timeout:
@@ -75,7 +75,7 @@ For a public demo, use `none()` (also from `eve/channels/auth`) to skip authenti
## Dev vs deploy topology
- **Local dev.** `npm run dev` boots the eve dev server next to `next dev` and rewrites the eve routes over to it. The browser only ever talks to the Next.js origin.
-- **Vercel.** The web app and the eve runtime deploy as a single project. The web app stays public; the eve runtime sits behind it on the same site origin. When the agent needs its own build step, set `eveBuildCommand`:
+- **Vercel.** The web app and the eve runtime deploy as a single project. `withEve()` writes Build Output `services` for eve and `routes` that send `/eve/v1/**` to that service before filesystem routing; the Next.js app itself remains the default app. When the agent needs its own build step, set `eveBuildCommand`:
```ts
export default withEve(nextConfig, {
diff --git a/packages/eve/src/public/next/index.integration.test.ts b/packages/eve/src/public/next/index.integration.test.ts
index a505b1aa0..8d6a001ae 100644
--- a/packages/eve/src/public/next/index.integration.test.ts
+++ b/packages/eve/src/public/next/index.integration.test.ts
@@ -4,12 +4,7 @@ import { join } from "node:path";
import { afterEach, describe, expect, it, vi } from "vitest";
-import {
- EVE_NEXT_SERVICE_PREFIX,
- withEve,
- type EveNextConfig,
- type EveNextRewriteSections,
-} from "./index.js";
+import { withEve, type EveNextConfig, type EveNextRewriteSections } from "./index.js";
interface TestConfig extends EveNextConfig {
readonly basePath?: string;
@@ -38,12 +33,10 @@ describe("withEve Vercel config", () => {
vi.unstubAllGlobals();
});
- it("does not create Build Output config when no Vercel project is detected", async () => {
+ it("does not create Build Output config outside Vercel when no Vercel project is detected", async () => {
const appRoot = await createTempAppRoot();
process.chdir(appRoot);
vi.stubEnv("NODE_ENV", "production");
- vi.stubEnv("VERCEL", "1");
- vi.stubEnv("VERCEL_URL", "preview.example.com");
const config = await resolveConfig(withEve<TestConfig>({}));
const rewrites = await config.rewrites?.();
@@ -52,11 +45,45 @@ describe("withEve Vercel config", () => {
readFile(join(appRoot, ".vercel", "output", "config.json"), "utf8"),
).rejects.toThrow();
expect(getBeforeFiles(rewrites)).toContainEqual({
- destination: `${EVE_NEXT_SERVICE_PREFIX}/eve/v1/:path+`,
+ destination: "http://127.0.0.1:4274/eve/v1/:path+",
source: "/eve/v1/:path+",
});
});
+ it("writes Build Output config in Vercel even when no linked project is detected", async () => {
+ const appRoot = await createTempAppRoot();
+ process.chdir(appRoot);
+ vi.stubEnv("NODE_ENV", "production");
+ vi.stubEnv("VERCEL", "1");
+ vi.stubEnv("VERCEL_URL", "preview.example.com");
+
+ const config = await resolveConfig(withEve<TestConfig>({}));
+ const rewrites = await config.rewrites?.();
+ const outputConfig = await readJsonFile(join(appRoot, ".vercel", "output", "config.json"));
+
+ expect(outputConfig).toEqual({
+ routes: [
+ {
+ destination: {
+ service: "eve",
+ type: "service",
+ },
+ src: "^/eve/v1/(.*)$",
+ },
+ ],
+ services: {
+ eve: {
+ buildCommand: "eve build",
+ entrypoint: "package.json",
+ framework: "eve",
+ root: ".",
+ },
+ },
+ version: 3,
+ });
+ expect(rewrites).toBeUndefined();
+ });
+
it("writes Build Output config to the closest existing .vercel directory", async () => {
const projectRoot = await createTempAppRoot();
const appRoot = join(projectRoot, "apps", "web");
@@ -73,29 +100,31 @@ describe("withEve Vercel config", () => {
const outputConfig = await readJsonFile(join(projectRoot, ".vercel", "output", "config.json"));
expect(outputConfig).toEqual({
- version: 3,
- experimentalServices: {
+ routes: [
+ {
+ destination: {
+ service: "eve",
+ type: "service",
+ },
+ src: "^/eve/v1/(.*)$",
+ },
+ ],
+ services: {
eve: {
buildCommand: "eve build",
- entrypoint: ".",
+ entrypoint: "package.json",
framework: "eve",
- mount: EVE_NEXT_SERVICE_PREFIX,
- type: "web",
- },
- web: {
- entrypoint: ".",
- framework: "nextjs",
- mount: "/",
- type: "web",
+ root: ".",
},
},
+ version: 3,
});
await expect(
readFile(join(appRoot, ".vercel", "output", "config.json"), "utf8"),
).rejects.toThrow();
});
- it("uses an already configured root eve service prefix", async () => {
+ it("uses an already configured root eve service", async () => {
const appRoot = await createTempAppRoot();
process.chdir(appRoot);
vi.stubEnv("NODE_ENV", "production");
@@ -106,16 +135,11 @@ describe("withEve Vercel config", () => {
`${JSON.stringify(
{
$schema: "https://openapi.vercel.sh/vercel.json",
- experimentalServices: {
+ services: {
agent: {
- entrypoint: "agent",
+ entrypoint: "package.json",
framework: "eve",
- routePrefix: "/private/agent",
- },
- frontend: {
- entrypoint: ".",
- framework: "nextjs",
- routePrefix: "/",
+ root: "agent",
},
},
},
@@ -130,36 +154,37 @@ describe("withEve Vercel config", () => {
await expect(
readFile(join(appRoot, ".vercel", "output", "config.json"), "utf8"),
).rejects.toThrow();
- expect(getBeforeFiles(rewrites)).toContainEqual({
- destination: "/private/agent/eve/v1/:path+",
- source: "/eve/v1/:path+",
- });
+ expect(rewrites).toBeUndefined();
});
- it("uses an already configured Build Output eve service prefix", async () => {
+ it("preserves an already configured Build Output eve service and inserts its route", async () => {
const appRoot = await createTempAppRoot();
process.chdir(appRoot);
vi.stubEnv("NODE_ENV", "production");
vi.stubEnv("VERCEL", "1");
vi.stubEnv("VERCEL_URL", "preview.example.com");
await mkdir(join(appRoot, ".vercel", "output"), { recursive: true });
+ await writeFile(join(appRoot, ".vercel", "project.json"), "{}\n");
await writeFile(
join(appRoot, ".vercel", "output", "config.json"),
`${JSON.stringify(
{
version: 3,
- experimentalServices: {
+ routes: [
+ { handle: "filesystem" },
+ {
+ destination: {
+ service: "agent",
+ type: "service",
+ },
+ src: "^/eve/v1/(.*)$",
+ },
+ ],
+ services: {
agent: {
- entrypoint: "agent",
+ entrypoint: "package.json",
framework: "eve",
- mount: "/private/agent",
- type: "web",
- },
- frontend: {
- entrypoint: ".",
- framework: "nextjs",
- mount: "/",
- type: "web",
+ root: "agent",
},
},
},
@@ -173,26 +198,26 @@ describe("withEve Vercel config", () => {
const outputConfig = await readJsonFile(join(appRoot, ".vercel", "output", "config.json"));
expect(outputConfig).toEqual({
- version: 3,
- experimentalServices: {
+ routes: [
+ {
+ destination: {
+ service: "agent",
+ type: "service",
+ },
+ src: "^/eve/v1/(.*)$",
+ },
+ { handle: "filesystem" },
+ ],
+ services: {
agent: {
- entrypoint: "agent",
+ entrypoint: "package.json",
framework: "eve",
- mount: "/private/agent",
- type: "web",
- },
- frontend: {
- entrypoint: ".",
- framework: "nextjs",
- mount: "/",
- type: "web",
+ root: "agent",
},
},
+ version: 3,
});
- expect(getBeforeFiles(rewrites)).toContainEqual({
- destination: "/private/agent/eve/v1/:path+",
- source: "/eve/v1/:path+",
- });
+ expect(rewrites).toBeUndefined();
});
it("accepts a custom eve service build command", async () => {
@@ -215,7 +240,7 @@ describe("withEve Vercel config", () => {
const outputConfig = await readJsonFile(join(appRoot, ".vercel", "output", "config.json"));
expect(outputConfig).toMatchObject({
- experimentalServices: {
+ services: {
eve: {
buildCommand: "pnpm build:eve",
},
diff --git a/packages/eve/src/public/next/index.test.ts b/packages/eve/src/public/next/index.test.ts
index 50345adda..bf3fb68f8 100644
--- a/packages/eve/src/public/next/index.test.ts
+++ b/packages/eve/src/public/next/index.test.ts
@@ -28,7 +28,7 @@ describe("withEve", () => {
vi.unstubAllEnvs();
});
- it("emits eve rewrites through beforeFiles when no user rewrites exist", async () => {
+ it("does not add Next.js rewrites on Vercel", async () => {
vi.stubEnv("NODE_ENV", "production");
vi.stubEnv("VERCEL", "1");
vi.stubEnv("VERCEL_URL", "preview.example.com");
@@ -36,23 +36,12 @@ describe("withEve", () => {
const config = await resolveConfig(withEve<TestConfig>({}));
const rewrites = await config.rewrites?.();
- expect(isRewriteSections(rewrites)).toBe(true);
- if (!isRewriteSections(rewrites)) {
- return;
- }
-
- expect(rewrites.beforeFiles).toEqual([
- {
- destination: `${EVE_NEXT_SERVICE_PREFIX}/eve/v1/:path+`,
- source: "/eve/v1/:path+",
- },
- ]);
+ expect(rewrites).toBeUndefined();
});
it("omits the basePath override so Next.js applies a configured basePath", async () => {
vi.stubEnv("NODE_ENV", "production");
- vi.stubEnv("VERCEL", "1");
- vi.stubEnv("VERCEL_URL", "preview.example.com");
+ vi.stubEnv("EVE_NEXT_PRODUCTION_ORIGIN", "https://agent.example.com");
const config = await resolveConfig(
withEve<TestConfig>({
@@ -63,30 +52,28 @@ describe("withEve", () => {
const [eveRewrite] = getBeforeFiles(rewrites);
expect(eveRewrite).toEqual({
- destination: `${EVE_NEXT_SERVICE_PREFIX}/eve/v1/:path+`,
+ destination: `https://agent.example.com${EVE_NEXT_SERVICE_PREFIX}/eve/v1/:path+`,
source: "/eve/v1/:path+",
});
expect(eveRewrite).not.toHaveProperty("basePath");
});
- it("adds production Vercel rewrites to the private eve service namespace", async () => {
+ it("adds non-Vercel production rewrites to the configured eve service namespace", async () => {
vi.stubEnv("NODE_ENV", "production");
- vi.stubEnv("VERCEL", "1");
- vi.stubEnv("VERCEL_URL", "preview.example.com");
+ vi.stubEnv("EVE_NEXT_PRODUCTION_ORIGIN", "https://agent.example.com");
const config = await resolveConfig(withEve<TestConfig>({}));
const rewrites = await config.rewrites?.();
expect(getBeforeFiles(rewrites)).toContainEqual({
- destination: `${EVE_NEXT_SERVICE_PREFIX}/eve/v1/:path+`,
+ destination: `https://agent.example.com${EVE_NEXT_SERVICE_PREFIX}/eve/v1/:path+`,
source: "/eve/v1/:path+",
});
});
it("only rewrites eve-prefixed non-index routes", async () => {
vi.stubEnv("NODE_ENV", "production");
- vi.stubEnv("VERCEL", "1");
- vi.stubEnv("VERCEL_URL", "preview.example.com");
+ vi.stubEnv("EVE_NEXT_PRODUCTION_ORIGIN", "https://agent.example.com");
const config = await resolveConfig(withEve<TestConfig>({}));
const rewrites = await config.rewrites?.();
@@ -99,14 +86,13 @@ describe("withEve", () => {
it("rewrites authored channel routes under the eve protocol prefix", async () => {
vi.stubEnv("NODE_ENV", "production");
- vi.stubEnv("VERCEL", "1");
- vi.stubEnv("VERCEL_URL", "preview.example.com");
+ vi.stubEnv("EVE_NEXT_PRODUCTION_ORIGIN", "https://agent.example.com");
const config = await resolveConfig(withEve<TestConfig>({}));
const rewrites = await config.rewrites?.();
expect(getBeforeFiles(rewrites)).toContainEqual({
- destination: `${EVE_NEXT_SERVICE_PREFIX}/eve/v1/:path+`,
+ destination: `https://agent.example.com${EVE_NEXT_SERVICE_PREFIX}/eve/v1/:path+`,
source: "/eve/v1/:path+",
});
});
@@ -124,7 +110,7 @@ describe("withEve", () => {
});
});
- it("ignores Vercel deployment URL when creating local service rewrites", async () => {
+ it("ignores Vercel deployment URL by leaving routing to Build Output config", async () => {
vi.stubEnv("NODE_ENV", "production");
vi.stubEnv("VERCEL", "1");
vi.stubEnv("VERCEL_URL", "http://preview.example.com");
@@ -132,10 +118,7 @@ describe("withEve", () => {
const config = await resolveConfig(withEve<TestConfig>({}));
const rewrites = await config.rewrites?.();
- expect(getBeforeFiles(rewrites)).toContainEqual({
- destination: `${EVE_NEXT_SERVICE_PREFIX}/eve/v1/:path+`,
- source: "/eve/v1/:path+",
- });
+ expect(rewrites).toBeUndefined();
});
it("ignores production origin overrides on Vercel", async () => {
@@ -146,16 +129,12 @@ describe("withEve", () => {
const config = await resolveConfig(withEve<TestConfig>({}));
const rewrites = await config.rewrites?.();
- expect(getBeforeFiles(rewrites)).toContainEqual({
- destination: `${EVE_NEXT_SERVICE_PREFIX}/eve/v1/:path+`,
- source: "/eve/v1/:path+",
- });
+ expect(rewrites).toBeUndefined();
});
it("preserves object config values and existing array rewrites", async () => {
vi.stubEnv("NODE_ENV", "production");
- vi.stubEnv("VERCEL", "1");
- vi.stubEnv("VERCEL_URL", "preview.example.com");
+ vi.stubEnv("EVE_NEXT_PRODUCTION_ORIGIN", "https://agent.example.com");
const config = await resolveConfig(
withEve<TestConfig>({
@@ -179,7 +158,7 @@ describe("withEve", () => {
}
expect(rewrites.beforeFiles).toContainEqual({
- destination: `${EVE_NEXT_SERVICE_PREFIX}/eve/v1/:path+`,
+ destination: `https://agent.example.com${EVE_NEXT_SERVICE_PREFIX}/eve/v1/:path+`,
source: "/eve/v1/:path+",
});
expect(rewrites.afterFiles).toContainEqual({
@@ -190,8 +169,7 @@ describe("withEve", () => {
it("prepends eve rewrites to beforeFiles when user rewrites use sections", async () => {
vi.stubEnv("NODE_ENV", "production");
- vi.stubEnv("VERCEL", "1");
- vi.stubEnv("VERCEL_URL", "preview.example.com");
+ vi.stubEnv("EVE_NEXT_PRODUCTION_ORIGIN", "https://agent.example.com");
const config = await resolveConfig(
withEve<TestConfig>({
@@ -221,7 +199,7 @@ describe("withEve", () => {
}
expect(rewrites.beforeFiles?.at(0)).toEqual({
- destination: `${EVE_NEXT_SERVICE_PREFIX}/eve/v1/:path+`,
+ destination: `https://agent.example.com${EVE_NEXT_SERVICE_PREFIX}/eve/v1/:path+`,
source: "/eve/v1/:path+",
});
expect(rewrites.beforeFiles).toContainEqual({
@@ -238,8 +216,7 @@ describe("withEve", () => {
it("accepts a custom private service prefix", async () => {
vi.stubEnv("NODE_ENV", "production");
- vi.stubEnv("VERCEL", "1");
- vi.stubEnv("VERCEL_URL", "preview.example.com");
+ vi.stubEnv("EVE_NEXT_PRODUCTION_ORIGIN", "https://agent.example.com");
const config = await resolveConfig(
withEve<TestConfig>(
@@ -252,7 +229,7 @@ describe("withEve", () => {
const rewrites = await config.rewrites?.();
expect(getBeforeFiles(rewrites)).toContainEqual({
- destination: "/internal/eve/eve/v1/:path+",
+ destination: "https://agent.example.com/internal/eve/eve/v1/:path+",
source: "/eve/v1/:path+",
});
});
diff --git a/packages/eve/src/public/next/index.ts b/packages/eve/src/public/next/index.ts
index b08d22f97..20cbdaf26 100644
--- a/packages/eve/src/public/next/index.ts
+++ b/packages/eve/src/public/next/index.ts
@@ -7,9 +7,8 @@ import { resolveEveDestinationPrefix } from "./server.js";
import { ensureEveVercelOutputConfig } from "./vercel-output-config.js";
/**
- * Default private route namespace for hosting eve as a separate experimental
- * Vercel service behind the Next.js app. {@link WithEveOptions.servicePrefix}
- * defaults to this value.
+ * Default private route namespace for legacy manually configured Vercel
+ * services. {@link WithEveOptions.servicePrefix} defaults to this value.
*/
export const EVE_NEXT_SERVICE_PREFIX = "/_eve_internal/eve";
@@ -92,11 +91,11 @@ export interface WithEveOptions {
*/
readonly eveBuildCommand?: string;
/**
- * Private Vercel service prefix for the eve deployment. Defaults to
- * {@link EVE_NEXT_SERVICE_PREFIX} (`/_eve_internal/eve`). `withEve` normalizes
- * the prefix (adds a leading slash, strips trailing slashes) and rejects a
- * prefix that resolves to the root route. The prefix must match the eve
- * service's mount in Vercel Build Output config.
+ * Private route namespace for legacy manually configured Vercel services and
+ * non-Vercel production proxying. Defaults to {@link EVE_NEXT_SERVICE_PREFIX}
+ * (`/_eve_internal/eve`). `withEve` normalizes the prefix (adds a leading
+ * slash, strips trailing slashes) and rejects a prefix that resolves to the
+ * root route.
*/
readonly servicePrefix?: string;
}
@@ -243,7 +242,8 @@ async function resolveNextConfig<TConfig extends EveNextConfig>(
*
* In development, starts `eve dev --no-ui --port 0` for the eve app and
* rewrites eve protocol endpoints to that local URL. In Vercel production,
- * rewrites to the resolved private eve service prefix.
+ * writes Build Output service routes so Vercel sends eve protocol endpoints to
+ * the eve service directly.
* Outside Vercel production, serves an existing `.output/server/index.mjs` build
* on a stable local port when present; otherwise set `EVE_NEXT_PRODUCTION_ORIGIN`
* to the origin serving the eve service namespace.
@@ -266,6 +266,11 @@ export function withEve<TConfig extends EveNextConfig>(
nextRoot,
servicePrefix: servicePrefixInput,
});
+
+ if (process.env.VERCEL) {
+ return nextConfig;
+ }
+
const productionDestination = resolveProductionDestination(configuredVercel.servicePrefix);
return {
diff --git a/packages/eve/src/public/next/vercel-output-config.ts b/packages/eve/src/public/next/vercel-output-config.ts
index 6b92871d8..8676387f1 100644
--- a/packages/eve/src/public/next/vercel-output-config.ts
+++ b/packages/eve/src/public/next/vercel-output-config.ts
@@ -9,6 +9,8 @@ import {
const VERCEL_JSON_FILE_NAME = "vercel.json";
const VERCEL_OUTPUT_CONFIG_FILE_NAME = ".vercel/output/config.json";
const VERCEL_BUILD_OUTPUT_VERSION = 3;
+const EVE_SERVICE_NAME = "eve";
+const EVE_SERVICE_ROUTE_SRC = "^/eve/v1/(.*)$";
interface VercelServiceMount {
readonly path?: string;
@@ -21,11 +23,25 @@ interface VercelServiceConfig {
readonly framework?: string;
readonly mount?: string | VercelServiceMount;
readonly routePrefix?: string;
+ readonly root?: string;
readonly type?: string;
}
+interface VercelServiceRouteDestination {
+ readonly service?: string;
+ readonly type?: string;
+}
+
+interface VercelRouteConfig {
+ readonly destination?: string | VercelServiceRouteDestination;
+ readonly handle?: string;
+ readonly src?: string;
+ readonly [key: string]: unknown;
+}
+
interface VercelServicesConfig {
- readonly experimentalServices?: Record<string, VercelServiceConfig>;
+ readonly routes?: readonly VercelRouteConfig[];
+ readonly services?: Record<string, VercelServiceConfig>;
readonly [key: string]: unknown;
}
@@ -83,7 +99,7 @@ async function resolveVercelOutputConfigLocation(nextRoot: string): Promise<{
}
return {
- canWriteGeneratedOutput: false,
+ canWriteGeneratedOutput: Boolean(process.env.VERCEL),
outputConfigPath: join(nextRoot, VERCEL_OUTPUT_CONFIG_FILE_NAME),
projectRoot,
};
@@ -94,10 +110,16 @@ function normalizeVercelServicesConfig(value: unknown, fileName: string): Vercel
throw new Error(`${fileName} must contain a JSON object.`);
}
- const experimentalServices = value.experimentalServices;
+ const services = value.services;
- if (experimentalServices !== undefined && !isRecord(experimentalServices)) {
- throw new Error(`${fileName} experimentalServices must be a JSON object.`);
+ if (services !== undefined && !isRecord(services)) {
+ throw new Error(`${fileName} services must be a JSON object.`);
+ }
+
+ const routes = value.routes;
+
+ if (routes !== undefined && !Array.isArray(routes)) {
+ throw new Error(`${fileName} routes must be an array.`);
}
return value as VercelServicesConfig;
@@ -125,7 +147,16 @@ function findServiceByFramework(
services: Record<string, VercelServiceConfig>,
framework: string,
): VercelServiceConfig | undefined {
- return Object.values(services).find((service) => service.framework === framework);
+ return findServiceEntryByFramework(services, framework)?.service;
+}
+
+function findServiceEntryByFramework(
+ services: Record<string, VercelServiceConfig>,
+ framework: string,
+): { readonly name: string; readonly service: VercelServiceConfig } | undefined {
+ return Object.entries(services)
+ .map(([name, service]) => ({ name, service }))
+ .find((entry) => entry.service.framework === framework);
}
function resolveServicePrefix(service: VercelServiceConfig | undefined): string | undefined {
@@ -160,22 +191,64 @@ function resolveConfiguredServicePrefix(input: {
return resolveServicePrefix(configuredEveService) ?? input.servicePrefix;
}
-function assertRootServicesAreComplete(input: {
+function assertRootServicesIncludeEve(input: {
readonly services: Record<string, VercelServiceConfig>;
readonly servicePrefix: string;
}): string {
const configuredEveService = findServiceByFramework(input.services, "eve");
- const configuredNextService = findServiceByFramework(input.services, "nextjs");
- if (configuredEveService !== undefined && configuredNextService !== undefined) {
+ if (configuredEveService !== undefined) {
return resolveServicePrefix(configuredEveService) ?? input.servicePrefix;
}
throw new Error(
- `${VERCEL_JSON_FILE_NAME} already defines experimentalServices, so withEve cannot add generated services through ${VERCEL_OUTPUT_CONFIG_FILE_NAME}. Add both the Next.js and eve services to ${VERCEL_JSON_FILE_NAME}, or remove experimentalServices from ${VERCEL_JSON_FILE_NAME}.`,
+ `${VERCEL_JSON_FILE_NAME} already defines services, so withEve cannot add a generated eve service through ${VERCEL_OUTPUT_CONFIG_FILE_NAME}. Add the eve service to ${VERCEL_JSON_FILE_NAME}, or remove services from ${VERCEL_JSON_FILE_NAME}.`,
+ );
+}
+
+function isEveServiceRoute(route: VercelRouteConfig, serviceName: string): boolean {
+ const destination = route.destination;
+
+ return (
+ route.src === EVE_SERVICE_ROUTE_SRC &&
+ isRecord(destination) &&
+ destination.type === "service" &&
+ destination.service === serviceName
);
}
+function createEveServiceRoute(serviceName: string): VercelRouteConfig {
+ return {
+ destination: {
+ service: serviceName,
+ type: "service",
+ },
+ src: EVE_SERVICE_ROUTE_SRC,
+ };
+}
+
+function insertEveServiceRoute(
+ routes: readonly VercelRouteConfig[],
+ serviceName: string,
+): readonly VercelRouteConfig[] {
+ const existingRoute = routes.find((route) => isEveServiceRoute(route, serviceName));
+ const routesWithoutEveRoute = routes.filter((route) => !isEveServiceRoute(route, serviceName));
+ const filesystemRouteIndex = routesWithoutEveRoute.findIndex(
+ (route) => route.handle === "filesystem",
+ );
+ const eveRoute = existingRoute ?? createEveServiceRoute(serviceName);
+
+ if (filesystemRouteIndex === -1) {
+ return [eveRoute, ...routesWithoutEveRoute];
+ }
+
+ return [
+ ...routesWithoutEveRoute.slice(0, filesystemRouteIndex),
+ eveRoute,
+ ...routesWithoutEveRoute.slice(filesystemRouteIndex),
+ ];
+}
+
export async function ensureEveVercelOutputConfig(input: {
readonly appRoot: string;
readonly eveBuildCommand: string;
@@ -188,11 +261,11 @@ export async function ensureEveVercelOutputConfig(input: {
join(projectRoot, VERCEL_JSON_FILE_NAME),
VERCEL_JSON_FILE_NAME,
);
- const rootServices = rootVercelConfig.experimentalServices;
+ const rootServices = rootVercelConfig.services;
if (hasServices(rootServices)) {
return {
- servicePrefix: assertRootServicesAreComplete({
+ servicePrefix: assertRootServicesIncludeEve({
services: rootServices,
servicePrefix: input.servicePrefix,
}),
@@ -203,11 +276,9 @@ export async function ensureEveVercelOutputConfig(input: {
outputConfigPath,
VERCEL_OUTPUT_CONFIG_FILE_NAME,
)) as VercelOutputConfig;
- const nextEntrypoint = ".";
const eveEntrypoint = resolveRelativeEntrypoint(input.nextRoot, input.appRoot);
- const existingServices = existingConfig.experimentalServices ?? {};
- const configuredEveService = findServiceByFramework(existingServices, "eve");
- const configuredNextService = findServiceByFramework(existingServices, "nextjs");
+ const existingServices = existingConfig.services ?? {};
+ const configuredEveServiceEntry = findServiceEntryByFramework(existingServices, "eve");
const servicePrefix = resolveConfiguredServicePrefix({
services: existingServices,
servicePrefix: input.servicePrefix,
@@ -219,33 +290,27 @@ export async function ensureEveVercelOutputConfig(input: {
};
}
- const experimentalServices: Record<string, VercelServiceConfig> = {
+ const services: Record<string, VercelServiceConfig> = {
...existingServices,
};
+ let eveServiceName = configuredEveServiceEntry?.name ?? EVE_SERVICE_NAME;
- if (configuredNextService === undefined) {
- experimentalServices.web = {
- entrypoint: nextEntrypoint,
- framework: "nextjs",
- mount: "/",
- type: "web",
- };
- }
-
- if (configuredEveService === undefined) {
- experimentalServices.eve = {
+ if (configuredEveServiceEntry === undefined) {
+ services[EVE_SERVICE_NAME] = {
buildCommand: input.eveBuildCommand,
- entrypoint: eveEntrypoint,
+ entrypoint: "package.json",
framework: "eve",
- mount: servicePrefix,
- type: "web",
+ root: eveEntrypoint,
};
+ eveServiceName = EVE_SERVICE_NAME;
}
+ const { services: _services, ...configWithoutLegacyServices } = existingConfig;
const vercelConfig: VercelOutputConfig = {
- ...existingConfig,
+ ...configWithoutLegacyServices,
+ routes: insertEveServiceRoute(existingConfig.routes ?? [], eveServiceName),
+ services,
version: VERCEL_BUILD_OUTPUT_VERSION,
- experimentalServices,
};
if (JSON.stringify(existingConfig) !== JSON.stringify(vercelConfig)) {