-
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.
Keep prerender component tree the same as regular render tree (#7760)
* Keep prerender component tree the same as regular render tree * Comment loader() type cast * Update test project * renderedLoadingState * syncLoader * No renderLoadingState * set state depending on prerender * Fix unprerendered pages * Use require.resolveWeak * save progress * working changes - update babel plugin to add webpack chunk name - remove server markup - revert hydrate logic - insert chunk in prerendered html file * clean up from late-night pairing session * set with prerender prop * rename syncLoader to prerenderLoader * update comment * add wip codemod * finish codemod * add codemod test * Update unit tests to match new page loaders * Update index.html in test project fixture * active-route-loader: fix initial load * Only use prerenderLoader in production * Detect prerendered pages * revert changes to analyzeRouterTree * Don't try to load separate chunk for / * createCell: Suspense null fallback
- Loading branch information
Showing
19 changed files
with
353 additions
and
131 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
27 changes: 27 additions & 0 deletions
27
packages/codemods/src/codemods/v5.x.x/checkReactRoot/README.md
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,27 @@ | ||
# Check React Root | ||
|
||
React 18 doesn't handle hydration errors the same way 17 did. It's very strict, so we have to be very careful about the server HTML we send to the browser to be hydrated. | ||
|
||
In v5, we changed the default index.html file a bit—we removed the `prerenderPlaceholder`: | ||
|
||
```diff | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<link rel="icon" type="image/png" href="/favicon.png" /> | ||
</head> | ||
|
||
<body> | ||
<div id="redwood-app"> | ||
- <!-- Please keep the line below for prerender support. --> | ||
- <%= prerenderPlaceholder %> | ||
</div> | ||
</body> | ||
|
||
</html> | ||
``` | ||
|
||
This codemod removes that templating syntax from a user's `index.html` file, and warns about other children in the react root. |
18 changes: 18 additions & 0 deletions
18
...mods/src/codemods/v5.x.x/checkReactRoot/__testfixtures__/default/input/web/src/index.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,18 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
|
||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<link rel="icon" type="image/png" href="/favicon.png" /> | ||
</head> | ||
|
||
<body> | ||
<div id="redwood-app"> | ||
<!-- Please keep the line below for prerender support. --> | ||
<%= prerenderPlaceholder %> | ||
<div>I shouldn't be here</div> | ||
</div> | ||
</body> | ||
|
||
</html> |
12 changes: 12 additions & 0 deletions
12
...ods/src/codemods/v5.x.x/checkReactRoot/__testfixtures__/default/output/web/src/index.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,12 @@ | ||
<!DOCTYPE html><html lang="en"><head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<link rel="icon" type="image/png" href="/favicon.png"> | ||
</head> | ||
|
||
<body> | ||
<div id="redwood-app"></div> | ||
|
||
|
||
|
||
<div>I shouldn't be here</div></body></html> |
7 changes: 7 additions & 0 deletions
7
packages/codemods/src/codemods/v5.x.x/checkReactRoot/__tests__/checkReactRoot.test.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,7 @@ | ||
import { checkReactRoot } from '../checkReactRoot' | ||
|
||
describe('tsconfigForRouteHooks', () => { | ||
it('Checks the react root', async () => { | ||
await matchFolderTransform(checkReactRoot, 'default') | ||
}) | ||
}) |
36 changes: 36 additions & 0 deletions
36
packages/codemods/src/codemods/v5.x.x/checkReactRoot/checkReactRoot.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,36 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
|
||
import { load } from 'cheerio' | ||
|
||
import getRWPaths from '../../../lib/getRWPaths' | ||
|
||
export function checkReactRoot() { | ||
const indexHTMLFilepath = path.join( | ||
getRWPaths().web.base, | ||
'src', | ||
'index.html' | ||
) | ||
|
||
const indexHTML = load(fs.readFileSync(indexHTMLFilepath, 'utf-8')) | ||
|
||
const reactRoot = indexHTML('#redwood-app') | ||
const reactRootChildren = reactRoot.children() | ||
|
||
if (reactRootChildren.length) { | ||
console.log( | ||
[ | ||
`The react root (<div id="redwood-app"></div>) in ${indexHTMLFilepath} has children:`, | ||
reactRoot.html(), | ||
'React expects to control this DOM node completely. This codemod has moved the children outside the react root', | ||
'but consider moving them into a layout.', | ||
'', | ||
].join('\n') | ||
) | ||
} | ||
|
||
indexHTML('body').append(reactRootChildren) | ||
reactRoot.text('') | ||
|
||
fs.writeFileSync(indexHTMLFilepath, indexHTML.html()) | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/codemods/src/codemods/v5.x.x/checkReactRoot/checkReactRoot.yargs.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,13 @@ | ||
import task, { TaskInnerAPI } from 'tasuku' | ||
|
||
import { checkReactRoot } from './checkReactRoot' | ||
|
||
export const command = 'check-react-root' | ||
export const description = '(v5.x.x->v5.x.x) Converts world to bazinga' | ||
|
||
export const handler = () => { | ||
task('Check React Root', ({ setOutput }: TaskInnerAPI) => { | ||
checkReactRoot() | ||
setOutput('All done!') | ||
}) | ||
} |
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.