I have a backend route '/products' (using ExpressJS and Prisma) that returns a list of all products, however it also has some query parameters that can be used to specify futher, namely:
I load the products on the frontend SvelteKit project in +page.server.ts and display them in a table format. However, when the user changes, for instance, the sort direction, how would I update the page data using a new route (namely the original one, with sortDir=desc). Is there some way of invalidating the query and replacing it with a new one, with the correct search parameters?
Or is there some other way that this is normally implemented in production?
By using searchParams you can read the values in the load function:
// +page.server.js
export async function load({ fetch, url }) => {
const sortDir = url.searchParams.get("sortDir")
return {
products: await ...,
};
};
Accessing the searchParam will let SvelteKit know to invalidate and rerun/reload the page data when the parameter changes.
To change the searchParam use either regular urls: <a href="?sortDir=asc">ASC</a> or use the goto from the $app/navigation module.
PS. You don't need a separate express backend, you can access prisma directly from +page.server.js files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With