Skip to content

Commit 1e7f8fc

Browse files
committed
feat: get data for single loader
1 parent 3c45a3c commit 1e7f8fc

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

Diff for: packages/qwik-router/src/middleware/request-handler/resolve-request-handlers.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,17 @@ export async function renderQData(requestEv: RequestEvent) {
590590
requestEv.request.headers.forEach((value, key) => (requestHeaders[key] = value));
591591
requestEv.headers.set('Content-Type', 'application/json; charset=utf-8');
592592

593+
const allLoaders = getRequestLoaders(requestEv);
594+
const loaders: Record<string, unknown> = {};
595+
for (const loaderId in allLoaders) {
596+
const loader = allLoaders[loaderId];
597+
if (loader) {
598+
loaders[loaderId] = loader;
599+
}
600+
}
601+
593602
const qData: ClientPageData = {
594-
loaders: getRequestLoaders(requestEv),
603+
loaders,
595604
action: requestEv.sharedMap.get(RequestEvSharedActionId),
596605
status: status !== 200 ? status : 200,
597606
href: getPathname(requestEv.url, trailingSlash),

Diff for: packages/qwik-router/src/runtime/src/server-functions.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,9 @@ export const routeLoaderQrl = ((
210210
}
211211
const data = untrack(() => state[id]);
212212
if (!data && isBrowser) {
213-
// TODO: fetch only loader with current id
214-
throw loadClientData(location.url, iCtx.$hostElement$).then(
215-
(data) => (state[id] = data?.loaders[id])
216-
);
213+
throw loadClientData(location.url, iCtx.$hostElement$, {
214+
loaderIds: [id],
215+
}).then((data) => (state[id] = data?.loaders[id]));
217216
}
218217
return _wrapStore(state, id);
219218
}

Diff for: packages/qwik-router/src/runtime/src/use-endpoint.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ export const loadClientData = async (
99
element: unknown,
1010
opts?: {
1111
action?: RouteActionValue;
12+
loaderIds?: string[];
1213
clearCache?: boolean;
1314
prefetchSymbols?: boolean;
1415
isPrefetch?: boolean;
1516
}
1617
) => {
1718
const pagePathname = url.pathname;
1819
const pageSearch = url.search;
19-
const clientDataPath = getClientDataPath(pagePathname, pageSearch, opts?.action);
20+
const clientDataPath = getClientDataPath(pagePathname, pageSearch, {
21+
actionId: opts?.action?.id,
22+
loaderIds: opts?.loaderIds,
23+
});
2024
let qData: Promise<ClientPageData | undefined> | undefined;
2125
if (!opts?.action) {
2226
qData = CLIENT_DATA_CACHE.get(clientDataPath);

Diff for: packages/qwik-router/src/runtime/src/utils.ts

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { RouteActionValue, SimpleURL } from './types';
1+
import type { SimpleURL } from './types';
22

3-
import { QACTION_KEY } from './constants';
3+
import { QACTION_KEY, QLOADER_KEY } from './constants';
44

55
/** Gets an absolute url path string (url.pathname + url.search + url.hash) */
66
export const toPath = (url: URL) => url.pathname + url.search + url.hash;
@@ -31,13 +31,26 @@ export const isSameOriginDifferentPathname = (a: SimpleURL, b: SimpleURL) =>
3131
export const getClientDataPath = (
3232
pathname: string,
3333
pageSearch?: string,
34-
action?: RouteActionValue
34+
options?: {
35+
actionId?: string;
36+
loaderIds?: string[];
37+
}
3538
) => {
36-
let search = pageSearch ?? '';
37-
if (action) {
38-
search += (search ? '&' : '?') + QACTION_KEY + '=' + encodeURIComponent(action.id);
39+
const search = new URLSearchParams(pageSearch);
40+
if (options?.actionId) {
41+
search.set(QACTION_KEY, options.actionId);
42+
} else if (options?.loaderIds) {
43+
for (const id of options.loaderIds) {
44+
search.append(QLOADER_KEY, id);
45+
}
3946
}
40-
return pathname + (pathname.endsWith('/') ? '' : '/') + 'q-data.json' + search;
47+
const searchString = search.toString();
48+
return (
49+
pathname +
50+
(pathname.endsWith('/') ? '' : '/') +
51+
'q-data.json' +
52+
(searchString.length ? '?' + searchString : '')
53+
);
4154
};
4255

4356
export const getClientNavPath = (props: Record<string, any>, baseUrl: { url: URL }) => {

0 commit comments

Comments
 (0)