Problem
How to implement pagination for components in a page without using query parameters?
Context
Just started learning Sveltekit (new to SSR in general) - decided to try and build an "admin dashboard". Dashboard page has two tables - one that displays posts and the other displays users.
What I can't figure out is how can I implement pagination (and search/sort)?
I was hoping to instead access the pagination value from +page.svelte
in the load()
function in +page.server.ts
somehow but not sure what the best approach is....
// +page.server.ts
export async function load({ fetch }) {
// how to pass limit and skip here from the client so that it can be used in the URL
// e.g. https://dummjson.com/posts?skip={skip}&limit={limit}
const postsResponse = await fetch('https://dummyjson.com/posts').then((result) => result.json());
const usersResponse = await fetch('https://dummyjson.com/posts').then((result) => result.json());
return {
posts: postsResponse.posts,
totalPosts: postsResponse.total,
users: usersResponse.users,
totalUsers: usersResponse.total
};
}
<!-- +page.svelte -->
<script>
import PostsTable from './components/PostsTable.svelte'
import UsersTable from './components/UsersTable.svelte'
export let data;
const handlePostsPageChanged = (page: number) => {
// what to do here?
// I want the SSR to call the load() function with the page number passed
}
const handleUsersPageChanged = (page: number) => {
// what to do here?
// I want the SSR to call the load() function with the page number passed
}
</script>
<h1>Welcome!</h1>
<PostsTable posts={data.posts} total={data.totalPosts} onPageChanged={handlePostsPageChanged} />
<UsersTable users={data.users} total={data.totalUsers} onPageChanged={handleUsersPageChanged} />
Am I even thinking about this correctly?
What I tried
I tried using cookies with form actions and that worked but feels like a rather hacky solution.
query params are not a preferable solution as then the url would look something like dashboard/skip_posts=30&limit_posts=30&skip_users=10&limit_users=30
which doesn't make sense because why does the dashboard have pagination? its the tables being paginated not the dashboard page.
Also, query-params would pollute the browser history (user tries to click back and they stay on the dashboard but the tables page changes which is unexpected UX)
Finally, instead of making the server do the fetch, I just fetch the data on the client but doesn't that defeats the purpose of SSR?