Skip to content

No agent access from Next.js SSR #804

@devgoldm

Description

@devgoldm

Describe the bug
When using Next.js via OpenNext on Cloudflare Workers, attempting to access a Cloudflare Agent from a Server-Side Rendered page fails at the build stage.

To Reproduce

  1. Checkout this repo https://github.com/talentscore/agents-ssr-issue.git (its a Minimal Reproducible Example)
  2. Run npm run cf-dev
  3. Observe the build error:
Error: Failed to collect configuration for /_not-found
  [cause]: Error: Failed to load external module cloudflare:email: Error: Cannot find module 'cloudflare:email'

Expected behavior
I would expect to be able to access the agent using getAgentByName in SSR components:

import { getAgentByName } from "agents";
import { getCloudflareContext } from "@opennextjs/cloudflare";

async function MyComponent() {
  const { env } = await getCloudflareContext();
  const agent = await getAgentByName(env.FUN_PHRASE_AGENT, "default");
  const phrase = await agent.getFunPhrase();
  // ...
}

This pattern would work fine in server-side worker code.

Version:
"@opennextjs/cloudflare": "^1.15.1",
"agents": "^0.3.6",
"next": "16.1.5",
"react": "19.1.5",
"react-dom": "19.1.5"

Additional context
Thanks @whoiskatrin for being so helpful at the coffee meetup in Soho London! I was recommended to create an issue by her - thanks to the whole team for all the awesome work you do with agents, it feels like the future!

Metadata

Metadata

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