Skip to content

Commit d675083

Browse files
committed
fix: memory leak by creating new anon supabase client every time
1 parent 4ace695 commit d675083

File tree

3 files changed

+42
-33
lines changed

3 files changed

+42
-33
lines changed

src/components/Card/CardPerson.tsx

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
'use client';
1+
'use client'
2+
23
import * as React from "react"
34
import { cn } from "@/lib/utils";
45
import { Card } from "../ui/card";
@@ -7,8 +8,8 @@ import { ImageWithFallback } from "../utils/ImageWithFallback";
78
import { useRouter } from "@/lib/i18n/navigation";
89
import { Button } from "../ui/button";
910
import { BadgeMedia } from "../Badge/BadgeMedia";
10-
import { DateOnlyYearTooltip } from "../utils/Date";
1111
import { WithLink } from "../utils/WithLink";
12+
import { getTmdbImage } from "@/lib/tmdb/getTmdbImage";
1213

1314
interface CardPersonProps
1415
extends React.ComponentProps<typeof Card> {
@@ -38,16 +39,12 @@ const CardPersonDefault = React.forwardRef<
3839
className={cn('relative h-full shrink-0 overflow-hidden aspect-square rounded-full', posterClassName)}
3940
>
4041
<ImageWithFallback
41-
src={person.profile_url ?? ''}
42+
src={getTmdbImage({ path: person.profile_path, size: 'w342' })}
4243
alt={person.name ?? ''}
4344
fill
4445
className="object-cover"
4546
type="person"
46-
sizes={`
47-
(max-width: 640px) 96px,
48-
(max-width: 1024px) 120px,
49-
150px
50-
`}
47+
unoptimized
5148
/>
5249
</div>
5350
<div className='px-2 py-1 space-y-1'>
@@ -76,16 +73,12 @@ const CardPersonRow = React.forwardRef<
7673
>
7774
<div className={cn("relative w-24 aspect-2/3 rounded-md overflow-hidden", posterClassName)}>
7875
<ImageWithFallback
79-
src={person.profile_url ?? ''}
80-
alt={person.name ?? ''}
81-
fill
82-
className="object-cover"
83-
type={'person'}
84-
sizes={`
85-
(max-width: 640px) 96px,
86-
(max-width: 1024px) 120px,
87-
150px
88-
`}
76+
src={getTmdbImage({ path: person.profile_path, size: 'w342' })}
77+
alt={person.name ?? ''}
78+
fill
79+
className="object-cover"
80+
type={'person'}
81+
unoptimized
8982
/>
9083
</div>
9184
<div className="flex items-center gap-4 justify-between w-full">

src/lib/supabase/anon.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
import { Database } from '@recomendapp/types';
2-
import { createClient as createClientSupabase } from '@supabase/supabase-js';
2+
import { createClient as createClientSupabase, SupabaseClient } from '@supabase/supabase-js';
33
import { SUPABASE_ANON_KEY, SUPABASE_URL } from '../env';
44

5-
export const createAnonClient = (locale?: string) => {
6-
return createClientSupabase<Database>(
7-
SUPABASE_URL,
8-
SUPABASE_ANON_KEY,
9-
locale ? {
10-
global: {
11-
headers: {
12-
'language': locale,
13-
}
14-
}
15-
} : undefined
16-
);
5+
const clientCache = new Map<string, SupabaseClient<Database>>();
6+
7+
export const createAnonClient = (locale: string = 'default') => {
8+
if (clientCache.has(locale)) {
9+
return clientCache.get(locale)!;
10+
}
11+
12+
const newClient = createClientSupabase<Database>(
13+
SUPABASE_URL,
14+
SUPABASE_ANON_KEY,
15+
{
16+
auth: {
17+
persistSession: false,
18+
autoRefreshToken: false,
19+
detectSessionInUrl: false,
20+
},
21+
global: {
22+
headers: {
23+
'language': locale,
24+
}
25+
}
26+
}
27+
);
28+
29+
clientCache.set(locale, newClient);
30+
31+
return newClient;
1732
};

src/lib/supabase/server.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import {
55
import { Database } from '@recomendapp/types';
66
import { SUPABASE_ANON_KEY, SUPABASE_URL } from '../env';
77
import { getLocale } from 'next-intl/server';
8+
import { cache } from 'react';
89

9-
export const createServerClient = async () => {
10+
export const createServerClient = cache(async () => {
1011
const cookieStore = await cookies();
1112
const locale = await getLocale();
1213
const client = createServerClientSupabase<Database>(
@@ -40,4 +41,4 @@ export const createServerClient = async () => {
4041
(client.auth as any).suppressGetSessionWarning = true;
4142

4243
return client;
43-
};
44+
});

0 commit comments

Comments
 (0)