-
Notifications
You must be signed in to change notification settings - Fork 46
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
how can use it with next 13? #44
Comments
Did you find a solution to work with next 13 !? |
https://gist.github.com/chemicstry/6a8e15d11517c9c7685a5ea72af2e8f8 This works from both server-side and client-side. It is implemented as react state, but you can extract just the cookie setting functions. |
While @chemicstry solution could work: you could wrap the two behaviors (server vs browser) into a single function. const isSsr = (): boolean => typeof window === 'undefined';
const ssrCookies = isSsr() ? require("next/headers").cookies : undefined; You probably should not, as it is discouraged and considered bad practice. It might be sort-of OK to import It may cause unexpected issues, secret leaks, etc. And even more dangerous: importing your own code might easily leak sensitive data (secrets keys) if you're not carefull. Importing a file that contains an API call with a secret token, will most likely end up in the public js bundle... It is kinda being discussed currently, but it's not very active, and it is not likely to be resolved any time soon IMO. See:
The "supported" approach would be to split your component in two, and provide the SSR cookie as a prop to the Client Component, like so: ServerComponent.tsximport { cookies } from 'next/headers'
export default function ServerComponent() {
const cookie = cookies().get('myCookie')?.value
return (
<ClientComponent initial={{ cookie }} />
)
} ClientComponent.tsx'use client'
import { getCookie } from 'cookies-next'
export default function ClientComponent(props: { inital: { cookie?: string }}) {
const cookie = getCookie('myCookie')?.toString()
return (
<p>{cookie ?? props.initial.cookie}</p>
)
}
} |
Wow, this whole SSR thing is really fragile. Thanks for in depth explanation. Do you know if it's possible to make it work with plain functions instead of react components? To implement |
Unfortunatly, I don't think so. Even in a plain function, in a separate file, you're most likely going to import it in a component somehow, and that's what's important here. You may still choose to do it, with the downsides I mentioned, at your own risk Server Components are still pretty new, it migth get more attention in the near futur. Minifying, tree-shaking, etc, was exclusively used for Client code, where this condition did not make sense... until now Edit: One possible workaround that may be a bit advanced (the compiler doesn't help, you are on your own), so I still woudn't recommend it: |
Finally! I had been looking for this solution for weeks. It was enough to separate the "server" from the "client" |
Hi all! |
is there any documentation to use cookies-next with new version of next.js (app directory). I think this library is still relevant when nextjs official cookies api doesn't yet support client side and the api is server only.
The text was updated successfully, but these errors were encountered: