-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f97aeb
commit d852a49
Showing
317 changed files
with
4,436 additions
and
236 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,7 @@ | |
"imports": { | ||
"react": "https://esm.sh/[email protected]?pin=v126&dev", | ||
"react-dom": "https://esm.sh/[email protected]?pin=v126&dev", | ||
"react-dom/client": "https://esm.sh/[email protected]/client?pin=v126&dev", | ||
"react-error-boundary": "https://esm.sh/[email protected]?pin=126&dev" | ||
"react-dom/client": "https://esm.sh/[email protected]/client?pin=v126&dev" | ||
} | ||
} | ||
</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
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 |
---|---|---|
|
@@ -9,8 +9,7 @@ | |
"imports": { | ||
"react": "https://esm.sh/[email protected]?pin=v126&dev", | ||
"react-dom": "https://esm.sh/[email protected]?pin=v126&dev", | ||
"react-dom/client": "https://esm.sh/[email protected]/client?pin=v126&dev", | ||
"react-error-boundary": "https://esm.sh/[email protected]?pin=126&dev" | ||
"react-dom/client": "https://esm.sh/[email protected]/client?pin=v126&dev" | ||
} | ||
} | ||
</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
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 +1,7 @@ | ||
# RSCs | ||
|
||
👨💼 Right now we have to load all the data of our app and pass the result as | ||
props to our components. Additionally, we're sending a bunch of code to the | ||
client to render a page that really doesn't need to be on the client. We want | ||
you to solve both of these problems by using `react-server-dom-esm` which will | ||
allow us to generated serialized JSX on the server and render it in the browser. |
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 |
---|---|---|
|
@@ -9,8 +9,7 @@ | |
"imports": { | ||
"react": "https://esm.sh/[email protected]?pin=v126&dev", | ||
"react-dom": "https://esm.sh/[email protected]?pin=v126&dev", | ||
"react-dom/client": "https://esm.sh/[email protected]/client?pin=v126&dev", | ||
"react-error-boundary": "https://esm.sh/[email protected]?pin=126&dev" | ||
"react-dom/client": "https://esm.sh/[email protected]/client?pin=v126&dev" | ||
} | ||
} | ||
</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
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 |
---|---|---|
|
@@ -10,8 +10,7 @@ | |
"react": "https://esm.sh/[email protected]?pin=v126&dev", | ||
"react-dom": "https://esm.sh/[email protected]?pin=v126&dev", | ||
"react-dom/client": "https://esm.sh/[email protected]/client?pin=v126&dev", | ||
"react-error-boundary": "https://esm.sh/[email protected]?pin=126&dev", | ||
"react-server-dom-esm/client": "/js/react-server-dom-esm/client" | ||
"react-server-dom-esm/client": "https://esm.sh/@kentcdodds/[email protected]/client?pin=v126&dev" | ||
} | ||
} | ||
</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
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
4 changes: 1 addition & 3 deletions
4
exercises/02.server-components/01.solution.rsc/src/ship-details.js
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
4 changes: 1 addition & 3 deletions
4
exercises/02.server-components/01.solution.rsc/src/ship-search-results.js
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions
1
exercises/02.server-components/02.problem.async-components/README.mdx
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 @@ | ||
# Async Components |
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...ponents/02.problem.streaming/package.json → .../02.problem.async-components/package.json
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
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
|
@@ -10,8 +10,7 @@ | |
"react": "https://esm.sh/[email protected]?pin=v126&dev", | ||
"react-dom": "https://esm.sh/[email protected]?pin=v126&dev", | ||
"react-dom/client": "https://esm.sh/[email protected]/client?pin=v126&dev", | ||
"react-error-boundary": "https://esm.sh/[email protected]?pin=126&dev", | ||
"react-server-dom-esm/client": "/js/react-server-dom-esm/client" | ||
"react-server-dom-esm/client": "https://esm.sh/@kentcdodds/[email protected]/client?pin=v126&dev" | ||
} | ||
} | ||
</script> | ||
|
File renamed without changes.
69 changes: 69 additions & 0 deletions
69
exercises/02.server-components/02.problem.async-components/server/app.js
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,69 @@ | ||
import closeWithGrace from 'close-with-grace' | ||
import compress from 'compression' | ||
import express from 'express' | ||
import { createElement as h } from 'react' | ||
import { renderToPipeableStream } from 'react-server-dom-esm/server' | ||
import { getShip, searchShips } from '../db/ship-api.js' | ||
import { App } from '../src/app.js' | ||
|
||
const PORT = process.env.PORT || 3000 | ||
|
||
const app = express() | ||
app.use(compress()) | ||
// this is here so the workshop app knows when the server has started | ||
app.head('/', (req, res) => res.status(200).end()) | ||
|
||
app.use(express.static('public', { index: false })) | ||
app.use('/js/src', express.static('src')) | ||
|
||
// This just cleans up the URL if the search ever gets cleared... Not important | ||
// for RSCs... Just ... I just can't help myself. I like URLs clean. | ||
app.use((req, res, next) => { | ||
if (req.query.search === '') { | ||
const searchParams = new URLSearchParams(req.search) | ||
searchParams.delete('search') | ||
const location = [req.path, searchParams.toString()] | ||
.filter(Boolean) | ||
.join('?') | ||
return res.redirect(302, location) | ||
} else { | ||
next() | ||
} | ||
}) | ||
|
||
app.get('/rsc/:shipId?', async function (req, res) { | ||
try { | ||
const shipId = req.params.shipId || null | ||
const search = req.query.search || '' | ||
const ship = shipId ? await getShip({ shipId }) : null | ||
const shipResults = await searchShips({ search }) | ||
const props = { shipId, search, ship, shipResults } | ||
const { pipe } = renderToPipeableStream(h(App, props)) | ||
pipe(res) | ||
} catch (error) { | ||
console.error(error) | ||
res.status(500).json({ error: error.message }) | ||
} | ||
}) | ||
|
||
app.get('/:shipId?', async function (req, res) { | ||
res.set('Content-type', 'text/html') | ||
return res.sendFile('index.html', { root: 'public' }) | ||
}) | ||
|
||
const server = app.listen(PORT, () => { | ||
console.log(`🚀 We have liftoff!`) | ||
console.log(`http://localhost:${PORT}`) | ||
}) | ||
|
||
closeWithGrace(async ({ signal, err }) => { | ||
if (err) console.error('Shutting down server due to error', err) | ||
else console.log('Shutting down server due to signal', signal) | ||
|
||
await new Promise((resolve, reject) => { | ||
server.close(err => { | ||
if (err) reject(err) | ||
else resolve() | ||
}) | ||
}) | ||
}) |
37 changes: 37 additions & 0 deletions
37
exercises/02.server-components/02.problem.async-components/src/app.js
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,37 @@ | ||
import { Fragment, createElement as h } from 'react' | ||
import { ShipDetails } from './ship-details.js' | ||
import { SearchResults } from './ship-search-results.js' | ||
|
||
export function App({ shipId, search, ship, shipResults }) { | ||
return h( | ||
'div', | ||
{ className: 'app' }, | ||
h( | ||
'div', | ||
{ className: 'search' }, | ||
h( | ||
Fragment, | ||
null, | ||
h( | ||
'form', | ||
{}, | ||
h('input', { | ||
name: 'search', | ||
placeholder: 'Filter ships...', | ||
type: 'search', | ||
defaultValue: search, | ||
autoFocus: true, | ||
}), | ||
), | ||
h('ul', null, h(SearchResults, { shipId, search, shipResults })), | ||
), | ||
), | ||
h( | ||
'div', | ||
{ className: 'details' }, | ||
shipId | ||
? h(ShipDetails, { shipId, ship }) | ||
: h('p', null, 'Select a ship from the list to see details'), | ||
), | ||
) | ||
} |
File renamed without changes.
File renamed without changes.
43 changes: 43 additions & 0 deletions
43
exercises/02.server-components/02.problem.async-components/src/ship-details.js
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,43 @@ | ||
import { createElement as h } from 'react' | ||
import { getImageUrlForShip } from './img-utils.js' | ||
|
||
export function ShipDetails({ ship }) { | ||
const shipImgSrc = getImageUrlForShip(ship.id, { size: 200 }) | ||
return h( | ||
'div', | ||
{ className: 'ship-info' }, | ||
h( | ||
'div', | ||
{ className: 'ship-info__img-wrapper' }, | ||
h('img', { src: shipImgSrc, alt: ship.name }), | ||
), | ||
h('section', null, h('h2', null, ship.name)), | ||
h('div', null, 'Top Speed: ', ship.topSpeed, ' ', h('small', null, 'lyh')), | ||
h( | ||
'section', | ||
null, | ||
ship.weapons.length | ||
? h( | ||
'ul', | ||
null, | ||
ship.weapons.map(weapon => | ||
h( | ||
'li', | ||
{ key: weapon.name }, | ||
h('label', null, weapon.name), | ||
':', | ||
' ', | ||
h( | ||
'span', | ||
null, | ||
weapon.damage, | ||
' ', | ||
h('small', null, '(', weapon.type, ')'), | ||
), | ||
), | ||
), | ||
) | ||
: h('p', null, 'NOTE: This ship is not equipped with any weapons.'), | ||
), | ||
) | ||
} |
Oops, something went wrong.