The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Blog: Nextjs 14 (#18612)
Jon Meyersec2ac6e9bcd5
diff --git a/apps/www/_blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14.mdx b/apps/www/_blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14.mdxnew file mode 100644index 00000000000..42b0f92e9ca--- /dev/null+++ b/apps/www/_blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14.mdx@@ -0,0 +1,200 @@+---+title: Supabase is now compatible with Next.js 14+description: The fastest way to build apps with Next.js 14 and Supabase+author: jonmeyers_io+categories:+ - product+tags:+ - nextjs+ - intgrations+date: '2023-11-01'+toc_depth: 3+image: 2023-11-01-supabase-is-now-compatible-with-nextjs-14/nextjs-compatible.png+thumb: 2023-11-01-supabase-is-now-compatible-with-nextjs-14/nextjs-compatible.png+---++As part of [Next.js Conf 2023](https://nextjs.org/conf), the team at Vercel released [Next.js 14](https://nextjs.org/blog/next-14). The huge headline feature was...++<a href="https://x.com/leeerob/status/1717596944623649198?s=20" target="_blank">+ <img+ className="dark:hidden !m-0"+ alt="Tweet from Lee Robinson about Next.js 14 containing no new APIs - light mode version"+ src="/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/lee-tweet-light.png"+ />++ <img+ className="hidden dark:block !m-0"+ alt="Tweet from Lee Robinson about Next.js 14 containing no new APIs - dark mode version"+ src="/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/lee-tweet-dark.png"+ />+</a>++That's right, the headline feature is no new features!++This may sound underwhelming at first, but is incredibly good news for the stability of Next.js. This release comes with a huge number of performance and stability improvements—such as [Server Actions](https://nextjs.org/docs/app/api-reference/functions/server-actions) being marked as stable. This means we can finally start promoting this fantastically simple way of authenticating users—entirely server-side!++```tsx+export default async function Page() {+ const signIn = async () => {+ 'use server'+ supabase.auth.signInWithOAuth({...})+ }++ return (+ <form action={signIn}>+ <button>Sign in with GitHub</button>+ </form>+ )+}+```++With [Server Components](https://nextjs.org/docs/app/building-your-application/rendering/server-components), fetching data in Next.js became as simple as:++```tsx+export default async function Page() {+ const { data } = await supabase.from('...').select()+ return ...+}+```++With Server Actions, you can now place mutation logic alongside the Server Components responsible for fetching data and rendering the page:++```tsx+export default async function Page() {+ const { data } = await supabase.from('...').select()++ const createNote = async () => {+ 'use server'+ await supabase.from('...').insert({...})+ }++ return ...+}+```++> To hear more about our thoughts on Next.js Conf and the release of Next.js 14, check out our [Twitter space](https://twitter.com/i/spaces/1yoKMwNWbRjJQ?s=20). [Yuri](https://twitter.com/yuricodesbot), [Alaister](https://twitter.com/alaisteryoung), [Terry](https://twitter.com/saltcod) and [myself](https://twitter.com/jonmeyers_io) talk through how we use Next.js at Supabase and what we personally found most exciting about Next.js Conf and the release of Next.js 14.++## Is Supabase compatible with Next.js 14?++Yes, it is! So much so that [Guillermo Rauch](https://twitter.com/rauchg) shouted us out in the keynote!++<a href="https://x.com/supabase/status/1717658742743761239?s=20" target="_blank">+ <img+ className="dark:hidden !m-0"+ alt="Tweet showing Guillermo Rauch mentioning Supabase as one of the companies compatible with the Next.js App Router - light mode version"+ src="/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/guillermo-tweet-light.png"+ />++ <img+ className="hidden dark:block !m-0"+ alt="Tweet showing Guillermo Rauch mentioning Supabase as one of the companies compatible with the Next.js App Router - dark mode version"+ src="/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/guillermo-tweet-dark.png"+ />+</a>++Since the release of the [App Router](https://nextjs.org/docs/app)—launched as beta with Next.js 13—we have been working closely with the team at Vercel to ensure that Supabase is fully compatible with every part of Next.js.++So for the [App Router](https://nextjs.org/docs/app), that's:++- [Client Components](https://nextjs.org/docs/app/building-your-application/rendering/client-components)+- [Server Components](https://nextjs.org/docs/app/building-your-application/rendering/server-components)+- [Route Handlers](https://nextjs.org/docs/app/building-your-application/routing/route-handlers)+- [Server Actions](https://nextjs.org/docs/app/api-reference/functions/server-actions)+- [Middleware](https://nextjs.org/docs/app/building-your-application/routing/middleware)++And for the [Pages Router](https://nextjs.org/docs/pages):++- [`getServerSideProps` function](https://nextjs.org/docs/pages/building-your-application/data-fetching/get-server-side-props)+- [`getStaticProps` function](https://nextjs.org/docs/pages/building-your-application/data-fetching/get-static-props)+- [API Routes](https://nextjs.org/docs/pages/building-your-application/routing/api-routes)+- [Page Components](https://nextjs.org/docs/pages/building-your-application/data-fetching/client-side)++So why does it require work on the Supabase side to make it compatible with Next.js?++## Configuring Supabase to use Cookies++This works well for client-side apps, but will fail when you try to use [`supabase-js`](https://supabase.com/docs/reference/javascript/introduction) in a Server Component, as there is no concept of 'localStorage' on the server.++To do this, we need to configure `supabase-js` to use cookies instead of `localStorage` when running on the server. But this code is a little verbose to ask people to duplicate across every app they build with Supabase:++```tsx+const supabase = createClient(supabaseUrl, supabaseAnonKey, {+ auth: {+ flowType: 'pkce',+ autoRefreshToken: false,+ detectSessionInUrl: false,+ persistSession: true,+ storage: {+ getItem: async (key: string) => {+ cookieStore.get(key)+ },+ setItem: async (key: string, value: string) => {+ cookieStore.set(key, value)+ },+ removeItem: async (key: string) => {+ cookieStore.remove(key)+ },+ },+ },+})+```++That takes care of the server-side pieces of Next.js, but since we recommend securing your apps with [Row Level Security (RLS) policies](https://www.youtube.com/watch?v=Ow_Uzedfohk), you can safely access your user's session client-side too. Therefore, we need to tell the browser how access that cookie too:++```tsx+const supabase = createClient(supabaseUrl, supabaseAnonKey, {+ auth: {+ flowType: 'pkce',+ autoRefreshToken: true,+ detectSessionInUrl: true,+ persistSession: true,+ storage: {+ getItem: async (key: string) => {+ return parse(document.cookie[key])+ },+ setItem: async (key: string, value: string) => {+ document.cookie = serialize(key, value)+ },+ },+ removeItem: async (key: string) => {+ document.cookie = serialize(key, '', {+ maxAge: 0,+ })+ },+ },+})+```++That is a lot of very confusing code! So we decided to [create a package called `@supabase/ssr`](https://supabase.com/docs/guides/auth/server-side/overview) that does all of this for you. Then we took it one step further and created a [Next.js and Supabase Starter Template](https://supabase.com/docs/guides/getting-started/quickstarts/nextjs), so you can just focus on building your awesome app! 🚀++> Check out my [Next.js Conf talk](https://www.youtube.com/watch?t=3880&v=FdiX5rHS_0Y) to see this starter template in action!++## How do I get started?++One command:++```bash+npx create-next-app@latest -e with-supabase+```++The landing page will guide you through the process of creating a Supabase project and configuring your environment variables.++Build ~~in a weekend~~ on a Friday night! Scale to millions!++## Meme zone++No Supabase blog is complete without a meme signing off. This one is of the least controversial command coming out of Next.js Conf:++<a href="https://x.com/saltcod/status/1718959967955275843?s=20" target="_blank">+ <img+ className="dark:hidden !m-0"+ alt="Tweet from Terry Sutton running the `with-supabase` command - light mode version"+ src="/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/terry-tweet-light.png"+ />++ <img+ className="hidden dark:block !m-0"+ alt="Tweet from Terry Sutton running the `with-supabase` commands - dark mode version"+ src="/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/terry-tweet-dark.png"+ />+</a>diff --git a/apps/www/public/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/guillermo-tweet-dark.png b/apps/www/public/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/guillermo-tweet-dark.pngnew file mode 100644index 00000000000..fa8a6e660baBinary files /dev/null and b/apps/www/public/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/guillermo-tweet-dark.png differdiff --git a/apps/www/public/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/guillermo-tweet-light.png b/apps/www/public/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/guillermo-tweet-light.pngnew file mode 100644index 00000000000..919fb195920Binary files /dev/null and b/apps/www/public/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/guillermo-tweet-light.png differdiff --git a/apps/www/public/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/lee-tweet-dark.png b/apps/www/public/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/lee-tweet-dark.pngnew file mode 100644index 00000000000..4804d38f559Binary files /dev/null and b/apps/www/public/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/lee-tweet-dark.png differdiff --git a/apps/www/public/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/lee-tweet-light.png b/apps/www/public/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/lee-tweet-light.pngnew file mode 100644index 00000000000..fcba4b1ad6dBinary files /dev/null and b/apps/www/public/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/lee-tweet-light.png differdiff --git a/apps/www/public/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/nextjs-compatible.png b/apps/www/public/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/nextjs-compatible.pngnew file mode 100644index 00000000000..05cacb0c68eBinary files /dev/null and b/apps/www/public/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/nextjs-compatible.png differdiff --git a/apps/www/public/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/terry-tweet-dark.png b/apps/www/public/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/terry-tweet-dark.pngnew file mode 100644index 00000000000..75d3b5d4c7eBinary files /dev/null and b/apps/www/public/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/terry-tweet-dark.png differdiff --git a/apps/www/public/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/terry-tweet-light.png b/apps/www/public/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/terry-tweet-light.pngnew file mode 100644index 00000000000..64894b94ad8Binary files /dev/null and b/apps/www/public/images/blog/2023-11-01-supabase-is-now-compatible-with-nextjs-14/terry-tweet-light.png differ