Skip to content

Commit 290f300

Browse files
committed
refactor: update SWR options for improved caching and performance across components
1 parent 802515f commit 290f300

8 files changed

Lines changed: 55 additions & 41 deletions

File tree

src/hooks/SubscriptionProvider.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ export function SubscriptionProviderMain({ children }: { children: React.ReactNo
3232
revalidateIfStale: false,
3333
revalidateOnFocus: false,
3434
revalidateOnReconnect: false,
35-
dedupingInterval: 15000,
36-
focusThrottleInterval: 30000,
37-
errorRetryInterval: 10000,
35+
dedupingInterval: 60000,
36+
focusThrottleInterval: 120000,
37+
errorRetryInterval: 30000,
3838
errorRetryCount: 1,
3939
},
4040
)

src/lib/hooks/useUpdateSetting.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ export const SETTINGS_SWR_OPTIONS = {
1919
revalidateIfStale: false,
2020
revalidateOnFocus: false,
2121
revalidateOnReconnect: false,
22-
dedupingInterval: 15000,
23-
focusThrottleInterval: 30000,
24-
errorRetryInterval: 10000,
22+
dedupingInterval: 60000,
23+
focusThrottleInterval: 120000,
24+
errorRetryInterval: 30000,
2525
errorRetryCount: 1,
2626
} as const
2727

src/middleware.ts

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,63 @@ export const config = {
1515
'/dashboard/:path*',
1616
'/dashboard/admin/:path*',
1717
'/install',
18-
'/[username]',
18+
'/:username',
1919
],
2020
}
2121

