Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix: preserve loaded pagination on refresh

Blue Mouse73639ff6a29f
diff --git a/web/app/pages/Project/tabs/Errors/ErrorsView.tsx b/web/app/pages/Project/tabs/Errors/ErrorsView.tsx
index 897f7a397..62453c186 100644
--- a/web/app/pages/Project/tabs/Errors/ErrorsView.tsx
+++ b/web/app/pages/Project/tabs/Errors/ErrorsView.tsx
@@ -578,6 +578,12 @@ const ErrorsViewInner = ({ deferredData }: ErrorsViewInnerProps) => {
// Initialize state from deferred data
const initialDataProcessed = useRef(false)
+ const errorsProxyRequestRef = useRef<{
+ mode: 'append' | 'replace'
+ limit?: number
+ } | null>(null)
+ const preserveErrorsRevalidationRef = useRef(false)
+ const preserveErrorsRevalidationStartedRef = useRef(false)
const [errorOptions, setErrorOptions] = useState<Record<string, boolean>>({
[ERROR_FILTERS_MAPPING.showResolved]: false,
@@ -674,6 +680,15 @@ const ErrorsViewInner = ({ deferredData }: ErrorsViewInnerProps) => {
useEffect(() => {
if (!initialDataProcessed.current) return
if (revalidator.state === 'idle') {
+ if (preserveErrorsRevalidationRef.current) {
+ if (preserveErrorsRevalidationStartedRef.current) {
+ preserveErrorsRevalidationRef.current = false
+ preserveErrorsRevalidationStartedRef.current = false
+ setErrorsLoading(false)
+ }
+ return
+ }
+
if (deferredData.errorsData) {
const errorsList = deferredData.errorsData.errors || []
setErrors(errorsList)
@@ -685,14 +700,18 @@ const ErrorsViewInner = ({ deferredData }: ErrorsViewInnerProps) => {
}
setErrorsLoading(false)
} else if (revalidator.state === 'loading') {
+ if (preserveErrorsRevalidationRef.current) {
+ preserveErrorsRevalidationStartedRef.current = true
+ }
setErrorsLoading(true)
}
}, [revalidator.state, deferredData])
// Load more errors via proxy
const loadMoreErrors = useCallback(() => {
- if (errorsLoading) return
+ if (errorsLoading || errorsProxy.isLoading) return
+ errorsProxyRequestRef.current = { mode: 'append' }
errorsProxy.fetchErrors(id, {
timeBucket,
period: period === 'custom' ? '' : period,
@@ -723,13 +742,29 @@ const ErrorsViewInner = ({ deferredData }: ErrorsViewInnerProps) => {
if (revalidator.state === 'loading') return
if (errorsProxy.data && !errorsProxy.isLoading) {
+ const proxyRequest = errorsProxyRequestRef.current
+ if (!proxyRequest) {
+ return
+ }
+
const newErrors = errorsProxy.data.errors || []
- setErrors((prev) => [...prev, ...newErrors])
- setErrorsSkip((prev) => prev + ERRORS_TAKE)
- setCanLoadMoreErrors(newErrors.length >= ERRORS_TAKE)
+ if (proxyRequest.mode === 'replace') {
+ const visibleErrors = newErrors.slice(0, proxyRequest.limit)
+ setErrors(visibleErrors)
+ setErrorsSkip(visibleErrors.length)
+ setCanLoadMoreErrors(newErrors.length > (proxyRequest.limit || 0))
+ } else {
+ setErrors((prev) => [...prev, ...newErrors])
+ setErrorsSkip((prev) => prev + newErrors.length)
+ setCanLoadMoreErrors(newErrors.length >= ERRORS_TAKE)
+ }
+ setErrorsLoading(false)
+ errorsProxyRequestRef.current = null
}
if (errorsProxy.error) {
toast.error(errorsProxy.error)
+ setErrorsLoading(false)
+ errorsProxyRequestRef.current = null
}
}, [
errorsProxy.data,
@@ -854,9 +889,30 @@ const ErrorsViewInner = ({ deferredData }: ErrorsViewInnerProps) => {
],
)
- // Handle refresh trigger - use revalidator for URL-based data
useEffect(() => {
if (errorsRefreshTrigger > 0) {
+ if (!activeEID) {
+ if (errorsProxy.isLoading) {
+ return
+ }
+
+ const limit = Math.max(errors.length, ERRORS_TAKE)
+ errorsProxyRequestRef.current = { mode: 'replace', limit }
+ preserveErrorsRevalidationRef.current = true
+ setErrorsLoading(true)
+ errorsProxy.fetchErrors(id, {
+ timeBucket,
+ period: period === 'custom' ? '' : period,
+ from: from || undefined,
+ to: to || undefined,
+ timezone: timezone || '',
+ filters,
+ take: limit + 1,
+ skip: 0,
+ options: errorOptions,
+ })
+ }
+
revalidator.revalidate()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
diff --git a/web/app/pages/Project/tabs/FeatureFlags/FeatureFlagsView.tsx b/web/app/pages/Project/tabs/FeatureFlags/FeatureFlagsView.tsx
index 87ab8f0e2..ceda9eb86 100644
--- a/web/app/pages/Project/tabs/FeatureFlags/FeatureFlagsView.tsx
+++ b/web/app/pages/Project/tabs/FeatureFlags/FeatureFlagsView.tsx
@@ -784,8 +784,14 @@ const FeatureFlagsViewInner = ({
)
const loadFlagProfiles = useCallback(
- async (flagId: string, append = false, resultFilter?: boolean) => {
- const currentProfiles = append ? flagProfiles[flagId] || [] : []
+ async (
+ flagId: string,
+ append = false,
+ resultFilter?: boolean,
+ options: { take?: number; limit?: number } = {},
+ ) => {
+ const existingProfiles = flagProfiles[flagId] || []
+ const currentProfiles = append ? existingProfiles : []
const skip = append ? currentProfiles.length : 0
const filter = resultFilter ?? flagResultFilters[flagId] ?? true
const apiFilter = filter ? 'true' : 'false'
@@ -797,16 +803,21 @@ const FeatureFlagsViewInner = ({
from,
to,
timezone,
- take: DEFAULT_FEATURE_FLAG_PROFILES_TAKE,
+ take: options.take || DEFAULT_FEATURE_FLAG_PROFILES_TAKE,
skip,
resultFilter: apiFilter,
})
if (isMountedRef.current && result) {
+ const visibleProfiles =
+ typeof options.limit === 'number'
+ ? result.profiles.slice(0, options.limit)
+ : result.profiles
+
setFlagProfiles((prev) => ({
...prev,
[flagId]: append
- ? [...currentProfiles, ...result.profiles]
- : result.profiles,
+ ? [...currentProfiles, ...visibleProfiles]
+ : visibleProfiles,
}))
setFlagProfilesTotal((prev) => ({ ...prev, [flagId]: result.total }))
}
@@ -895,7 +906,14 @@ const FeatureFlagsViewInner = ({
setFlagStats({})
if (expandedFlagId) {
setFlagProfiles((prev) => ({ [expandedFlagId]: prev[expandedFlagId] }))
- loadFlagProfiles(expandedFlagId)
+ const limit = Math.max(
+ flagProfiles[expandedFlagId]?.length || 0,
+ DEFAULT_FEATURE_FLAG_PROFILES_TAKE,
+ )
+ loadFlagProfiles(expandedFlagId, false, undefined, {
+ take: limit,
+ limit,
+ })
} else {
setFlagProfiles({})
setFlagProfilesTotal({})
diff --git a/web/app/pages/Project/tabs/Profiles/ProfilesView.tsx b/web/app/pages/Project/tabs/Profiles/ProfilesView.tsx
index 5fb203404..90e773a4c 100644
--- a/web/app/pages/Project/tabs/Profiles/ProfilesView.tsx
+++ b/web/app/pages/Project/tabs/Profiles/ProfilesView.tsx
@@ -107,7 +107,11 @@ const ProfilesView = ({ tnMapping }: ProfilesViewProps) => {
}
}, [])
- const loadProfiles = async (forcedSkip?: number, override?: boolean) => {
+ const loadProfiles = async (
+ forcedSkip?: number,
+ override?: boolean,
+ options: { take?: number; limit?: number } = {},
+ ) => {
if (profilesLoading) {
return
}
@@ -130,7 +134,7 @@ const ProfilesView = ({ tnMapping }: ProfilesViewProps) => {
filters,
from: period === 'custom' && dateRange ? from : '',
to: period === 'custom' && dateRange ? to : '',
- take: SESSIONS_TAKE,
+ take: options.take || SESSIONS_TAKE,
skip,
timezone,
profileType: profileTypeFilter,
@@ -140,25 +144,24 @@ const ProfilesView = ({ tnMapping }: ProfilesViewProps) => {
const profilesList = (dataProfiles?.profiles || []).map(
mapApiProfileToUI,
)
+ const visibleProfiles =
+ typeof options.limit === 'number'
+ ? profilesList.slice(0, options.limit)
+ : profilesList
+
if (override) {
- setProfiles(profilesList)
+ setProfiles(visibleProfiles)
} else {
- setProfiles((prev) => [...prev, ...profilesList])
+ setProfiles((prev) => [...prev, ...visibleProfiles])
}
- setProfilesSkip((prev) => {
- if (typeof forcedSkip === 'number') {
- return SESSIONS_TAKE + forcedSkip
- }
- return SESSIONS_TAKE + prev
- })
+ setProfilesSkip(skip + visibleProfiles.length)
- if (
- !dataProfiles?.profiles ||
- dataProfiles.profiles.length < SESSIONS_TAKE
- ) {
- setCanLoadMoreProfiles(false)
+ if (typeof options.limit === 'number') {
+ setCanLoadMoreProfiles(profilesList.length > options.limit)
} else {
- setCanLoadMoreProfiles(true)
+ setCanLoadMoreProfiles(
+ profilesList.length >= (options.take || SESSIONS_TAKE),
+ )
}
}
} catch (reason) {
@@ -173,7 +176,10 @@ const ProfilesView = ({ tnMapping }: ProfilesViewProps) => {
}
}
- const loadProfile = async (profileId: string) => {
+ const loadProfile = async (
+ profileId: string,
+ options: { sessionsTake?: number; sessionsLimit?: number } = {},
+ ) => {
const requestId = ++profileRequestIdRef.current
const hasExistingProfileData =
!!activeProfile && activeProfile.profileId === profileId
@@ -209,8 +215,10 @@ const ProfilesView = ({ tnMapping }: ProfilesViewProps) => {
data
) {
setActiveProfile(data as unknown as ProfileDetailsType)
- // Load initial sessions for the profile (override existing on refresh)
- loadProfileSessionsData(profileId, 0, true)
+ loadProfileSessionsData(profileId, 0, true, {
+ take: options.sessionsTake,
+ limit: options.sessionsLimit,
+ })
}
} catch (reason) {
console.error('[ERROR](loadProfile) Loading profile data failed:', reason)
@@ -228,6 +236,7 @@ const ProfilesView = ({ tnMapping }: ProfilesViewProps) => {
profileId: string,
forcedSkip?: number,
override?: boolean,
+ options: { take?: number; limit?: number } = {},
) => {
if (profileSessionsLoading) {
return
@@ -255,7 +264,7 @@ const ProfilesView = ({ tnMapping }: ProfilesViewProps) => {
filters,
from,
to,
- take: SESSIONS_TAKE,
+ take: options.take || SESSIONS_TAKE,
skip,
timezone,
},
@@ -267,25 +276,24 @@ const ProfilesView = ({ tnMapping }: ProfilesViewProps) => {
) {
const sessionsList = (dataSessions?.sessions ||
[]) as unknown as Session[]
+ const visibleSessions =
+ typeof options.limit === 'number'
+ ? sessionsList.slice(0, options.limit)
+ : sessionsList
+
if (override) {
- setProfileSessions(sessionsList)
+ setProfileSessions(visibleSessions)
} else {
- setProfileSessions((prev) => [...prev, ...sessionsList])
+ setProfileSessions((prev) => [...prev, ...visibleSessions])
}
- setProfileSessionsSkip((prev) => {
- if (typeof forcedSkip === 'number') {
- return SESSIONS_TAKE + forcedSkip
- }
- return SESSIONS_TAKE + prev
- })
+ setProfileSessionsSkip(skip + visibleSessions.length)
- if (
- !dataSessions?.sessions ||
- dataSessions.sessions.length < SESSIONS_TAKE
- ) {
- setCanLoadMoreProfileSessions(false)
+ if (typeof options.limit === 'number') {
+ setCanLoadMoreProfileSessions(sessionsList.length > options.limit)
} else {
- setCanLoadMoreProfileSessions(true)
+ setCanLoadMoreProfileSessions(
+ sessionsList.length >= (options.take || SESSIONS_TAKE),
+ )
}
}
} catch (reason) {
@@ -367,10 +375,14 @@ const ProfilesView = ({ tnMapping }: ProfilesViewProps) => {
useEffect(() => {
if (profilesRefreshTrigger > 0) {
if (activeProfileId) {
- loadProfile(activeProfileId)
+ const limit = Math.max(profileSessions.length, SESSIONS_TAKE)
+ loadProfile(activeProfileId, {
+ sessionsTake: limit + 1,
+ sessionsLimit: limit,
+ })
} else {
- setProfilesSkip(0)
- loadProfiles(0, true)
+ const limit = Math.max(profiles.length, SESSIONS_TAKE)
+ loadProfiles(0, true, { take: limit + 1, limit })
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
diff --git a/web/app/pages/Project/tabs/Sessions/SessionsView.tsx b/web/app/pages/Project/tabs/Sessions/SessionsView.tsx
index e75c3eff3..fee1ce40d 100644
--- a/web/app/pages/Project/tabs/Sessions/SessionsView.tsx
+++ b/web/app/pages/Project/tabs/Sessions/SessionsView.tsx
@@ -227,7 +227,11 @@ const SessionsViewInner = ({
// Track when loader data was last applied to detect stale proxy responses
const loaderUpdateCounterRef = useRef(0)
- const proxyRequestCounterRef = useRef(0)
+ const proxyRequestRef = useRef<{
+ mode: 'append' | 'replace'
+ loaderUpdateCounter: number
+ limit?: number
+ } | null>(null)
// Track if we've shown content in the current data set to prevent NoSessions flash during exit animation
const hasShownContentRef = useRef(false)
@@ -265,6 +269,7 @@ const SessionsViewInner = ({
setCanLoadMoreSessions(sessionsList.length >= SESSIONS_TAKE)
// Increment counter to invalidate any in-flight proxy responses
loaderUpdateCounterRef.current += 1
+ proxyRequestRef.current = null
// Reset content tracking when loader provides empty results (e.g., after filter change)
if (_isEmpty(sessionsList)) {
hasShownContentRef.current = false
@@ -279,25 +284,47 @@ const SessionsViewInner = ({
if (revalidator.state === 'loading') return
if (sessionsProxy.data && !sessionsProxy.isLoading) {
+ const proxyRequest = proxyRequestRef.current
+ if (!proxyRequest) {
+ return
+ }
+
// Ignore stale proxy responses that were initiated before the last loader update
- if (proxyRequestCounterRef.current < loaderUpdateCounterRef.current) {
+ if (
+ proxyRequest.loaderUpdateCounter < loaderUpdateCounterRef.current
+ ) {
return
}
const newSessions = sessionsProxy.data.sessions || []
- setSessions((prev) => {
- // Deduplicate: only add sessions whose psid is not already present
- const existingIds = new Set(prev.map((s) => s.psid))
- const uniqueNewSessions = newSessions.filter(
- (s) => !existingIds.has(s.psid),
+ setError(null)
+
+ if (proxyRequest.mode === 'replace') {
+ const visibleSessions = newSessions.slice(0, proxyRequest.limit)
+ setSessions(visibleSessions)
+ setSessionsSkip(visibleSessions.length)
+ setCanLoadMoreSessions(
+ newSessions.length > (proxyRequest.limit || 0),
)
- return [...prev, ...uniqueNewSessions]
- })
- setSessionsSkip((prev) => prev + SESSIONS_TAKE)
- setCanLoadMoreSessions(newSessions.length >= SESSIONS_TAKE)
+ hasShownContentRef.current = !_isEmpty(visibleSessions)
+ } else {
+ setSessions((prev) => {
+ // Deduplicate: only add sessions whose psid is not already present
+ const existingIds = new Set(prev.map((s) => s.psid))
+ const uniqueNewSessions = newSessions.filter(
+ (s) => !existingIds.has(s.psid),
+ )
+ return [...prev, ...uniqueNewSessions]
+ })
+ setSessionsSkip((prev) => prev + newSessions.length)
+ setCanLoadMoreSessions(newSessions.length >= SESSIONS_TAKE)
+ }
+
+ proxyRequestRef.current = null
}
if (sessionsProxy.error) {
setError(sessionsProxy.error)
+ proxyRequestRef.current = null
}
}, [
sessionsProxy.data,
@@ -306,10 +333,7 @@ const SessionsViewInner = ({
revalidator.state,
])
- // Load more sessions via proxy
- const loadMoreSessions = () => {
- if (sessionsLoading) return
-
+ const getSessionsParams = (take: number, skip: number) => {
let from: string | undefined
let to: string | undefined
@@ -318,24 +342,47 @@ const SessionsViewInner = ({
to = getFormatDate(dateRange[1])
}
- // Track when this proxy request was initiated relative to loader updates
- proxyRequestCounterRef.current = loaderUpdateCounterRef.current
-
- sessionsProxy.fetchSessions(id, {
+ return {
timeBucket: 'day',
period: period === 'custom' ? '' : period,
from,
to,
timezone,
filters,
- take: SESSIONS_TAKE,
- skip: sessionsSkip,
- })
+ take,
+ skip,
+ }
+ }
+
+ // Load more sessions via proxy
+ const loadMoreSessions = () => {
+ if (sessionsLoading) return
+
+ // Track when this proxy request was initiated relative to loader updates
+ proxyRequestRef.current = {
+ mode: 'append',
+ loaderUpdateCounter: loaderUpdateCounterRef.current,
+ }
+
+ sessionsProxy.fetchSessions(
+ id,
+ getSessionsParams(SESSIONS_TAKE, sessionsSkip),
+ )
}
- // Handle refresh trigger - use revalidator for URL-based data
useEffect(() => {
if (sessionsRefreshTrigger > 0) {
+ if (!activePSID && !sessionsLoading) {
+ const limit = Math.max(sessions.length, SESSIONS_TAKE)
+ proxyRequestRef.current = {
+ mode: 'replace',
+ loaderUpdateCounter: loaderUpdateCounterRef.current,
+ limit,
+ }
+ sessionsProxy.fetchSessions(id, getSessionsParams(limit + 1, 0))
+ return
+ }
+
revalidator.revalidate()
}
// eslint-disable-next-line react-hooks/exhaustive-deps