-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: create a component tree that matches between client and server t…
…o avoid `useId` output mismatch (#371) Co-authored-by: Marco Pasqualetti <[email protected]>
- Loading branch information
1 parent
43910fd
commit 33aa37e
Showing
20 changed files
with
242 additions
and
99 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
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 |
---|---|---|
@@ -1,10 +1,9 @@ | ||
import type { Router } from './router' | ||
import type { ServerProps } from './types' | ||
|
||
declare global { | ||
interface Window { | ||
__TUONO__ROUTER__: Router | ||
__TUONO_SSR_PROPS__?: { | ||
props?: unknown | ||
} | ||
__TUONO_SSR_PROPS__?: ServerProps | ||
} | ||
} |
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,24 @@ | ||
import type { JSX } from 'react' | ||
import { useRouterContext } from 'tuono-router' | ||
|
||
export const ProdResources = (): JSX.Element => { | ||
const { serverSideProps } = useRouterContext() | ||
|
||
return ( | ||
<> | ||
{serverSideProps?.cssBundles.map((cssHref) => ( | ||
<link | ||
key={cssHref} | ||
rel="stylesheet" | ||
precedence="high" | ||
type="text/css" | ||
href={`/${cssHref}`} | ||
/> | ||
))} | ||
|
||
{serverSideProps?.jsBundles.map((scriptSrc) => ( | ||
<script key={scriptSrc} type="module" src={`/${scriptSrc}`}></script> | ||
))} | ||
</> | ||
) | ||
} |
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 @@ | ||
import type { JSX } from 'react' | ||
|
||
import { useRouterContext } from 'tuono-router' | ||
|
||
import { DevResources } from './DevResources' | ||
import { ProdResources } from './ProdResources' | ||
|
||
export function TuonoScripts(): JSX.Element { | ||
const { serverSideProps } = useRouterContext() | ||
|
||
return ( | ||
<> | ||
<script>{`window.__TUONO_SSR_PROPS__=${JSON.stringify(serverSideProps)}`}</script> | ||
{serverSideProps?.mode === 'Dev' && <DevResources />} | ||
{serverSideProps?.mode === 'Prod' && <ProdResources />} | ||
</> | ||
) | ||
} |
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,24 @@ | ||
import { lazy, createElement } from 'react' | ||
import type { ReactElement } from 'react' | ||
|
||
import type { RouteComponent } from 'tuono-router' | ||
|
||
type ImportFn = () => Promise<{ default: RouteComponent }> | ||
|
||
export const RouteLazyLoading = (factory: ImportFn): RouteComponent => { | ||
let LoadedComponent: RouteComponent | undefined | ||
const LazyComponent = lazy<RouteComponent>(factory) | ||
|
||
const loadComponent = (): Promise<void> => | ||
factory().then((module) => { | ||
LoadedComponent = module.default | ||
}) | ||
|
||
const Component = ( | ||
props: React.ComponentProps<RouteComponent>, | ||
): ReactElement => createElement(LoadedComponent || LazyComponent, props) | ||
|
||
Component.preload = loadComponent | ||
|
||
return Component | ||
} |
Oops, something went wrong.