-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add files needed for React Streaming SSR (#8810)
- Loading branch information
Showing
4 changed files
with
253 additions
and
34 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
26 changes: 26 additions & 0 deletions
26
packages/cli/src/commands/experimental/templates/streamingSsr/Document.tsx.template
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,26 @@ | ||
import React from 'react' | ||
|
||
import { Css, Meta } from '@redwoodjs/web' | ||
import type { TagDescriptor } from '@redwoodjs/web' | ||
|
||
interface DocumentProps { | ||
children: React.ReactNode | ||
css: string[] // array of css import strings | ||
meta?: TagDescriptor[] | ||
} | ||
|
||
export const Document: React.FC<DocumentProps> = ({ children, css, meta }) => { | ||
return ( | ||
<html lang="en"> | ||
<head> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<link rel="icon" type="image/png" href="/favicon.png" /> | ||
<Css css={css} /> | ||
<Meta tags={meta} /> | ||
</head> | ||
<body> | ||
<div id="redwood-app">{children}</div> | ||
</body> | ||
</html> | ||
) | ||
} |
38 changes: 38 additions & 0 deletions
38
packages/cli/src/commands/experimental/templates/streamingSsr/entry.client.tsx.template
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,38 @@ | ||
import { hydrateRoot, createRoot } from 'react-dom/client' | ||
|
||
// TODO (STREAMING) This was marked "temporary workaround" | ||
// Need to figure out why it's a temporary workaround and what we | ||
// should do instead. | ||
import { ServerContextProvider } from '@redwoodjs/web/dist/serverContext' | ||
|
||
import App from './App' | ||
import { Document } from './Document' | ||
|
||
/** | ||
* When `#redwood-app` isn't empty then it's very likely that you're using | ||
* prerendering. So React attaches event listeners to the existing markup | ||
* rather than replacing it. | ||
* https://reactjs.org/docs/react-dom-client.html#hydrateroot | ||
*/ | ||
const redwoodAppElement = document.getElementById('redwood-app') | ||
|
||
if (redwoodAppElement.children?.length > 0) { | ||
hydrateRoot( | ||
document, | ||
<ServerContextProvider value={{}}> | ||
<Document css={window.__assetMap?.()?.css}> | ||
<App /> | ||
</Document> | ||
</ServerContextProvider> | ||
) | ||
} else { | ||
console.log('Rendering from scratch') | ||
const root = createRoot(document) | ||
root.render( | ||
<ServerContextProvider value={{}}> | ||
<Document css={window.__assetMap?.()?.css}> | ||
<App /> | ||
</Document> | ||
</ServerContextProvider> | ||
) | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/cli/src/commands/experimental/templates/streamingSsr/entry.server.tsx.template
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 @@ | ||
import { LocationProvider } from '@redwoodjs/router' | ||
import { ServerContextProvider } from '@redwoodjs/web/dist/serverContext' | ||
|
||
import App from './App' | ||
import { Document } from './Document' | ||
|
||
interface Props { | ||
routeContext: any | ||
url: string | ||
css: string[] | ||
meta?: any[] | ||
} | ||
|
||
export const ServerEntry: React.FC<Props> = ({ | ||
routeContext, | ||
url, | ||
css, | ||
meta, | ||
}) => { | ||
return ( | ||
<ServerContextProvider value={routeContext}> | ||
<LocationProvider location={{ pathname: url }}> | ||
<Document css={css} meta={meta}> | ||
<App /> | ||
</Document> | ||
</LocationProvider> | ||
</ServerContextProvider> | ||
) | ||
} |