Use that compact 1.4 KB library to manually run route data requests (getServerSideProps and getStaticProps) during soft navigation. By using this library with Tanstack Query (aka react-query), you can unlock extensive client-side optimizations.
$ npm install next-query-glue
$ yarn add next-query-glue
react-query + View Transitions API
- NextQueryGlueProvider that should wrap your application. It will slightly modify router singltone.
- prepareDirectNavigation, function that makes pages router skip next data/middleware call and display page immediately. Should be called before navigation.
- getQueryKey is a function that returns key for the current page. Basically, it's just an url of the page, but without a hash part.
- getQueryFn is an async function that calls route data/middleware.
- usePageData() for getting route data, no more props drilling.
- Fetch library agnostic, you can use anything.
- Supports most next.js versions.
- Doesn't break default router behaviour.
- View Transitions API works better.
- Lets your api work slow. Potentially, 300-600 ms lag can be invisible within optimistic navigation.
- All the benefits of react-query/rtk-query/swr. Like caching, optimistic navigation, persistence storage etc.
- Complicates the code. Now your components needs to handle situation when route data is loading.
- Routes that trigger NextResponse.rewrite will act like prepareDirectNavigation wasn't called. As workaround, you can pass pathModifier function in NextQueryGlueProvider to rewrite paths on the client side.