-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow to dehydrate and restore promises (#7481)
* feat: allow to dehydrate and restore promises * fix: retries with initialPromise, but without queryFn * fix: retries for infinite queries this could use some reconciliation * refactor: streamline the way we get the queryFn between Query and InfiniteQuery * fix: only dehydrate query.promise for pending queries * feat: allow setting hydration and dehydration defaultOptions on the QueryClient * test: global defaultOptions for hydrate / dehydrate * tests: hydration of promises * feat: next15 integration test * docs: app directory prefetching example * docs: global hydrate and dehydrate options * feat: use streaming * docs: prefetching * test: useQuery with initialPromise * fix: do not leak server errors to the client * docs: typo * fix: ignore next in sherif because we test against multiple versions of next * test: await promise before clearing client to avoid error * feat: always respect the `promise` passed to hydrate, even if we already have a cached entry. * Update docs/framework/react/guides/advanced-ssr.md Co-authored-by: Fredrik Höglund <fredrik.hoglund@gmail.com> * Update docs/framework/react/guides/advanced-ssr.md Co-authored-by: Fredrik Höglund <fredrik.hoglund@gmail.com> * chore: remove leftover 'use client' * oops * docs: better text * chore: better error messages * update note * chore: fix lock file --------- Co-authored-by: Fredrik Höglund <fredrik.hoglund@gmail.com>
- Loading branch information
Showing
41 changed files
with
1,245 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** @type {import('eslint').Linter.Config} */ | ||
module.exports = { | ||
extends: ['plugin:react/jsx-runtime', 'plugin:react-hooks/recommended'], | ||
settings: { | ||
react: { | ||
version: 'detect', | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
|
||
# testing | ||
/coverage | ||
|
||
# next.js | ||
/.next/ | ||
/out/ | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
*.pem | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# local env files | ||
.env*.local | ||
|
||
# vercel | ||
.vercel | ||
|
||
# typescript | ||
*.tsbuildinfo | ||
next-env.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app). | ||
|
||
## Getting Started | ||
|
||
First, run the development server: | ||
|
||
```bash | ||
npm run dev | ||
# or | ||
yarn dev | ||
# or | ||
pnpm dev | ||
``` | ||
|
||
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. | ||
|
||
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file. | ||
|
||
This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font. | ||
|
||
## Learn More | ||
|
||
To learn more about Next.js, take a look at the following resources: | ||
|
||
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. | ||
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. | ||
|
||
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome! | ||
|
||
## Deploy on Vercel | ||
|
||
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. | ||
|
||
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details. |
Binary file not shown.
33 changes: 33 additions & 0 deletions
33
examples/react/nextjs-app-prefetching/app/get-query-client.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { QueryClient, defaultShouldDehydrateQuery } from '@tanstack/react-query' | ||
|
||
function makeQueryClient() { | ||
return new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
staleTime: 60 * 1000, | ||
}, | ||
dehydrate: { | ||
// include pending queries in dehydration | ||
shouldDehydrateQuery: (query) => | ||
defaultShouldDehydrateQuery(query) || | ||
query.state.status === 'pending', | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
let browserQueryClient: QueryClient | undefined = undefined | ||
|
||
export function getQueryClient() { | ||
if (typeof window === 'undefined') { | ||
// Server: always make a new query client | ||
return makeQueryClient() | ||
} else { | ||
// Browser: make a new query client if we don't already have one | ||
// This is very important, so we don't re-make a new client if React | ||
// suspends during the initial render. This may not be needed if we | ||
// have a suspense boundary BELOW the creation of the query client | ||
if (!browserQueryClient) browserQueryClient = makeQueryClient() | ||
return browserQueryClient | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import Providers from './providers' | ||
import type React from 'react' | ||
import type { Metadata } from 'next' | ||
|
||
export const metadata: Metadata = { | ||
title: 'Create Next App', | ||
description: 'Generated by create next app', | ||
} | ||
|
||
export default function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode | ||
}) { | ||
return ( | ||
<html lang="en"> | ||
<body> | ||
<Providers>{children}</Providers> | ||
</body> | ||
</html> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from 'react' | ||
import { HydrationBoundary, dehydrate } from '@tanstack/react-query' | ||
import { pokemonOptions } from '@/app/pokemon' | ||
import { getQueryClient } from '@/app/get-query-client' | ||
import { PokemonInfo } from './pokemon-info' | ||
|
||
export default function Home() { | ||
const queryClient = getQueryClient() | ||
|
||
void queryClient.prefetchQuery(pokemonOptions) | ||
|
||
return ( | ||
<main> | ||
<h1>Pokemon Info</h1> | ||
<HydrationBoundary state={dehydrate(queryClient)}> | ||
<PokemonInfo /> | ||
</HydrationBoundary> | ||
</main> | ||
) | ||
} |
18 changes: 18 additions & 0 deletions
18
examples/react/nextjs-app-prefetching/app/pokemon-info.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use client' | ||
|
||
import React from 'react' | ||
import { useSuspenseQuery } from '@tanstack/react-query' | ||
import { pokemonOptions } from '@/app/pokemon' | ||
|
||
export function PokemonInfo() { | ||
const { data } = useSuspenseQuery(pokemonOptions) | ||
|
||
return ( | ||
<div> | ||
<figure> | ||
<img src={data.sprites.front_shiny} height={200} alt={data.name} /> | ||
<h2>I'm {data.name}</h2> | ||
</figure> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { queryOptions } from '@tanstack/react-query' | ||
|
||
export const pokemonOptions = queryOptions({ | ||
queryKey: ['pokemon'], | ||
queryFn: async () => { | ||
const response = await fetch('https://pokeapi.co/api/v2/pokemon/25') | ||
|
||
return response.json() | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use client' | ||
import { QueryClientProvider } from '@tanstack/react-query' | ||
import { ReactQueryDevtools } from '@tanstack/react-query-devtools' | ||
import { getQueryClient } from '@/app/get-query-client' | ||
import type * as React from 'react' | ||
|
||
export default function Providers({ children }: { children: React.ReactNode }) { | ||
const queryClient = getQueryClient() | ||
|
||
return ( | ||
<QueryClientProvider client={queryClient}> | ||
{children} | ||
<ReactQueryDevtools /> | ||
</QueryClientProvider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = { | ||
eslint: { | ||
ignoreDuringBuilds: true, | ||
}, | ||
} | ||
|
||
module.exports = nextConfig |
Oops, something went wrong.