mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
fix: limits no longer cache during session changes
This commit is contained in:
@ -16,13 +16,10 @@ export const getLimits = async ({ headers }: GetLimitsOptions = {}) => {
|
|||||||
headers: {
|
headers: {
|
||||||
...requestHeaders,
|
...requestHeaders,
|
||||||
},
|
},
|
||||||
next: {
|
|
||||||
revalidate: 60,
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
.then(async (res) => res.json())
|
.then(async (res) => res.json())
|
||||||
.then((res) => ZLimitsResponseSchema.parse(res))
|
.then((res) => ZLimitsResponseSchema.parse(res))
|
||||||
.catch(() => {
|
.catch((_err) => {
|
||||||
return {
|
return {
|
||||||
quota: FREE_PLAN_LIMITS,
|
quota: FREE_PLAN_LIMITS,
|
||||||
remaining: FREE_PLAN_LIMITS,
|
remaining: FREE_PLAN_LIMITS,
|
||||||
|
|||||||
@ -3,7 +3,6 @@ import { NextApiRequest, NextApiResponse } from 'next';
|
|||||||
import { getToken } from 'next-auth/jwt';
|
import { getToken } from 'next-auth/jwt';
|
||||||
import { match } from 'ts-pattern';
|
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 { getFlag } from '@documenso/lib/universal/get-feature-flag';
|
||||||
|
|
||||||
import { SELFHOSTED_PLAN_LIMITS } from './constants';
|
import { SELFHOSTED_PLAN_LIMITS } from './constants';
|
||||||
@ -21,7 +20,7 @@ export const limitsHandler = async (
|
|||||||
const isBillingEnabled = await getFlag('app_billing');
|
const isBillingEnabled = await getFlag('app_billing');
|
||||||
|
|
||||||
if (!isBillingEnabled) {
|
if (!isBillingEnabled) {
|
||||||
return withStaleWhileRevalidate<typeof res>(res).status(200).json({
|
return res.status(200).json({
|
||||||
quota: SELFHOSTED_PLAN_LIMITS,
|
quota: SELFHOSTED_PLAN_LIMITS,
|
||||||
remaining: SELFHOSTED_PLAN_LIMITS,
|
remaining: SELFHOSTED_PLAN_LIMITS,
|
||||||
});
|
});
|
||||||
@ -33,7 +32,7 @@ export const limitsHandler = async (
|
|||||||
|
|
||||||
const limits = await getServerLimits({ email: token.email });
|
const limits = await getServerLimits({ email: token.email });
|
||||||
|
|
||||||
return withStaleWhileRevalidate<typeof res>(res).status(200).json(limits);
|
return res.status(200).json(limits);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('error', err);
|
console.error('error', err);
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { createContext, useContext, useEffect, useRef, useState } from 'react';
|
import { createContext, useContext, useEffect, useState } from 'react';
|
||||||
|
|
||||||
import { equals } from 'remeda';
|
import { equals } from 'remeda';
|
||||||
|
|
||||||
@ -33,17 +33,18 @@ export const LimitsProvider = ({ initialValue, children }: LimitsProviderProps)
|
|||||||
remaining: FREE_PLAN_LIMITS,
|
remaining: FREE_PLAN_LIMITS,
|
||||||
};
|
};
|
||||||
|
|
||||||
const $limits = useRef(initialValue ?? defaultValue);
|
const [limits, setLimits] = useState(() => initialValue ?? defaultValue);
|
||||||
const [limits, setLimits] = useState(() => $limits.current);
|
|
||||||
|
|
||||||
const refreshLimits = async () => {
|
const refreshLimits = async () => {
|
||||||
const newLimits = await getLimits();
|
const newLimits = await getLimits();
|
||||||
|
|
||||||
if (equals(newLimits, $limits.current)) {
|
setLimits((oldLimits) => {
|
||||||
return;
|
if (equals(oldLimits, newLimits)) {
|
||||||
}
|
return oldLimits;
|
||||||
|
}
|
||||||
|
|
||||||
setLimits(newLimits);
|
return newLimits;
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user