-
Notifications
You must be signed in to change notification settings - Fork 46.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the ESM loader used in fixtures
Wired up an example of how to call into the server
- Loading branch information
1 parent
6278554
commit b35a364
Showing
9 changed files
with
298 additions
and
89 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,49 @@ | ||
'use strict'; | ||
|
||
const {renderToPipeableStream} = require('react-server-dom-webpack/server'); | ||
const {readFile} = require('fs'); | ||
const {readFile} = require('fs').promises; | ||
const {resolve} = require('path'); | ||
const React = require('react'); | ||
|
||
module.exports = function (req, res) { | ||
// const m = require('../src/App.js'); | ||
import('../src/App.js').then(m => { | ||
const dist = process.env.NODE_ENV === 'development' ? 'dist' : 'build'; | ||
readFile( | ||
resolve(__dirname, `../${dist}/react-client-manifest.json`), | ||
'utf8', | ||
(err, data) => { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
const App = m.default.default || m.default; | ||
res.setHeader('Access-Control-Allow-Origin', '*'); | ||
const moduleMap = JSON.parse(data); | ||
const {pipe} = renderToPipeableStream( | ||
React.createElement(App), | ||
moduleMap | ||
); | ||
pipe(res); | ||
module.exports = async function (req, res) { | ||
switch (req.method) { | ||
case 'POST': { | ||
const serverReference = JSON.parse(req.get('rsc-action')); | ||
const {filepath, name} = serverReference; | ||
const action = (await import(filepath))[name]; | ||
// Validate that this is actually a function we intended to expose and | ||
// not the client trying to invoke arbitrary functions. In a real app, | ||
// you'd have a manifest verifying this before even importing it. | ||
if (action.$$typeof !== Symbol.for('react.server.reference')) { | ||
throw new Error('Invalid action'); | ||
} | ||
); | ||
}); | ||
|
||
const args = JSON.parse(req.body); | ||
const result = action.apply(null, args); | ||
|
||
res.setHeader('Access-Control-Allow-Origin', '*'); | ||
const {pipe} = renderToPipeableStream(result, {}); | ||
pipe(res); | ||
|
||
return; | ||
} | ||
default: { | ||
// const m = require('../src/App.js'); | ||
const m = await import('../src/App.js'); | ||
const dist = process.env.NODE_ENV === 'development' ? 'dist' : 'build'; | ||
const data = await readFile( | ||
resolve(__dirname, `../${dist}/react-client-manifest.json`), | ||
'utf8' | ||
); | ||
const App = m.default.default || m.default; | ||
res.setHeader('Access-Control-Allow-Origin', '*'); | ||
const moduleMap = JSON.parse(data); | ||
const {pipe} = renderToPipeableStream( | ||
React.createElement(App), | ||
moduleMap | ||
); | ||
pipe(res); | ||
return; | ||
} | ||
} | ||
}; |
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,15 @@ | ||
'use client'; | ||
|
||
import * as React from 'react'; | ||
|
||
export default function Button({action, children}) { | ||
return ( | ||
<button | ||
onClick={async () => { | ||
const result = await action(); | ||
console.log(result); | ||
}}> | ||
{children} | ||
</button> | ||
); | ||
} |
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,6 @@ | ||
'use server'; | ||
|
||
export async function like() { | ||
console.log('Like'); | ||
return 'Liked'; | ||
} |
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.