-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: call createAppRouter
within App
component; implement shouldRevalidate
for rootLoader
sub-route navigation
#1256
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a9e78fd
fix: call createAppRouter within App render
adamstankiewicz 208fb17
chore: tests
adamstankiewicz 3114e4d
chore: updates to use shouldRevalidate
adamstankiewicz 01d60f4
fix: disable suspense for useNotices
adamstankiewicz 0bec2a8
docs: add ADR about shouldRevalidate on rootLoader
adamstankiewicz 59cb844
fix: fallback to defaultShouldRevalidate
adamstankiewicz b84f52d
fix: updates
adamstankiewicz 6c567b5
chore: test coverage
adamstankiewicz e45969f
chore: test coverage
adamstankiewicz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# 0014. Triggering Revalidation on the `rootLoader` Route Loader on Sub-Route Navigation | ||
|
||
## Status | ||
|
||
Accepted (January 2025) | ||
|
||
## Context | ||
|
||
In this route-based application, the `rootLoader` is responsible for pre-fetching common queries needed across multiple routes. However, when navigating between sub-routes (e.g., from the Dashboard to the Search page), the `rootLoader` doesn’t re-execute. This becomes a problem when a component in the target route accesses query data with `useQuery` that hasn’t already been pre-fetched by a loader. | ||
|
||
By default, this issue is masked due to the query client configuration where queries have `suspense: true`. In these cases, if a query hasn’t been pre-fetched in a route loader, the `useQuery` hook causes the component to suspend, triggering the `AppSuspenseFallback` component while the query resolves asynchronously. This fallback behavior hides the underlying problem, making it harder to identify when query data is being accessed without pre-fetching. | ||
|
||
## Decision | ||
|
||
To ensure data consistency and explicitly address the issue of missing pre-fetched queries, two key changes will be implemented: | ||
|
||
### 1. Remove the Parent `Suspense` and `AppSuspenseFallback` Component | ||
|
||
The parent `Suspense` boundary and its fallback (`AppSuspenseFallback`) will be removed from the `App` component. By default, the query client configuration uses `suspense: true`, which masks the issue of missing pre-fetched query data by suspending components and displaying the fallback while queries resolve asynchronously. Removing this fallback ensures that missing query data triggers explicit errors instead of silently deferring to the fallback. This change improves the navigation experience and makes it easier to debug and fix data-loading issues during development. | ||
|
||
### 2. Implement `shouldRevalidate` on the `rootLoader` | ||
|
||
React Router by default only revalidates loaders after specific actions, such as form submissions or mutations. It does not automatically revalidate loaders during navigation between sibling routes under the same parent. To address this, the `shouldRevalidate` function will be implemented on the `rootLoader` route, with logic explicitly tailored to handle sub-route navigation. | ||
|
||
The `shouldRevalidate` function will determine whether the loader should re-execute by checking the following: | ||
|
||
* If the pathname has not changed between the current and next URLs, no revalidation will occur. | ||
* If the pathname indicates navigation to a sub-route within the parent route (e.g., an enterprise-specific route or its sub-routes), the `rootLoader` will revalidate to ensure all required query data is pre-fetched and available. | ||
|
||
This logic ensures that revalidation is scoped specifically to relevant sub-route transitions, avoiding unnecessary data fetching while ensuring consistent query cache availability. | ||
|
||
### Benefits of this Approach | ||
|
||
* Queries required by UI components in the target route are always pre-fetched and cached before rendering. | ||
* Developers can easily identify and address missing pre-fetches through explicit errors instead of relying on silent fallback behavior. | ||
* Data consistency is maintained across sibling route transitions, particularly during the incremental migration to a Backend-for-Frontend (BFF) or API Gateway architecture. | ||
|
||
## Consequences | ||
|
||
### Positive Outcomes | ||
|
||
* **Reliable Query Data Availability:** Ensures that all required query data is pre-fetched and cached during sub-route transitions, preventing issues where `useQuery` tries to access missing or stale data. | ||
* **Explicit Error Handling:** Removing the `AppSuspenseFallback` ensures that missing pre-fetched data triggers explicit errors instead of being masked by the fallback. This simplifies debugging and leads to better long-term reliability. | ||
* **Scoped and Efficient Revalidation:** The `shouldRevalidate` function selectively targets relevant sub-route transitions. Combined with `ensureQueryData`, it prevents redundant API requests for data that remains fresh, minimizing performance impact. | ||
* **Improved Developer Workflow:** By exposing missing pre-fetches, the approach facilitates early identification and resolution of query-related issues, reducing risks of data inconsistencies. | ||
|
||
### Negative Outcomes | ||
|
||
* **Initial Debugging Effort:** Removing the fallback may reveal existing cases where query data was not pre-fetched. This change could introduce additional debugging during development as missing pre-fetches surface as explicit errors. However, addressing these issues early ensures long-term consistency in data-loading strategies by enforcing a robust pattern of pre-fetching query data in route loaders. | ||
* **Small Overhead for Non-Fresh Data:** Revalidating the `rootLoader` during sub-route navigation may result in occasional additional backend calls if data is not fresh. However, these calls are scoped and optimized, ensuring minimal impact on performance. | ||
|
||
## Alternatives Considered | ||
|
||
* Keeping the parent `Suspense` and its fallback component (`AppSuspenseFallback`) in the `App` component would allow the application to handle missing pre-fetches silently, suspending components and showing a secondary loading state during navigation. While this approach mitigates user-facing errors, it obscures data-loading inconsistencies and makes debugging more difficult. Additionally, it risks subtle issues in route navigation where query data dependencies are unclear. By adopting the chosen approach, explicit error handling ensures better enforcement of consistent and predictable data-loading practices. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { createMemoryRouter } from 'react-router-dom'; | ||
import { act } from 'react-dom/test-utils'; | ||
import App from './App'; | ||
import { createAppRouter } from './routes'; | ||
|
||
jest.mock('./routes', () => ({ | ||
...jest.requireActual('./routes'), | ||
createAppRouter: jest.fn(), | ||
})); | ||
|
||
jest.mock('@tanstack/react-query-devtools', () => ({ | ||
ReactQueryDevtools: () => <div data-testid="react-query-devtools">ReactQueryDevtools</div>, | ||
})); | ||
|
||
jest.mock('@tanstack/react-query-devtools/production', () => ({ | ||
ReactQueryDevtools: () => <div data-testid="react-query-devtools-production">ReactQueryDevtoolsProduction</div>, | ||
})); | ||
|
||
jest.mock('@edx/frontend-platform/react', () => ({ | ||
AppProvider: ({ children }) => <div data-testid="app-provider">{children}</div>, | ||
})); | ||
|
||
describe('App', () => { | ||
let queryClient; | ||
|
||
beforeEach(() => { | ||
queryClient = new QueryClient(); | ||
const mockRouter = createMemoryRouter( | ||
[{ path: '/', element: <div data-testid="mock-route">Mock Route</div> }], | ||
{ initialEntries: ['/'] }, | ||
); | ||
createAppRouter.mockReturnValue(mockRouter); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('renders App component without errors', () => { | ||
render( | ||
<QueryClientProvider client={queryClient}> | ||
<App /> | ||
</QueryClientProvider>, | ||
); | ||
|
||
expect(screen.getByTestId('app-provider')).toBeInTheDocument(); | ||
}); | ||
|
||
test('toggles ReactQueryDevtoolsProduction visibility', async () => { | ||
render(<App />); | ||
|
||
expect(screen.getByTestId('react-query-devtools')).toBeInTheDocument(); | ||
|
||
// Toggle visibility ON | ||
act(() => { | ||
window.toggleReactQueryDevtools(); | ||
}); | ||
await waitFor(() => { | ||
expect(screen.getByTestId('react-query-devtools-production')).toBeInTheDocument(); | ||
}); | ||
|
||
// Toggle visibility OFF | ||
act(() => { | ||
window.toggleReactQueryDevtools(); | ||
}); | ||
await waitFor(() => { | ||
expect(screen.queryByTestId('react-query-devtools-production')).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
test('uses the custom router created by createAppRouter', () => { | ||
render(<App />); | ||
|
||
expect(createAppRouter).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
defaultOptions: { | ||
queries: expect.objectContaining({ | ||
cacheTime: 1000 * 60 * 30, | ||
keepPreviousData: true, | ||
retry: expect.any(Function), | ||
staleTime: 1000 * 20, | ||
suspense: true, | ||
}), | ||
}, | ||
}), | ||
); | ||
|
||
expect(screen.getByTestId('mock-route')).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🥳