-
Notifications
You must be signed in to change notification settings - Fork 27.6k
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
[Next13 App dir] Bug with server-only being used in an API route handler #43700
Comments
I think you don’t need to import server only in an api route. Since when used in a api route it is sure to only run on the server. I think the problem is that |
I think there is still an issue with API routes wrongly being confused with client components. I have a function that uses:
and is used in an API route. The function should only be run by server-side code so this is ok, but NextJS thinks that the calling functions (API routes) are client-side:
I can fix by adding import "server-only" to the api files but then I get the same error as @KolbySisk. |
I think it is rightfully a bug, with NextJs confusing the api route as a client component, but I also think that these directives should fail, as you imported same thing for When you try to use them in a different context out of react (i.e api handlers) NextJs is lost and produces and generic incorrect error |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
I also have this bug. Just creating a simple API route like this one:
Error is this:
So NextJS treats this api route component as a client component?! In the beta docs there is no hint that API routes are not working with latest release so that i am still a bit unsure if this is really a bug. NextJS v13.1.1 |
You can only import { cookies } from 'next/headers'; inside of server components and not anywhere else. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Just throwing my name here for a 👍 for this issue, as I just hit it. I use import "server-only" in my backend authentication code to make sure no one imports it in a client component tree. But I need to use the same code in server components as I do in APIs. Unfortunately, server-only fails in API routes, so I can't. I have to move it up to a wrapper or something made just for server components, but then my auth code is vulnerable to someone making an honest mistake. It would be great if I could use server-only to poison my code against client side usage but still have it work with route handlers. |
Route handlers should be fine. This issue in particular is about |
I'm using Currently, I have to comment out |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
…nts targets to be available (#55394) Users want to use `server-only` to restrict the middleware / app routes / pages api, but now it's failing as we're treating them as different webpack layers, but validating the `server-only` only with server components layers. Here we modify the rules a bit to let everyone can use "server-only" for the bundles that targeting server-side. For next-swc transformer, we introduce the new option `bundleType` which only has `"server" | "client" | "default"` 3 values: * - `server` for server-side targets, like server components, app routes, pages api, middleware * - `client` for client components targets such as client components app pages, or page routes under pages directory. * - `default` for environment like jest, we don't validate module graph with swc, replaced the `disable_checks` introduced [#54891](#54891). Refactor a bit webpack-config to adapt to the new rules, after that `server-only` will be able to used in the server-side targets conventions like middleware and `pages/api` Fixes #43700 Fixes #54549 Fixes #52833 Closes NEXT-1616 Closes NEXT-1607 Closes NEXT-1385
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Verify canary release
Provide environment information
Which area(s) of Next.js are affected? (leave empty if unsure)
App directory (appDir: true)
Link to reproduction - Issues with a link to complete (but minimal) reproduction code will be addressed faster
https://github.com/KolbySisk/next-server-only-bug
To Reproduce
2.
npm i server-only
import "server-only";
to the api route.See error:
You're importing a component that needs server-only. That only works in a Server Component but one of its parents is marked with "use client", so it's a Client Component.
Describe the Bug
API routes are handled only on the server, so I would expect
import "server-only";
to work fine.More realistically, I have a function which includes sensitive information that I want to ensure only runs on the server. It can be used in both a server component, and an API route, given the context.
I'm also curious how Next even knows about the relationship between a client component and an API route handler.
Expected Behavior
Code is executed as expected without the error.
Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
NEXT-1385
The text was updated successfully, but these errors were encountered: