Paginated Queries and Downloads
Hi all,
I’ve got an application with a reporting module where the user can choose from some report params and then view a table (built with Tanstack Table) with the first 50 records..
I’m needing to add an “Export all” button that should page through all the data and write it out to an excel file. I’ve gone through multiple implementations of this, but so far I’ve only been able to make this work by writing the paginated query twice– once to power the table and one to fetch the data for the download (using tRPC’s useContext() hook). One reason I went this direction is because when performing the download, I don’t want the fetched data to cause a re-render of the table.
Obviously, this is not ideal and I’d prefer to reuse the same query (without the need to invoke useContext()). Has anyone solved a similar problem? I’d really like some advice on this.
7 Replies
I think a better way to do it might be to have an export-all endpoint which dumps all of the data (not paginated) so you don't need to have multiple requests for every "page"...
@erik.gh Are you aware of any response body limits I might run into going that route? Otherwise, that’s a good thought.
By default nextjs limits an api route's response size to 4mb. You can change that however:
https://nextjs.org/docs/messages/api-routes-response-size-limit
I am not sure if that's a good idea though.
Good to know. Thanks for the help!
how big is the table? if it's not too huge, maybe just keep all pages cached?
Pretty large. Some of the downloaded reports could be into the tens of thousands.
ok in that case youre describing two different queries
one lets you browse a page of a paginated table
the other one does a very different thing