-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use @react-aria/ssr for isomorphic ID generation. (#1409)
* Use @react-aria/ssr for isomorphic ID generation. * Update docs/content/ssr.mdx Co-authored-by: Cole Bemis <colebemis@github.com> * Update docs/content/ssr.mdx Co-authored-by: Cole Bemis <colebemis@github.com> * Doc readability Co-authored-by: Cole Bemis <colebemis@github.com>
- Loading branch information
Showing
19 changed files
with
146 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@primer/components': minor | ||
--- | ||
|
||
Use @react-aria/ssr for isomorphic ID generation. |
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,41 @@ | ||
--- | ||
title: Server-side rendering with Primer React | ||
--- | ||
|
||
## SSR-safe ID generation | ||
|
||
Some Primer components generate their own DOM IDs. Those IDs must be isomorphic (so that server-side rendering and client-side rendering yield the same ID, avoiding hydration issues) and unique across the DOM. We use [@react-aria/ssr](https://react-spectrum.adobe.com/react-aria/ssr.html) to generate those IDs. In client-only rendering, this doesn't require any additional work. In SSR contexts, you must wrap your application with at least one `SSRProvider`: | ||
|
||
``` | ||
import {SSRProvider} from '@primer/components'; | ||
function App() { | ||
return ( | ||
<SSRProvider> | ||
<MyApplication /> | ||
</SSRProvider> | ||
) | ||
} | ||
``` | ||
|
||
`SSRProvider` maintains the context necessary to ensure IDs are consistent. In cases where some parts of the react tree are rendered asynchronously, you should wrap an additional `SSRProvider` around the conditionally rendered elements: | ||
|
||
``` | ||
function MyApplication() { | ||
const [dataA] = useAsyncData('a'); | ||
const [dataB] = useAsyncData('b'); | ||
return <> | ||
<SSRProvider> | ||
{dataA && <MyComponentA data={dataA} />} | ||
</SSRProvider> | ||
<SSRProvider> | ||
{dataB && <MyComponentB data={dataB} />} | ||
</SSRProvider> | ||
</> | ||
} | ||
``` | ||
|
||
This will ensure that the IDs are consistent for any sequencing of `dataA` and `dataB` being resolved. | ||
|
||
See also [React Aria's server side rendering documentation](https://react-spectrum.adobe.com/react-aria/ssr.html). |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Oops, something went wrong.