-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: react-query-next-experimental package #5598
Merged
Merged
Changes from 30 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
42c4613
chore: fix a copy-paste error
TkDodo 5777555
chore: bootstrap package
TkDodo f6344ca
chore: setup package
TkDodo 526c2e3
chore: allow passing with no tests
TkDodo dd7233c
fix: remove 'use client' from index bundle
TkDodo 6ddb1ca
chore: cleanup copy/paste error
TkDodo 2d7265b
chore: fix prettier
TkDodo c428e49
refactor: replace ref with direct props access
TkDodo 40efb37
fix: do not write to refs during render
TkDodo 7336754
refactor: inline function into useEffect to avoid useCallback
TkDodo c5169c5
fix: eslint no-shadow
TkDodo fede312
refactor: namespace id
TkDodo 9ce9513
refactor: removed pointless check
TkDodo a83ca67
fix: set to empty array on cleanup
TkDodo cb6d33f
Merge branch 'alpha' into feature/suspense
TkDodo 17edfd5
Merge branch 'alpha' into feature/suspense
TkDodo 09ac023
fix: adapt for v5 changes
TkDodo ed8baa5
chore: fix build
TkDodo 6ebe4f3
docs: add streaming example
TkDodo 595c732
chore: fix outdated lockfile
TkDodo bd75087
chore: fix prettier
TkDodo c6727fe
Merge branch 'alpha' into feature/suspense
TkDodo 83ec6f6
Merge branch 'alpha' into feature/suspense
TkDodo 4ef87c1
refactor: remove isSubscribed ref
TkDodo ba57ad8
refactor: re-arrange comment
TkDodo acf0da6
chore: remove comments
TkDodo 137433d
Merge branch 'alpha' into feature/suspense
TkDodo 63c2891
chore: fix broken lock file
TkDodo 2d45542
Merge branch 'alpha' into feature/suspense
TkDodo 4cad0d4
Merge branch 'alpha' into feature/suspense
TkDodo 9d67d8b
feat: allow customization of hydrate / dehydrate options
TkDodo 5bf15c2
Merge branch 'alpha' into feature/suspense
TkDodo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = { | ||
eslint: { | ||
ignoreDuringBuilds: true, | ||
}, | ||
experimental: { | ||
appDir: true, | ||
serverActions: true, | ||
}, | ||
webpack: (config) => { | ||
if (config.name === 'server') config.optimization.concatenateModules = false | ||
|
||
return config | ||
}, | ||
} | ||
|
||
module.exports = nextConfig |
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,23 @@ | ||
{ | ||
"name": "@tanstack/query-example-nextjs-suspense-streaming", | ||
"private": true, | ||
"license": "MIT", | ||
"scripts": { | ||
"dev": "next dev", | ||
"build": "next build", | ||
"start": "next start" | ||
}, | ||
"dependencies": { | ||
"@tanstack/react-query": "^v5.0.0-alpha.68", | ||
"@tanstack/react-query-devtools": "^v5.0.0-alpha.68", | ||
"next": "^13.4.4", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
"superjson": "^1.12.3" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "20.2.5", | ||
"@types/react": "18.2.8", | ||
"typescript": "5.1.3" | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
examples/react/nextjs-suspense-streaming/src/app/api/wait/route.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,10 @@ | ||
import { NextResponse } from 'next/server' | ||
|
||
export async function GET(request: Request) { | ||
const { searchParams } = new URL(request.url) | ||
const wait = Number(searchParams.get('wait')) | ||
|
||
await new Promise((resolve) => setTimeout(resolve, wait)) | ||
|
||
return NextResponse.json(`waited ${wait}ms`) | ||
} |
20 changes: 20 additions & 0 deletions
20
examples/react/nextjs-suspense-streaming/src/app/layout.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,20 @@ | ||
import { Providers } from './providers' | ||
|
||
export const metadata = { | ||
title: 'Next.js', | ||
description: 'Generated by Next.js', | ||
} | ||
|
||
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,89 @@ | ||
'use client' | ||
import { useQuery } from '@tanstack/react-query' | ||
import { Suspense } from 'react' | ||
|
||
// export const runtime = "edge"; // 'nodejs' (default) | 'edge' | ||
|
||
function getBaseURL() { | ||
if (typeof window !== 'undefined') { | ||
return '' | ||
} | ||
if (process.env.VERCEL_URL) { | ||
return `https://${process.env.VERCEL_URL}` | ||
} | ||
return 'http://localhost:3000' | ||
} | ||
const baseUrl = getBaseURL() | ||
function useWaitQuery(props: { wait: number }) { | ||
const query = useQuery({ | ||
queryKey: ['wait', props.wait], | ||
queryFn: async () => { | ||
const path = `/api/wait?wait=${props.wait}` | ||
const url = baseUrl + path | ||
|
||
console.log('fetching', url) | ||
const res: string = await ( | ||
await fetch(url, { | ||
cache: 'no-store', | ||
}) | ||
).json() | ||
return res | ||
}, | ||
suspense: true, | ||
}) | ||
|
||
return [query.data as string, query] as const | ||
} | ||
|
||
function MyComponent(props: { wait: number }) { | ||
const [data] = useWaitQuery(props) | ||
|
||
return <div>result: {data}</div> | ||
} | ||
|
||
export default function MyPage() { | ||
return ( | ||
<> | ||
<Suspense fallback={<div>waiting 100....</div>}> | ||
<MyComponent wait={100} /> | ||
</Suspense> | ||
<Suspense fallback={<div>waiting 200....</div>}> | ||
<MyComponent wait={200} /> | ||
</Suspense> | ||
<Suspense fallback={<div>waiting 300....</div>}> | ||
<MyComponent wait={300} /> | ||
</Suspense> | ||
<Suspense fallback={<div>waiting 400....</div>}> | ||
<MyComponent wait={400} /> | ||
</Suspense> | ||
<Suspense fallback={<div>waiting 500....</div>}> | ||
<MyComponent wait={500} /> | ||
</Suspense> | ||
<Suspense fallback={<div>waiting 600....</div>}> | ||
<MyComponent wait={600} /> | ||
</Suspense> | ||
<Suspense fallback={<div>waiting 700....</div>}> | ||
<MyComponent wait={700} /> | ||
</Suspense> | ||
|
||
<fieldset> | ||
<legend> | ||
combined <code>Suspense</code>-container | ||
</legend> | ||
<Suspense | ||
fallback={ | ||
<> | ||
<div>waiting 800....</div> | ||
<div>waiting 900....</div> | ||
<div>waiting 1000....</div> | ||
</> | ||
} | ||
> | ||
<MyComponent wait={800} /> | ||
<MyComponent wait={900} /> | ||
<MyComponent wait={1000} /> | ||
</Suspense> | ||
</fieldset> | ||
</> | ||
) | ||
} |
29 changes: 29 additions & 0 deletions
29
examples/react/nextjs-suspense-streaming/src/app/providers.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,29 @@ | ||
// app/providers.jsx | ||
'use client' | ||
|
||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query' | ||
import { ReactQueryDevtools } from '@tanstack/react-query-devtools' | ||
import React from 'react' | ||
import { ReactQueryStreamedHydration } from '@tanstack/react-query-next-experimental' | ||
|
||
export function Providers(props: { children: React.ReactNode }) { | ||
const [queryClient] = React.useState( | ||
() => | ||
new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
staleTime: 5 * 1000, | ||
}, | ||
}, | ||
}), | ||
) | ||
|
||
return ( | ||
<QueryClientProvider client={queryClient}> | ||
<ReactQueryStreamedHydration> | ||
{props.children} | ||
</ReactQueryStreamedHydration> | ||
<ReactQueryDevtools initialIsOpen={false} /> | ||
</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,25 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"lib": ["dom", "dom.iterable", "esnext"], | ||
"allowJs": true, | ||
"skipLibCheck": true, | ||
"strict": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"noEmit": true, | ||
"esModuleInterop": true, | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"jsx": "preserve", | ||
"incremental": true, | ||
"plugins": [ | ||
{ | ||
"name": "next" | ||
} | ||
] | ||
}, | ||
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], | ||
"exclude": ["node_modules"] | ||
} |
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,16 @@ | ||
// @ts-check | ||
|
||
/** @type {import('eslint').Linter.Config} */ | ||
const config = { | ||
extends: ['plugin:react/recommended', 'plugin:react-hooks/recommended'], | ||
parserOptions: { | ||
tsconfigRootDir: __dirname, | ||
project: './tsconfig.eslint.json', | ||
}, | ||
rules: { | ||
'react/jsx-key': ['error', { checkFragmentShorthand: true }], | ||
'react-hooks/exhaustive-deps': 'error', | ||
}, | ||
} | ||
|
||
module.exports = config |
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,57 @@ | ||
{ | ||
"name": "@tanstack/react-query-next-experimental", | ||
"version": "5.0.0-alpha.67", | ||
"description": "Hydration utils for React Query in the NextJs app directory", | ||
"author": "tannerlinsley", | ||
"license": "MIT", | ||
"repository": "tanstack/query", | ||
"homepage": "https://tanstack.com/query", | ||
"funding": { | ||
"type": "github", | ||
"url": "https://github.com/sponsors/tannerlinsley" | ||
}, | ||
"type": "module", | ||
"types": "build/lib/index.d.ts", | ||
"main": "build/lib/index.legacy.cjs", | ||
"module": "build/lib/index.legacy.js", | ||
"exports": { | ||
".": { | ||
"types": "./build/lib/index.d.ts", | ||
"import": "./build/lib/index.js", | ||
"require": "./build/lib/index.cjs", | ||
"default": "./build/lib/index.cjs" | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"sideEffects": false, | ||
"files": [ | ||
"build/lib/*", | ||
"src" | ||
], | ||
"scripts": { | ||
"clean": "rimraf ./build && rimraf ./coverage", | ||
"test:eslint": "eslint --ext .ts,.tsx ./src", | ||
"test:types": "tsc --noEmit", | ||
"test:lib": "vitest run --coverage --passWithNoTests", | ||
"test:lib:dev": "pnpm run test:lib --watch", | ||
"test:build": "publint --strict", | ||
"build": "pnpm build:rollup && pnpm build:types", | ||
"build:rollup": "rollup --config rollup.config.js", | ||
"build:types": "tsc --emitDeclarationOnly" | ||
}, | ||
"devDependencies": { | ||
"@tanstack/react-query": "workspace:*", | ||
"@types/react": "^18.2.4", | ||
"@types/react-dom": "^18.2.4", | ||
"next": "^13.4.6", | ||
"react": "^18.2.0", | ||
"react-dom": "^18.2.0", | ||
"react-error-boundary": "^3.1.4" | ||
}, | ||
"peerDependencies": { | ||
"@tanstack/react-query": "workspace:*", | ||
"next": "^13.0.0", | ||
"react": "^18.0.0", | ||
"react-dom": "^18.0.0" | ||
} | ||
} |
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,12 @@ | ||
// @ts-check | ||
|
||
import { defineConfig } from 'rollup' | ||
import { buildConfigs } from '../../scripts/getRollupConfig.js' | ||
|
||
export default defineConfig( | ||
buildConfigs({ | ||
name: 'react-query-next-experimental', | ||
outputFile: 'index', | ||
entryFile: './src/index.ts', | ||
}), | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this
no-store
-directive even if it's a client component?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont think it does anything on the client