Unverified Commit fc4f062b authored by Jon Meyers's avatar Jon Meyers Committed by GitHub

Chore: Update with-supabase example to Next.js 16 (#86105)

### What?

Upgrade with-supabase example to be compatible with Next.js 16

### Why?

1. Users get console warnings about middleware vs proxy
2. Enabling Cache Components config results in build errors for async
behaviour outside Suspense boundary
3. Turbopack flag is default and therefore, no longer necessary on `npm
run dev`

### How?

1. Renamed all instances of `middleware` to `proxy`
2. Wraped async behaviour in Suspense boundaries
3. Removed --turbopack flag from `npm run dev`

---------
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent dc8f7737
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
- Works across the entire [Next.js](https://nextjs.org) stack - Works across the entire [Next.js](https://nextjs.org) stack
- App Router - App Router
- Pages Router - Pages Router
- Middleware - Proxy
- Client - Client
- Server - Server
- It just works! - It just works!
......
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Suspense } from "react";
export default async function Page({ async function ErrorContent({
searchParams, searchParams,
}: { }: {
searchParams: Promise<{ error: string }>; searchParams: Promise<{ error: string }>;
}) { }) {
const params = await searchParams; const params = await searchParams;
return (
<>
{params?.error ? (
<p className="text-sm text-muted-foreground">
Code error: {params.error}
</p>
) : (
<p className="text-sm text-muted-foreground">
An unspecified error occurred.
</p>
)}
</>
);
}
export default function Page({
searchParams,
}: {
searchParams: Promise<{ error: string }>;
}) {
return ( return (
<div className="flex min-h-svh w-full items-center justify-center p-6 md:p-10"> <div className="flex min-h-svh w-full items-center justify-center p-6 md:p-10">
<div className="w-full max-w-sm"> <div className="w-full max-w-sm">
...@@ -18,15 +39,9 @@ export default async function Page({ ...@@ -18,15 +39,9 @@ export default async function Page({
</CardTitle> </CardTitle>
</CardHeader> </CardHeader>
<CardContent> <CardContent>
{params?.error ? ( <Suspense>
<p className="text-sm text-muted-foreground"> <ErrorContent searchParams={searchParams} />
Code error: {params.error} </Suspense>
</p>
) : (
<p className="text-sm text-muted-foreground">
An unspecified error occurred.
</p>
)}
</CardContent> </CardContent>
</Card> </Card>
</div> </div>
......
...@@ -7,6 +7,7 @@ import { ConnectSupabaseSteps } from "@/components/tutorial/connect-supabase-ste ...@@ -7,6 +7,7 @@ import { ConnectSupabaseSteps } from "@/components/tutorial/connect-supabase-ste
import { SignUpUserSteps } from "@/components/tutorial/sign-up-user-steps"; import { SignUpUserSteps } from "@/components/tutorial/sign-up-user-steps";
import { hasEnvVars } from "@/lib/utils"; import { hasEnvVars } from "@/lib/utils";
import Link from "next/link"; import Link from "next/link";
import { Suspense } from "react";
export default function Home() { export default function Home() {
return ( return (
...@@ -20,7 +21,13 @@ export default function Home() { ...@@ -20,7 +21,13 @@ export default function Home() {
<DeployButton /> <DeployButton />
</div> </div>
</div> </div>
{!hasEnvVars ? <EnvVarWarning /> : <AuthButton />} {!hasEnvVars ? (
<EnvVarWarning />
) : (
<Suspense>
<AuthButton />
</Suspense>
)}
</div> </div>
</nav> </nav>
<div className="flex-1 flex flex-col gap-20 max-w-5xl p-5"> <div className="flex-1 flex flex-col gap-20 max-w-5xl p-5">
......
...@@ -4,6 +4,7 @@ import { AuthButton } from "@/components/auth-button"; ...@@ -4,6 +4,7 @@ import { AuthButton } from "@/components/auth-button";
import { ThemeSwitcher } from "@/components/theme-switcher"; import { ThemeSwitcher } from "@/components/theme-switcher";
import { hasEnvVars } from "@/lib/utils"; import { hasEnvVars } from "@/lib/utils";
import Link from "next/link"; import Link from "next/link";
import { Suspense } from "react";
export default function ProtectedLayout({ export default function ProtectedLayout({
children, children,
...@@ -21,7 +22,13 @@ export default function ProtectedLayout({ ...@@ -21,7 +22,13 @@ export default function ProtectedLayout({
<DeployButton /> <DeployButton />
</div> </div>
</div> </div>
{!hasEnvVars ? <EnvVarWarning /> : <AuthButton />} {!hasEnvVars ? (
<EnvVarWarning />
) : (
<Suspense>
<AuthButton />
</Suspense>
)}
</div> </div>
</nav> </nav>
<div className="flex-1 flex flex-col gap-20 max-w-5xl p-5"> <div className="flex-1 flex flex-col gap-20 max-w-5xl p-5">
......
...@@ -3,15 +3,20 @@ import { redirect } from "next/navigation"; ...@@ -3,15 +3,20 @@ import { redirect } from "next/navigation";
import { createClient } from "@/lib/supabase/server"; import { createClient } from "@/lib/supabase/server";
import { InfoIcon } from "lucide-react"; import { InfoIcon } from "lucide-react";
import { FetchDataSteps } from "@/components/tutorial/fetch-data-steps"; import { FetchDataSteps } from "@/components/tutorial/fetch-data-steps";
import { Suspense } from "react";
export default async function ProtectedPage() { async function UserDetails() {
const supabase = await createClient(); const supabase = await createClient();
const { data, error } = await supabase.auth.getClaims(); const { data, error } = await supabase.auth.getClaims();
if (error || !data?.claims) { if (error || !data?.claims) {
redirect("/auth/login"); redirect("/auth/login");
} }
return JSON.stringify(data.claims, null, 2);
}
export default function ProtectedPage() {
return ( return (
<div className="flex-1 w-full flex flex-col gap-12"> <div className="flex-1 w-full flex flex-col gap-12">
<div className="w-full"> <div className="w-full">
...@@ -24,7 +29,9 @@ export default async function ProtectedPage() { ...@@ -24,7 +29,9 @@ export default async function ProtectedPage() {
<div className="flex flex-col gap-2 items-start"> <div className="flex flex-col gap-2 items-start">
<h2 className="font-bold text-2xl mb-4">Your user details</h2> <h2 className="font-bold text-2xl mb-4">Your user details</h2>
<pre className="text-xs font-mono p-3 rounded border max-h-32 overflow-auto"> <pre className="text-xs font-mono p-3 rounded border max-h-32 overflow-auto">
{JSON.stringify(data.claims, null, 2)} <Suspense>
<UserDetails />
</Suspense>
</pre> </pre>
</div> </div>
<div> <div>
......
...@@ -7,7 +7,7 @@ export async function updateSession(request: NextRequest) { ...@@ -7,7 +7,7 @@ export async function updateSession(request: NextRequest) {
request, request,
}); });
// If the env vars are not set, skip middleware check. You can remove this // If the env vars are not set, skip proxy check. You can remove this
// once you setup the project. // once you setup the project.
if (!hasEnvVars) { if (!hasEnvVars) {
return supabaseResponse; return supabaseResponse;
......
...@@ -24,7 +24,7 @@ export async function createClient() { ...@@ -24,7 +24,7 @@ export async function createClient() {
); );
} catch { } catch {
// The `setAll` method was called from a Server Component. // The `setAll` method was called from a Server Component.
// This can be ignored if you have middleware refreshing // This can be ignored if you have proxy refreshing
// user sessions. // user sessions.
} }
}, },
......
import type { NextConfig } from "next"; import type { NextConfig } from "next";
const nextConfig: NextConfig = { const nextConfig: NextConfig = {
/* config options here */ cacheComponents: true,
}; };
export default nextConfig; export default nextConfig;
{ {
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "next dev --turbopack", "dev": "next dev",
"build": "next build", "build": "next build",
"start": "next start", "start": "next start",
"lint": "eslint ." "lint": "eslint ."
......
import { updateSession } from "@/lib/supabase/middleware"; import { updateSession } from "@/lib/supabase/proxy";
import { type NextRequest } from "next/server"; import { type NextRequest } from "next/server";
export async function middleware(request: NextRequest) { export async function proxy(request: NextRequest) {
return await updateSession(request); return await updateSession(request);
} }
......
...@@ -23,6 +23,12 @@ ...@@ -23,6 +23,12 @@
"@/*": ["./*"] "@/*": ["./*"]
} }
}, },
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], "include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts",
".next/dev/types/**/*.ts"
],
"exclude": ["node_modules"] "exclude": ["node_modules"]
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment