perf(tmdb): trim responses to fields the UI uses, in proxy and server client
Move the trimmer from the route into a shared lib/tmdbTrim and apply it in the server-side client too (server hits TMDB directly, bypassing the proxy), so both paths return identical shapes. Show page HTML drops ~22% (88.4KB -> 68.8KB); TV details payload -90%.
This commit is contained in:
1 parent
88f51f783e
commit
8c8d752962
3 files changed
+95
-6
No files matched your search
@@ -1,9 +1,24 @@
|
||||
import { isServer } from '@tanstack/react-query';
|
||||
|
||||
import { MOVIE_DB_API_URL, MOVIE_DB_TOKEN } from '@/env';
|
||||
import trimTmdb from '@/lib/tmdbTrim';
|
||||
|
||||
import Http from './Http';
|
||||
|
||||
const movieAdvisor = isServer ? new Http(MOVIE_DB_API_URL, `Bearer ${MOVIE_DB_TOKEN}`) : new Http('/api/tmdb');
|
||||
type Search = Record<string, string | number | boolean | null | undefined>;
|
||||
|
||||
const http = isServer ? new Http(MOVIE_DB_API_URL, `Bearer ${MOVIE_DB_TOKEN}`) : new Http('/api/tmdb');
|
||||
|
||||
// The browser goes through the /api/tmdb proxy, which trims responses; on the server we hit the
|
||||
// API directly, so apply the same trim to keep both paths returning identical data.
|
||||
const movieAdvisor = {
|
||||
get: <T>(url: string, { params, preventCache }: { params?: Search; preventCache?: boolean } = {}) =>
|
||||
http.get<T>(url, { params, preventCache }).then((data) => trimTmdb(url, data) as T),
|
||||
post: <T, B = unknown>(
|
||||
url: string,
|
||||
{ params, preventCache }: { params?: Search; preventCache?: boolean } = {},
|
||||
body?: B
|
||||
) => http.post<T, B>(url, { params, preventCache }, body)
|
||||
};
|
||||
|
||||
export default movieAdvisor;
|
||||
Reference in new issue
Block a user