Repository navigation
⚙️ refactor: lazy-load React Query Devtools #13639
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
29db445
perf(client): lazy-load query devtools
upman 492a041
fix: keep query devtools deps lazy
danny-avila ee5e6d6
Merge remote-tracking branch 'origin/dev' into ravi/librechat-perf-qu…
danny-avila cc80959
fix: address query devtools review findings
danny-avila ed40410
fix: exclude query devtools from pwa precache
danny-avila 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,33 @@ | ||
| import { lazy, Suspense } from 'react'; | ||
|
|
||
| interface QueryDevtoolsConfig { | ||
| enableQueryDevtools?: boolean; | ||
| } | ||
|
|
||
| interface QueryDevtoolsGateProps { | ||
| config?: QueryDevtoolsConfig; | ||
| isDevelopment?: boolean; | ||
| } | ||
|
|
||
| const LazyReactQueryDevtools = lazy(() => | ||
| import('@tanstack/react-query-devtools/production').then(({ ReactQueryDevtools }) => ({ | ||
| default: ReactQueryDevtools, | ||
| })), | ||
| ); | ||
|
|
||
| export const shouldEnableQueryDevtools = ({ | ||
| isDevelopment = import.meta.env.DEV, | ||
| config = typeof window === 'undefined' ? undefined : window.__LIBRECHAT_CONFIG__, | ||
| }: QueryDevtoolsGateProps = {}) => isDevelopment || config?.enableQueryDevtools === true; | ||
|
|
||
| export default function QueryDevtoolsGate({ isDevelopment, config }: QueryDevtoolsGateProps = {}) { | ||
| if (!shouldEnableQueryDevtools({ isDevelopment, config })) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <Suspense fallback={null}> | ||
| <LazyReactQueryDevtools initialIsOpen={false} position="top-right" /> | ||
| </Suspense> | ||
| ); | ||
| } |
29 changes: 29 additions & 0 deletions
29
client/src/components/__tests__/QueryDevtoolsGate.spec.tsx
This file contains hidden or 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,29 @@ | ||
| import { render, screen, waitFor } from '@testing-library/react'; | ||
| import QueryDevtoolsGate, { shouldEnableQueryDevtools } from '../QueryDevtoolsGate'; | ||
|
|
||
| jest.mock('@tanstack/react-query-devtools/production', () => ({ | ||
| ReactQueryDevtools: () => <div data-testid="query-devtools" />, | ||
| })); | ||
|
|
||
| describe('QueryDevtoolsGate', () => { | ||
| it('keeps query devtools disabled in production by default', () => { | ||
| expect(shouldEnableQueryDevtools({ isDevelopment: false, config: undefined })).toBe(false); | ||
|
|
||
| const { container } = render(<QueryDevtoolsGate isDevelopment={false} config={undefined} />); | ||
|
|
||
| expect(container).toBeEmptyDOMElement(); | ||
| expect(screen.queryByTestId('query-devtools')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('enables query devtools in local development', async () => { | ||
| render(<QueryDevtoolsGate isDevelopment={true} config={undefined} />); | ||
|
|
||
| await waitFor(() => expect(screen.getByTestId('query-devtools')).toBeInTheDocument()); | ||
| }); | ||
|
|
||
| it('enables query devtools in production when the server-injected flag is true', async () => { | ||
| render(<QueryDevtoolsGate isDevelopment={false} config={{ enableQueryDevtools: true }} />); | ||
|
|
||
| await waitFor(() => expect(screen.getByTestId('query-devtools')).toBeInTheDocument()); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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,34 @@ | ||
| import type { QueryDevtoolsRequest } from './devtools'; | ||
| import { | ||
| QUERY_DEVTOOLS_HEADER, | ||
| maybeInjectQueryDevtoolsBootstrap, | ||
| shouldEnableQueryDevtools, | ||
| } from './devtools'; | ||
|
|
||
| const createReq = (value?: string): QueryDevtoolsRequest => ({ | ||
| get: (header) => (header === QUERY_DEVTOOLS_HEADER ? value : undefined), | ||
| }); | ||
|
|
||
| describe('query devtools HTML bootstrap', () => { | ||
| const html = | ||
| '<!DOCTYPE html><html lang="en-US"><head><title>LibreChat</title></head><body></body></html>'; | ||
|
|
||
| it('uses the documented debug header value as the opt-in signal', () => { | ||
| expect(shouldEnableQueryDevtools(createReq('1'))).toBe(true); | ||
| expect(shouldEnableQueryDevtools(createReq('true'))).toBe(false); | ||
| expect(shouldEnableQueryDevtools(createReq())).toBe(false); | ||
| }); | ||
|
|
||
| it('does not inject the production devtools flag by default', () => { | ||
| expect(maybeInjectQueryDevtoolsBootstrap(html, createReq())).toBe(html); | ||
| }); | ||
|
|
||
| it('injects a server-to-client flag when the debug header is present', () => { | ||
| const updatedHtml = maybeInjectQueryDevtoolsBootstrap(html, createReq('1')); | ||
|
|
||
| expect(updatedHtml).toContain('window.__LIBRECHAT_CONFIG__'); | ||
| expect(updatedHtml).toContain('data-librechat-query-devtools="true"'); | ||
| expect(updatedHtml).toContain('"enableQueryDevtools":true'); | ||
| expect(updatedHtml.indexOf('enableQueryDevtools')).toBeLessThan(updatedHtml.indexOf('</head>')); | ||
| }); | ||
| }); |
This file contains hidden or 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,34 @@ | ||
| export const QUERY_DEVTOOLS_HEADER = 'x-librechat-enable-query-devtools'; | ||
|
|
||
| const QUERY_DEVTOOLS_SENTINEL = 'data-librechat-query-devtools="true"'; | ||
| const QUERY_DEVTOOLS_BOOTSTRAP = `<script ${QUERY_DEVTOOLS_SENTINEL}>window.__LIBRECHAT_CONFIG__=Object.assign({},window.__LIBRECHAT_CONFIG__,{"enableQueryDevtools":true});</script>`; | ||
|
|
||
| export interface QueryDevtoolsRequest { | ||
| get(header: string): string | undefined; | ||
| } | ||
|
|
||
| export const shouldEnableQueryDevtools = (req: QueryDevtoolsRequest): boolean => | ||
| req.get(QUERY_DEVTOOLS_HEADER) === '1'; | ||
|
|
||
| const injectQueryDevtoolsBootstrap = (html: string): string => { | ||
| if (html.includes(QUERY_DEVTOOLS_SENTINEL)) { | ||
| return html; | ||
| } | ||
|
|
||
| if (html.includes('</head>')) { | ||
| return html.replace('</head>', `${QUERY_DEVTOOLS_BOOTSTRAP}</head>`); | ||
| } | ||
|
|
||
| return html.replace(/<body([^>]*)>/i, `<body$1>${QUERY_DEVTOOLS_BOOTSTRAP}`); | ||
| }; | ||
|
|
||
| export const maybeInjectQueryDevtoolsBootstrap = ( | ||
| html: string, | ||
| req: QueryDevtoolsRequest, | ||
| ): string => { | ||
| if (!shouldEnableQueryDevtools(req)) { | ||
| return html; | ||
| } | ||
|
|
||
| return injectQueryDevtoolsBootstrap(html); | ||
| }; |
This file contains hidden or 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 @@ | ||
| export * from './devtools'; |
This file contains hidden or 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.
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.
In production builds where the PWA service worker is registered, this manual chunk is emitted as
assets/query-devtools-*.js, but the sameclient/vite.config.tsWorkbox config still precaches every**/*.{js,css,html}file and only ignoresindex.html, maps, RUM, and locale chunks. That means clients still download/cache the React Query Devtools chunk on every service-worker install/update even when the opt-in header is absent, which defeats the default network-byte savings of lazy-loading; add the chunk toglobIgnoresor otherwise keep it out of the precache manifest.Useful? React with 👍 / 👎.
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.
Fixed in
ed40410a5by addingassets/query-devtools*.jsto the WorkboxglobIgnoreslist so the service worker precache skips the lazy React Query Devtools chunk. Validation: Prettier/import-sort/ESLint onclient/vite.config.ts,npm run build:client, andrg "query-devtools" client/dist/sw.jsconfirmed the generated service worker has no precache reference.