-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
WIP: Improve support for server side rendering #570
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ce6918a
feat(queryCache): add makeServerQueryCache and de/rehydration
Ephem 65b216f
test(queryCache): add ssr and de/rehydration tests for queryCache
Ephem c200859
chore(examples): add server-side-rendering example
Ephem 9645fca
docs(*): update ssr docs and add new de/rehydration apis
Ephem 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 hidden or 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 hidden or 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 @@ | ||
| logs | ||
| *.log | ||
| npm-debug.log* | ||
| .DS_Store | ||
|
|
||
| coverage | ||
| node_modules | ||
| build | ||
| .env.local | ||
| .env.development.local | ||
| .env.test.local | ||
| .env.production.local |
This file contains hidden or 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,6 @@ | ||
| # Example | ||
|
|
||
| To run this example: | ||
|
|
||
| - `npm install` or `yarn` | ||
| - `npm run start` or `yarn start` |
This file contains hidden or 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 @@ | ||
| { | ||
| "name": "react-query-server-side-rendering-example", | ||
| "version": "0.1.0", | ||
| "license": "MIT", | ||
| "scripts": { | ||
| "start": "razzle start", | ||
| "build": "razzle build", | ||
| "test": "razzle test --env=jsdom", | ||
| "start:prod": "NODE_ENV=production node build/server.js" | ||
| }, | ||
| "dependencies": { | ||
| "express": "^4.17.1", | ||
| "isomorphic-fetch": "^2.2.1", | ||
| "razzle": "^3.1.3", | ||
| "react": "^16.13.1", | ||
| "react-dom": "^16.13.1", | ||
| "react-query": "^1.5.5", | ||
| "react-router-config": "^5.1.1", | ||
| "react-router-dom": "^5.2.0", | ||
| "serialize-javascript": "^4.0.0" | ||
| } | ||
| } |
Binary file not shown.
This file contains hidden or 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,2 @@ | ||
| User-agent: * | ||
|
|
This file contains hidden or 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,26 @@ | ||
| body { | ||
| margin: 0; | ||
| padding: 0; | ||
| font-family: sans-serif; | ||
| background-color: #f0f4f8; | ||
| } | ||
|
|
||
| .App-container { | ||
| max-width: 500px; | ||
| margin: 0 auto; | ||
| min-height: 100vh; | ||
| background-color: #ffffff; | ||
| box-shadow: 0px 0px 2px #102a43; | ||
| } | ||
|
|
||
| .App-header { | ||
| text-align: center; | ||
| background-color: #222; | ||
| min-height: 70px; | ||
| padding: 20px; | ||
| color: white; | ||
| } | ||
|
|
||
| .App-content { | ||
| padding: 12px 24px; | ||
| } |
This file contains hidden or 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,24 @@ | ||
| import React from 'react' | ||
| import { Route, Switch } from 'react-router-dom' | ||
| import routes from './routes' | ||
|
|
||
| import './App.css' | ||
|
|
||
| const App = () => ( | ||
| <div className="App-container"> | ||
| <div> | ||
| <div className="App-header"> | ||
| <h2>React Query Server Side Rendering Example</h2> | ||
| </div> | ||
| <div className="App-content"> | ||
| <Switch> | ||
| {routes.map((routeConfig, index) => ( | ||
| <Route key={index} {...routeConfig} /> | ||
| ))} | ||
| </Switch> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ) | ||
|
|
||
| export default App |
This file contains hidden or 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,40 @@ | ||
| import React from 'react' | ||
| import { Link, useParams } from 'react-router-dom' | ||
| import { useQuery } from 'react-query' | ||
| import fetch from './fetch' | ||
|
|
||
| const fetchCharacter = (key, characterId) => | ||
| fetch(`https://rickandmortyapi.com/api/character/${characterId}`) | ||
|
|
||
| function Character() { | ||
| const { characterId } = useParams() | ||
| const { status, data } = useQuery(['character', characterId], fetchCharacter) | ||
|
|
||
| let character | ||
| if (status === 'loading') { | ||
| character = <p>Loading ...</p> | ||
| } else if (status === 'error') { | ||
| character = <p>Error :(</p> | ||
| } else if (status === 'success' && data) { | ||
| character = ( | ||
| <> | ||
| <h1>{data.name}</h1> | ||
| <p>Gender: {data.gender}</p> | ||
| <p>Status: {data.status}</p> | ||
| <p>Species: {data.species}</p> | ||
| </> | ||
| ) | ||
| } | ||
|
|
||
| return ( | ||
| <> | ||
| <Link to="/">Start</Link> | ||
| {character} | ||
| </> | ||
| ) | ||
| } | ||
|
|
||
| Character.prefetch = (queryCache, params) => | ||
| queryCache.prefetchQuery(['character', params.characterId], fetchCharacter) | ||
|
|
||
| export default Character |
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.
Would love to see codesandbox (using Next.js) for these
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've been wanting to try out Codesandbox with a server for a while so that sounds good, I'll see what I can do.
I added the existing Prefetching example here just because I saw it was missing from the Readme. Adding Codesandbox for that here would be scope creep, want me to remove it again until someone can add a sandbox?
I wanted to include a "low level" SSR-example in this PR to demo the nitty gritty, I think a Next-example is the logical follow-up. I kind of think that example should also be included in this PR to demo that it's compatible, I'll go ahead and add that after the vacation!