fix: limits no longer cache during session changes

This commit is contained in:
Mythie
2023-10-22 11:18:00 +11:00
parent 2f2079e020
commit 1d604fff2c
3 changed files with 11 additions and 14 deletions

View File

@ -16,13 +16,10 @@ export const getLimits = async ({ headers }: GetLimitsOptions = {}) => {
headers: {
...requestHeaders,
},
next: {
revalidate: 60,
},
})
.then(async (res) => res.json())
.then((res) => ZLimitsResponseSchema.parse(res))
.catch(() => {
.catch((_err) => {
return {
quota: FREE_PLAN_LIMITS,
remaining: FREE_PLAN_LIMITS,

View File

@ -3,7 +3,6 @@ import { NextApiRequest, NextApiResponse } from 'next';
import { getToken } from 'next-auth/jwt';
import { match } from 'ts-pattern';
import { withStaleWhileRevalidate } from '@documenso/lib/server-only/http/with-swr';
import { getFlag } from '@documenso/lib/universal/get-feature-flag';
import { SELFHOSTED_PLAN_LIMITS } from './constants';
@ -21,7 +20,7 @@ export const limitsHandler = async (
const isBillingEnabled = await getFlag('app_billing');
if (!isBillingEnabled) {
return withStaleWhileRevalidate<typeof res>(res).status(200).json({
return res.status(200).json({
quota: SELFHOSTED_PLAN_LIMITS,
remaining: SELFHOSTED_PLAN_LIMITS,
});
@ -33,7 +32,7 @@ export const limitsHandler = async (
const limits = await getServerLimits({ email: token.email });
return withStaleWhileRevalidate<typeof res>(res).status(200).json(limits);
return res.status(200).json(limits);
} catch (err) {
console.error('error', err);

View File

@ -1,6 +1,6 @@
'use client';
import { createContext, useContext, useEffect, useRef, useState } from 'react';
import { createContext, useContext, useEffect, useState } from 'react';
import { equals } from 'remeda';
@ -33,17 +33,18 @@ export const LimitsProvider = ({ initialValue, children }: LimitsProviderProps)
remaining: FREE_PLAN_LIMITS,
};
const $limits = useRef(initialValue ?? defaultValue);
const [limits, setLimits] = useState(() => $limits.current);
const [limits, setLimits] = useState(() => initialValue ?? defaultValue);
const refreshLimits = async () => {
const newLimits = await getLimits();
if (equals(newLimits, $limits.current)) {
return;
setLimits((oldLimits) => {
if (equals(oldLimits, newLimits)) {
return oldLimits;
}
setLimits(newLimits);
return newLimits;
});
};
useEffect(() => {