GeezerVison is showing github.com — [Fizz] Add option to inject bootstrapping script tags after the shell… · react/react@cdb8a1d
File treeExpand file treeCollapse file tree Open diff view settings react-native-renderer/src/server react-server-dom-relay/src
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: fixtures/ssr/server/render.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,6 +21,7 @@ export default function render(url, res) { | 21 | 21 | }); | 22 | 22 | let didError = false; | 23 | 23 | const {pipe, abort} = renderToPipeableStream(<App assets={assets} />, { | | 24 | + bootstrapScripts: [assets['main.js']], | 24 | 25 | onCompleteShell() { | 25 | 26 | // If something errored before we started streaming, we set the error code appropriately. | 26 | 27 | res.statusCode = didError ? 500 : 200; | |
Collapse file Copy file name to clipboardExpand all lines: fixtures/ssr/src/components/Chrome.js -1Lines changed: 0 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -46,7 +46,6 @@ export default class Chrome extends Component { | 46 | 46 | __html: `assetManifest = ${JSON.stringify(assets)};`, | 47 | 47 | }} | 48 | 48 | /> | 49 | | - <script src={assets['main.js']} /> | 50 | 49 | </body> | 51 | 50 | </html> | 52 | 51 | ); | |
Collapse file Copy file name to clipboardExpand all lines: fixtures/ssr/yarn.lock +1-9Lines changed: 1 addition & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4265,7 +4265,7 @@ longest@^1.0.1: | 4265 | 4265 | resolved "https://registry.yarnpkg.com/longest/-/longest-1.0.1.tgz#30a0b2da38f73770e8294a0d22e6625ed77d0097" | 4266 | 4266 | integrity sha1-MKCy2jj3N3DoKUoNIuZiXtd9AJc= | 4267 | 4267 |
| 4268 | | -loose-envify@^1.0.0, loose-envify@^1.1.0: | | 4268 | +loose-envify@^1.0.0: | 4269 | 4269 | version "1.4.0" | 4270 | 4270 | resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf" | 4271 | 4271 | integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q== | @@ -5945,14 +5945,6 @@ sax@^1.2.1, sax@~1.2.1: | 5945 | 5945 | resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9" | 5946 | 5946 | integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw== | 5947 | 5947 |
| 5948 | | -scheduler@^0.20.1: | 5949 | | - version "0.20.2" | 5950 | | - resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.20.2.tgz#4baee39436e34aa93b4874bddcbf0fe8b8b50e91" | 5951 | | - integrity sha512-2eWfGgAqqWFGqtdMmcL5zCMK1U8KlXv8SQFGglL3CEtd0aDVDWgeF/YoCmvln55m5zSk3J/20hTaSBeSObsQDQ== | 5952 | | - dependencies: | 5953 | | - loose-envify "^1.1.0" | 5954 | | - object-assign "^4.1.1" | 5955 | | - | 5956 | 5948 | "semver@2 || 3 || 4 || 5", semver@^5.1.0, semver@^5.3.0: | 5957 | 5949 | version "5.7.1" | 5958 | 5950 | resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7" | |
Collapse file Copy file name to clipboardExpand all lines: fixtures/ssr2/package.json +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 15 | 15 | "concurrently": "^5.3.0", | 16 | 16 | "express": "^4.17.1", | 17 | 17 | "nodemon": "^2.0.6", | 18 | | - "react": "18.0.0-alpha-7ec4c5597", | 19 | | - "react-dom": "18.0.0-alpha-7ec4c5597", | | 18 | + "react": "link:../../build/node_modules/react", | | 19 | + "react-dom": "link:../../build/node_modules/react-dom", | 20 | 20 | "react-error-boundary": "^3.1.3", | 21 | 21 | "resolve": "1.12.0", | 22 | 22 | "rimraf": "^3.0.2", | |
Collapse file Copy file name to clipboardExpand all lines: fixtures/ssr2/server/render.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -42,6 +42,7 @@ module.exports = function render(url, res) { | 42 | 42 | <App assets={assets} /> | 43 | 43 | </DataProvider>, | 44 | 44 | { | | 45 | + bootstrapScripts: [assets['main.js']], | 45 | 46 | onCompleteShell() { | 46 | 47 | // If something errored before we started streaming, we set the error code appropriately. | 47 | 48 | res.statusCode = didError ? 500 : 200; | |
Collapse file Copy file name to clipboardExpand all lines: fixtures/ssr2/src/Html.js -1Lines changed: 0 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,7 +28,6 @@ export default function Html({assets, children, title}) { | 28 | 28 | __html: `assetManifest = ${JSON.stringify(assets)};`, | 29 | 29 | }} | 30 | 30 | /> | 31 | | - <script async src={assets['main.js']} /> | 32 | 31 | </body> | 33 | 32 | </html> | 34 | 33 | ); | |
Collapse file Copy file name to clipboardExpand all lines: fixtures/ssr2/yarn.lock +5,277Lines changed: 5277 additions & 0 deletions Large diffs are not rendered by default. Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js +24-7Lines changed: 24 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,6 +21,7 @@ let useSyncExternalStore; | 21 | 21 | let useSyncExternalStoreExtra; | 22 | 22 | let PropTypes; | 23 | 23 | let textCache; | | 24 | +let window; | 24 | 25 | let document; | 25 | 26 | let writable; | 26 | 27 | let CSPnonce = null; | @@ -56,6 +57,7 @@ describe('ReactDOMFizzServer', () => { | 56 | 57 | runScripts: 'dangerously', | 57 | 58 | }, | 58 | 59 | ); | | 60 | + window = jsdom.window; | 59 | 61 | document = jsdom.window.document; | 60 | 62 | container = document.getElementById('container'); | 61 | 63 |
| @@ -338,11 +340,18 @@ describe('ReactDOMFizzServer', () => { | 338 | 340 | ); | 339 | 341 | } | 340 | 342 |
| | 343 | + let bootstrapped = false; | | 344 | + window.__INIT__ = function() { | | 345 | + bootstrapped = true; | | 346 | + // Attempt to hydrate the content. | | 347 | + ReactDOM.hydrateRoot(container, <App isClient={true} />); | | 348 | + }; | | 349 | + | 341 | 350 | await act(async () => { | 342 | 351 | const {pipe} = ReactDOMFizzServer.renderToPipeableStream( | 343 | 352 | <App isClient={false} />, | 344 | | - | 345 | 353 | { | | 354 | + bootstrapScriptContent: '__INIT__();', | 346 | 355 | onError(x) { | 347 | 356 | loggedErrors.push(x); | 348 | 357 | }, | @@ -351,10 +360,8 @@ describe('ReactDOMFizzServer', () => { | 351 | 360 | pipe(writable); | 352 | 361 | }); | 353 | 362 | expect(loggedErrors).toEqual([]); | | 363 | + expect(bootstrapped).toBe(true); | 354 | 364 |
| 355 | | - // Attempt to hydrate the content. | 356 | | - const root = ReactDOM.createRoot(container, {hydrate: true}); | 357 | | - root.render(<App isClient={true} />); | 358 | 365 | Scheduler.unstable_flushAll(); | 359 | 366 |
| 360 | 367 | // We're still loading because we're waiting for the server to stream more content. | @@ -507,17 +514,27 @@ describe('ReactDOMFizzServer', () => { | 507 | 514 | ); | 508 | 515 | } | 509 | 516 |
| | 517 | + let bootstrapped = false; | | 518 | + window.__INIT__ = function() { | | 519 | + bootstrapped = true; | | 520 | + // Attempt to hydrate the content. | | 521 | + ReactDOM.hydrateRoot(container, <App />); | | 522 | + }; | | 523 | + | 510 | 524 | await act(async () => { | 511 | | - const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<App />); | | 525 | + const {pipe} = ReactDOMFizzServer.renderToPipeableStream(<App />, { | | 526 | + bootstrapScriptContent: '__INIT__();', | | 527 | + }); | 512 | 528 | pipe(writable); | 513 | 529 | }); | 514 | 530 |
| 515 | 531 | // We're still showing a fallback. | 516 | 532 | expect(getVisibleChildren(container)).toEqual(<div>Loading...</div>); | 517 | 533 |
| | 534 | + // We already bootstrapped. | | 535 | + expect(bootstrapped).toBe(true); | | 536 | + | 518 | 537 | // Attempt to hydrate the content. | 519 | | - const root = ReactDOM.createRoot(container, {hydrate: true}); | 520 | | - root.render(<App />); | 521 | 538 | Scheduler.unstable_flushAll(); | 522 | 539 |
| 523 | 540 | // We're still loading because we're waiting for the server to stream more content. | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMFizzServerBrowser-test.js +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -71,6 +71,22 @@ describe('ReactDOMFizzServer', () => { | 71 | 71 | ); | 72 | 72 | }); | 73 | 73 |
| | 74 | + // @gate experimental | | 75 | + it('should emit bootstrap script src at the end', async () => { | | 76 | + const stream = ReactDOMFizzServer.renderToReadableStream( | | 77 | + <div>hello world</div>, | | 78 | + { | | 79 | + bootstrapScriptContent: 'INIT();', | | 80 | + bootstrapScripts: ['init.js'], | | 81 | + bootstrapModules: ['init.mjs'], | | 82 | + }, | | 83 | + ); | | 84 | + const result = await readResult(stream); | | 85 | + expect(result).toMatchInlineSnapshot( | | 86 | + `"<div>hello world</div><script>INIT();</script><script src=\\"init.js\\" async=\\"\\"></script><script type=\\"module\\" src=\\"init.mjs\\" async=\\"\\"></script>"`, | | 87 | + ); | | 88 | + }); | | 89 | + | 74 | 90 | // @gate experimental | 75 | 91 | it('emits all HTML as one unit if we wait until the end to start', async () => { | 76 | 92 | let hasLoaded = false; | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/__tests__/ReactDOMFizzServerNode-test.js +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -82,6 +82,24 @@ describe('ReactDOMFizzServer', () => { | 82 | 82 | ); | 83 | 83 | }); | 84 | 84 |
| | 85 | + // @gate experimental | | 86 | + it('should emit bootstrap script src at the end', () => { | | 87 | + const {writable, output} = getTestWritable(); | | 88 | + const {pipe} = ReactDOMFizzServer.renderToPipeableStream( | | 89 | + <div>hello world</div>, | | 90 | + { | | 91 | + bootstrapScriptContent: 'INIT();', | | 92 | + bootstrapScripts: ['init.js'], | | 93 | + bootstrapModules: ['init.mjs'], | | 94 | + }, | | 95 | + ); | | 96 | + pipe(writable); | | 97 | + jest.runAllTimers(); | | 98 | + expect(output.result).toMatchInlineSnapshot( | | 99 | + `"<div>hello world</div><script>INIT();</script><script src=\\"init.js\\" async=\\"\\"></script><script type=\\"module\\" src=\\"init.mjs\\" async=\\"\\"></script>"`, | | 100 | + ); | | 101 | + }); | | 102 | + | 85 | 103 | // @gate experimental | 86 | 104 | it('should start writing after pipe', () => { | 87 | 105 | const {writable, output} = getTestWritable(); | |
|