Sveltekit allows to implement multilayered data loading, which is great!
Now suppose you have some complex logic inside your load-functions, which you would like to test. While you might use unit tests, or you might use something like playwright, sometimes you just want to get that $page.data prop to test it
In my case we have a very complex inverted data loading scheme (in our organisation we have a convention to use NodeJs ONLY for rendering - so no backend calls from nodejs), where GO "frontend" calls svelte-kit service twice - first to get the "data-loading schema" (a bunch of graphql-queries), then loading this data, and second - POST request to svelte-kit again with that very data to render the page
Making svelte-kit to render page on POST-request and feeding it data on load-functions is not a problem, we use our adapter and async_hooks for it, but getting "data-loading schema" from load-functions turned out to be quite difficult. New hook solves this problem
Describe the proposed solution
I propose a new handlePageData, which should be called inside render_page function just before rendering
import { type HandlePageData } from '@sveltejs/kit'
export const handlePageData: HandlePageData = function({event, pageData}) {
if (event.request.headers.get('x-e2e-testing')) {
return new Response(JSON.stringify(pageData), {
headers: {
'x-e2e-testing': 'PAGE_DATA'
}
});
}
}
Alternatives considered
I considered using /__data.json route to get page data, but it only calls +page.server.ts load functions, which does not fully represents page data on render
Regarding the hook signature - first I tried to implement something like the handle hook, ((pageData: unknown, resolve: (pageData: unknown) => Response) => Response), but isolating just the pageData for this resolve function turned out to be too big of a deal
Describe the problem
Sveltekit allows to implement multilayered data loading, which is great!
Now suppose you have some complex logic inside your load-functions, which you would like to test. While you might use unit tests, or you might use something like playwright, sometimes you just want to get that
$page.data
prop to test itIn my case we have a very complex inverted data loading scheme (in our organisation we have a convention to use NodeJs ONLY for rendering - so no backend calls from nodejs), where GO "frontend" calls svelte-kit service twice - first to get the "data-loading schema" (a bunch of graphql-queries), then loading this data, and second - POST request to svelte-kit again with that very data to render the page
Making svelte-kit to render page on POST-request and feeding it data on load-functions is not a problem, we use our adapter and async_hooks for it, but getting "data-loading schema" from load-functions turned out to be quite difficult. New hook solves this problem
Describe the proposed solution
I propose a new
handlePageData
, which should be called inside render_page function just before renderingPatch might look just something like this
Then hook might look like this
Alternatives considered
I considered using
/__data.json
route to get page data, but it only calls+page.server.ts
load functions, which does not fully represents page data on renderRegarding the hook signature - first I tried to implement something like the
handle
hook, ((pageData: unknown, resolve: (pageData: unknown) => Response) => Response
), but isolating just thepageData
for this resolve function turned out to be too big of a dealImportance
would make my life easier
Additional Information
No response