Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r is not a function when wrapping API route with a function and use server #68568

Open
Floriferous opened this issue Aug 6, 2024 · 1 comment
Labels
bug Issue was opened via the bug report template. Developer Experience Issues related to Next.js logs, Error overlay, etc.

Comments

@Floriferous
Copy link

Floriferous commented Aug 6, 2024

Link to the code that reproduces this issue

https://github.com/Floriferous/next-api-bug

To Reproduce

  • Clone this repo: https://github.com/Floriferous/next-api-bug
  • Run it with npm run dev
  • Visit '/' with your browser
  • Notice the error r is not a function in the terminal
  • When you remove 'use server', the API route starts working again

Current vs. Expected behavior

This is a reopening of #67833, as I was still experiencing the bug even after doing what @icyJoseph suggested. The issue is more complex, as it happens because I had 'use server' at the top of the wrapper function's file. I would expect to have some other kind of error, or something that helps in understanding that this pattern is not allowed.

When adding use server to a file, it seems to do weird things with your functions, and create really difficult to understand errors and error stacks:

I've defined 2 files:

// my API route

import { apiRoute } from "../helpers";

export const GET = apiRoute(async () => {
  console.log('wut?');
  
  return new Response({ hello: 'world' });
})
// The apiRoute function
'use server'; // This is the mistake

export const apiRoute =
  (handler: any) =>
  async (...args) => {
    logStuff();
    return handler(...args);
  };

Using these 2 together is somehow not allowed, and the error message does not make much sense.

Here's the error stack:

 ⨯ TypeError: r is not a function
    at /Users/flo/dev/next-api-bug/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:55044
    at /Users/flo/dev/next-api-bug/node_modules/next/dist/server/lib/trace/tracer.js:140:36
    at NoopContextManager.with (/Users/flo/dev/next-api-bug/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:7062)
    at ContextAPI.with (/Users/flo/dev/next-api-bug/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:518)
    at NoopTracer.startActiveSpan (/Users/flo/dev/next-api-bug/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:18093)
    at ProxyTracer.startActiveSpan (/Users/flo/dev/next-api-bug/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:18854)
    at /Users/flo/dev/next-api-bug/node_modules/next/dist/server/lib/trace/tracer.js:122:103
    at NoopContextManager.with (/Users/flo/dev/next-api-bug/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:7062)
    at ContextAPI.with (/Users/flo/dev/next-api-bug/node_modules/next/dist/compiled/@opentelemetry/api/index.js:1:518)
    at NextTracerImpl.trace (/Users/flo/dev/next-api-bug/node_modules/next/dist/server/lib/trace/tracer.js:122:28)
    at /Users/flo/dev/next-api-bug/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:47853
    at AsyncLocalStorage.run (node:async_hooks:346:14)
    at Object.wrap (/Users/flo/dev/next-api-bug/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:40244)
    at /Users/flo/dev/next-api-bug/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:46429
    at AsyncLocalStorage.run (node:async_hooks:346:14)
    at Object.wrap (/Users/flo/dev/next-api-bug/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:37583)
    at /Users/flo/dev/next-api-bug/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:46391
    at AsyncLocalStorage.run (node:async_hooks:346:14)
    at ek.execute (/Users/flo/dev/next-api-bug/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:45838)
    at ek.handle (/Users/flo/dev/next-api-bug/node_modules/next/dist/compiled/next-server/app-route.runtime.dev.js:6:56303)
    at doRender (/Users/flo/dev/next-api-bug/node_modules/next/dist/server/base-server.js:1377:60)
    at cacheEntry.responseCache.get.routeKind (/Users/flo/dev/next-api-bug/node_modules/next/dist/server/base-server.js:1599:34)
    at ResponseCache.get (/Users/flo/dev/next-api-bug/node_modules/next/dist/server/response-cache/index.js:49:26)
    at DevServer.renderToResponseWithComponentsImpl (/Users/flo/dev/next-api-bug/node_modules/next/dist/server/base-server.js:1507:53)
    at /Users/flo/dev/next-api-bug/node_modules/next/dist/server/base-server.js:1002:121
    at NextTracerImpl.trace (/Users/flo/dev/next-api-bug/node_modules/next/dist/server/lib/trace/tracer.js:105:20)
    at DevServer.renderToResponseWithComponents (/Users/flo/dev/next-api-bug/node_modules/next/dist/server/base-server.js:1002:41)
    at DevServer.renderPageComponent (/Users/flo/dev/next-api-bug/node_modules/next/dist/server/base-server.js:1931:35)
    at async DevServer.renderToResponseImpl (/Users/flo/dev/next-api-bug/node_modules/next/dist/server/base-server.js:1969:32)
    at async DevServer.pipeImpl (/Users/flo/dev/next-api-bug/node_modules/next/dist/server/base-server.js:920:25)
    at async NextNodeServer.handleCatchallRenderRequest (/Users/flo/dev/next-api-bug/node_modules/next/dist/server/next-server.js:272:17)
    at async DevServer.handleRequestImpl (/Users/flo/dev/next-api-bug/node_modules/next/dist/server/base-server.js:816:17)
    at async /Users/flo/dev/next-api-bug/node_modules/next/dist/server/dev/next-dev-server.js:339:20
    at async Span.traceAsyncFn (/Users/flo/dev/next-api-bug/node_modules/next/dist/trace/trace.js:154:20)
    at async DevServer.handleRequest (/Users/flo/dev/next-api-bug/node_modules/next/dist/server/dev/next-dev-server.js:336:24)
    at async invokeRender (/Users/flo/dev/next-api-bug/node_modules/next/dist/server/lib/router-server.js:174:21)
    at async handleRequest (/Users/flo/dev/next-api-bug/node_modules/next/dist/server/lib/router-server.js:353:24)
    at async requestHandlerImpl (/Users/flo/dev/next-api-bug/node_modules/next/dist/server/lib/router-server.js:377:13)
    at async Server.requestListener (/Users/flo/dev/next-api-bug/node_modules/next/dist/server/lib/start-server.js:141:13)
 GET /api/stuff 500 in 153ms

Provide environment information

Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 23.5.0: Wed May  1 20:19:05 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T8112
  Available memory (MB): 24576
  Available CPU cores: 8
Binaries:
  Node: 20.11.0
  npm: 10.7.0
  Yarn: 1.21.1
  pnpm: N/A
Relevant Packages:
  next: 14.2.5 // Latest available version is detected (14.2.5).
  eslint-config-next: 14.2.5
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.5.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Developer Experience

Which stage(s) are affected? (Select all that apply)

next dev (local)

Additional context

No response

@Floriferous Floriferous added the bug Issue was opened via the bug report template. label Aug 6, 2024
@github-actions github-actions bot added the Developer Experience Issues related to Next.js logs, Error overlay, etc. label Aug 6, 2024
@icyJoseph
Copy link
Contributor

Yeah, I'd say, refactor by moving your helper function into a module without the use server annotation.

There's a longer explanation, but I'm out of the - city... In short, server actions are asynchronous because they represent work triggered on the server, by the client.

Because the underlying network calls are always asynchronous, 'use server' can only be used on async functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Developer Experience Issues related to Next.js logs, Error overlay, etc.
Projects
None yet
Development

No branches or pull requests

2 participants