@clerk/nextjs v4.5
- Category
- Engineering
- Published
New in 4.5: Authentication moves to middleware, switchable runtime support, improved developer experience, preparation for Layouts
Version 4.5 of @clerk/nextjs brings sweeping improvements to server-side authentication in Next.js.
Get started with npm i @clerk/nextjs@latest
Authentication moves to middleware
Going forward, developers will authenticate requests once in Next.js middleware, then simply read the authentication state in endpoints (API routes and getServerSideProps).
middleware.js example:
API routes example:
getServerSideProps example:
With this change, authentication in Next.js will finally feel familiar for developers who have worked with frameworks like Express or Ruby on Rails, where authentication is normally handled within middleware.
Switchable runtime support
Next.js 12.2 brought a switchable runtime, so developers could choose between a node runtime or an edge runtime for each of their API routes and server-rendered pages.
Our new getAuth() helper is isomorphic, so it works regardless of the runtime you choose.
Improved developer experience
If you've used Clerk in the past, you'll notice three small but delightful improvements to our developer experience:
- All server-side imports are now from @clerk/nextjs/server instead of having import paths specific to the endpoint-type
- Wrapper functions are no longer required inside endpoints, only once in middleware
- getAuth(req) works everywhere there's a request object
Preparation for Layouts
Our team has been watching the Next.js Layouts RFC closely, and we designed the new helpers in anticipation of layouts support.
We expect that by computing the authentication state in middleware, we'll be able to share it with each of your parallel-loading layout files.
Get started
Get started right away with our guide to Next.js authentication, or join our community Discord to discuss with our team.
Thanks to the contributors: Mark Pitsilos, Nikos Douvlis
Ready to get started?
Sign up today