Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Merge branch 'master' into docs/nextjs-server-components

dng26ecdd778ea9
diff --cc apps/docs/pages/guides/auth/auth-helpers/nextjs-server-components.mdx
index da7987bdf14,a49f6c3c2ca..f094dd4f486
--- a/apps/docs/pages/guides/auth/auth-helpers/nextjs-server-components.mdx
+++ b/apps/docs/pages/guides/auth/auth-helpers/nextjs-server-components.mdx
@@@ -196,100 -207,66 +196,110 @@@ We still need a Supabase instance clien
>
<TabPanel id="js" label="JavaScript">
-Create a `/components/supabase-listener.jsx` file and add the following:
+Create a new file at `/utils/supabase-browser.js` and populate with the following:
-```jsx title="/components/supabase-listener.jsx"
+```js title="/utils/supabase-browser.js"
+import { createBrowserSupabaseClient } from '@supabase/auth-helpers-nextjs'
+export const createClient = () => createBrowserSupabaseClient()
+```
+
+</TabPanel>
+
+<TabPanel id="ts" label="TypeScript">
+
+Create a new file at `/utils/supabase-browser.ts` and populate with the following:
+
+```ts
+import { createBrowserSupabaseClient } from '@supabase/auth-helpers-nextjs'
+import { Database } from '../lib/database.types'
+
+export const createClient = () => createBrowserSupabaseClient<Database>()
+```
+
+> TypeScript types can be [generated with the Supabase CLI](https://supabase.com/docs/reference/javascript/typescript-support) and passed to `createBrowserSupabaseClient` to add type support to the Supabase client.
+
+</TabPanel>
+</Tabs>
+
+Next, we need to create a single instance of Supabase to use client-side. Let's create a new Provider for Supabase.
+
+<Tabs
+ scrollable
+ size="small"
+ type="underlined"
+ defaultActiveId="js"
+>
+<TabPanel id="js" label="JavaScript">
+
+```jsx title=components/supabase-provider.jsx
'use client'
-import { useRouter } from 'next/navigation'
-import { useEffect } from 'react'
-import supabase from '../utils/supabase-browser'
+import { createContext, useContext, useState } from 'react'
+import { createClient } from '../utils/supabase-browser'
-export default function SupabaseListener({ accessToken }) {
- const router = useRouter()
+const Context = createContext()
+
+export default function SupabaseProvider({ children }) {
+ const [supabase] = useState(() => createClient())
+ useEffect(() => {
+ const { data: subscription } = supabase.auth.onAuthStateChange((event, session) => {
+ if (session?.access_token !== accessToken) {
+ router.refresh()
+ }
+ })
+
+ return () => subscription.unsubscribe()
+ }, [accessToken])
+
- return null
+ return (
+ <Context.Provider value={{ supabase }}>
+ <>{children}</>
+ </Context.Provider>
+ )
}
+
+export const useSupabase = () => useContext(Context)
```
-> `use client` tells Next.js that this is a [Client Component](https://beta.nextjs.org/docs/rendering/server-and-client-components#client-components). Only Client Components can use hooks like `useEffect` and `useRouter`.
+</TabPanel>
-The function we pass to `onAuthStateChange` is automatically called by Supabase whenever a user's session changes. This component takes an `accessToken` prop, which will be the server's state for our user (we'll set this up next). If the `accessToken` from the server and the new `access_token` do not match then the client and server are out of sync, therefore, we want to reload the active route.
+<TabPanel id="ts" label="TypeScript">
-Lastly, fetch the server-side session in the `RootLayout` and pass it to our new `<SupabaseListener />` component.
+```tsx title=components/supabase-provider.tsx
+'use client'
-```jsx title="/app/layout.jsx"
-import SupabaseListener from '../components/supabase-listener'
-import createClient from '../utils/supabase-server'
+import { createContext, useContext, useState } from 'react'
+import { createClient } from '../utils/supabase-browser'
-export default async function RootLayout({ children }) {
- const supabase = createClient()
+import type { SupabaseClient } from '@supabase/auth-helpers-nextjs'
+import type { Database } from '../lib/database.types'
- const {
- data: { session },
- } = await supabase.auth.getSession()
+type SupabaseContext = {
+ supabase: SupabaseClient<Database>
+}
+
+const Context = createContext<SupabaseContext>()
+
+export default function SupabaseProvider({ children }: { children: React.ReactNode }) {
+ const [supabase] = useState(() => createClient())
return (
- // html and head section omitted
- <body>
- <SupabaseListener accessToken={session?.access_token} />
- {children}
- </body>
+ <Context.Provider value={{ supabase }}>
+ <>{children}</>
+ </Context.Provider>
)
}
+
+export const useSupabase = () => useContext(Context)
```
-We don't want Next.js to cache this `session` value, so we need to export a `revalidate` value of `0` in the layout file.
+> TypeScript types can be [generated with the Supabase CLI](https://supabase.com/docs/reference/javascript/typescript-support) and passed to `createBrowserSupabaseClient` to add type support to the Supabase client.
-```jsx
-export const revalidate = 0
-```
+</TabPanel>
+</Tabs>
-We also want to tell Next.js to explicitly exclude this component's code from the client bundle by installing the `server-only` package:
+We need to set up a listener to fetch fresh data whenever our user logs in or out. For this we need to check whether our client and server sessions match. Let's start by installing the `server-only` package.
```bash
npm install server-only