2222
export async function middleware(req: NextRequestWithAuth) {
23+
const pathname = req.nextUrl.pathname
24+
2325
// Redirect literal [username] path to login page
24-
if (req.nextUrl.pathname === '/[username]') {
26+
if (pathname === '/[username]') {
2527
return NextResponse.redirect(new URL('/login', req.url))
2628
}
2729

28-
if (req.nextUrl.pathname === '/install') {
30+
if (pathname === '/install') {
2931
return NextResponse.redirect(new URL('/api/install', req.url))
3032
}
3133

32-
if (req.nextUrl.pathname === '/dashboard/troubleshoot') {
34+
if (pathname === '/dashboard/troubleshoot') {
3335
return NextResponse.redirect(new URL('/dashboard/help', req.url))
3436
}
3537

36-
if (!process.env.EDGE_CONFIG) {
37-
req.nextUrl.pathname = '/missing-edge-config'
38-
return NextResponse.rewrite(req.nextUrl)
39-
}
38+
const shouldCheckMaintenance =
39+
pathname === '/' ||
40+
pathname === '/login' ||
41+
pathname === '/verify' ||
42+
pathname === '/install' ||
43+
pathname === '/[username]' ||
44+
pathname.startsWith('/overlay')
4045

41-
try {
42-
// Check whether the maintenance page should be shown
43-
const isInMaintenanceMode = await get<boolean>('isInMaintenanceMode')
46+
if (shouldCheckMaintenance) {
47+
if (!process.env.EDGE_CONFIG) {
48+
req.nextUrl.pathname = '/missing-edge-config'
49+
return NextResponse.rewrite(req.nextUrl)
50+
}
4451

45-
// If is in maintenance mode, point the url pathname to the maintenance page
46-
if (isInMaintenanceMode && process.env.VERCEL_ENV === 'production') {
47-
// Check if the path starts with 'overlay' and return an empty page
48-
if (req.nextUrl.pathname.startsWith('/overlay')) {
49-
return new NextResponse(null, { status: 200 })
50-
}
52+
try {
53+
// Check whether the maintenance page should be shown
54+
const isInMaintenanceMode = await get<boolean>('isInMaintenanceMode')
5155

52-
req.nextUrl.pathname = '/maintenance'
53-
return NextResponse.rewrite(req.nextUrl)
56+
// If is in maintenance mode, point the url pathname to the maintenance page
57+
if (isInMaintenanceMode && process.env.VERCEL_ENV === 'production') {
58+
// Check if the path starts with 'overlay' and return an empty page
59+
if (pathname.startsWith('/overlay')) {
60+
return new NextResponse(null, { status: 200 })
61+
}
62+
63+
req.nextUrl.pathname = '/maintenance'
64+
return NextResponse.rewrite(req.nextUrl)
65+
}
66+
} catch (error) {
67+
captureException(error)
68+
// show the default page if EDGE_CONFIG env var is missing,
69+
// but log the error to the console
70+
console.error(error)
5471
}
55-
} catch (error) {
56-
captureException(error)
57-
// show the default page if EDGE_CONFIG env var is missing,
58-
// but log the error to the console
59-
console.error(error)
6072
}
61-
if (req.nextUrl.pathname.startsWith('/dashboard') || req.nextUrl.pathname.endsWith('/overlay')) {
73+
74+
if (pathname.startsWith('/dashboard') || pathname.endsWith('/overlay')) {
6275
// Use the token in the request directly to check role
6376
// withAuth already adds the token and user to the request
6477
const onlyChatterRole = await withAuth(req, {

src/pages/api/lastfm/now-playing.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@ async function handler(
1717
res: NextApiResponse<LastFmResponse | { error: string }>,
1818
) {
1919
try {
20-
const session = await getServerSession(req, res, authOptions)
21-
const isPublicOverlayRequest = (Boolean(req.query.id) || Boolean(req.query.token)) && !session?.user?.id
20+
const publicUserId = (req.query.id as string) || (req.query.token as string)
21+
const session = publicUserId ? null : await getServerSession(req, res, authOptions)
22+
const isPublicOverlayRequest = Boolean(publicUserId)
2223
if (isPublicOverlayRequest) {
2324
res.setHeader('Cache-Control', 'public, s-maxage=15, stale-while-revalidate=45')
2425
} else {
2526
res.setHeader('Cache-Control', 'private, max-age=15, stale-while-revalidate=30')
2627
}
2728

28-
const userId = (req.query.id as string) || session?.user?.id || (req.query.token as string)
29+
const userId = publicUserId || session?.user?.id
2930

3031
if (!userId) {
3132
return res.status(400).json({ error: 'Username is required' })

src/pages/api/stripe/subscription.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import { getSubscription, isSubscriptionActive, SUBSCRIPTION_TIERS } from '@/uti
66

77
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
88
try {
9-
const session = await getServerSession(req, res, authOptions)
9+
const userId = req.query.id as string | undefined
10+
const session = userId ? null : await getServerSession(req, res, authOptions)
1011

1112
// Get the effective user ID
1213
// When impersonating, we want the subscription of the user being viewed (query param)
1314
// When not impersonating, we want the current user's subscription
14-
const userId = req.query.id as string | undefined
1515
const userIdToUse = userId || session?.user?.id
1616

17-
const isPublicOverlayRequest = Boolean(userId) && !session?.user?.id
17+
const isPublicOverlayRequest = Boolean(userId)
1818
if (isPublicOverlayRequest) {
1919
res.setHeader('Cache-Control', 'public, s-maxage=30, stale-while-revalidate=120')
2020
} else {

src/pages/login-as-bot.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { chatBotScopes } from '@/lib/authScopes'
99
import type { NextPageWithLayout } from '@/pages/_app'
1010

1111
const Login: NextPageWithLayout = () => {
12-
const { status } = useSession()
12+
useSession()
1313
const router = useRouter()
1414
const [countdown, setCountdown] = useState(6)
1515
const [isLoading, setIsLoading] = useState(false)

src/pages/login.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { NextPageWithLayout } from '@/pages/_app'
1111

1212
const Login: NextPageWithLayout = () => {
1313
const { status } = useSession()
14-
const { message } = App.useApp()
14+
App.useApp()
1515
const router = useRouter()
1616
const { notification } = App.useApp()
1717

src/pages/verify.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ const VerifyPage: NextPageWithLayout = () => {
9494
const [checkingAccount, setCheckingAccount] = useState(false)
9595
const [accountToUnlink, setAccountToUnlink] = useState<string | null>(null)
9696
const [unlinkModalVisible, setUnlinkModalVisible] = useState(false)
97-
const { notification, modal } = App.useApp()
97+
const { notification } = App.useApp()
9898
const track = useTrack()
9999
const [actionLoading, setActionLoading] = useState<{ [key: string]: boolean }>({})
100100
const [isSigningOut, setIsSigningOut] = useState(false)
@@ -205,7 +205,7 @@ const VerifyPage: NextPageWithLayout = () => {
205205
throw new Error(error.message || 'Steam authentication validation failed')
206206
}
207207

208-
const { steam32Id, profileData } = await response.json()
208+
const { steam32Id } = await response.json()
209209

210210
if (steam32Id) {
211211
// Show success notification

0 commit comments

Comments
 (0)