Skip to content

Commit

Permalink
Fix example
Browse files Browse the repository at this point in the history
  • Loading branch information
leerob committed Feb 16, 2022
1 parent e6bfb02 commit a222ff8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
10 changes: 8 additions & 2 deletions examples/react-server-components/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# React Server Components (Alpha)

This examples shows a basic "hello world" Next.js application using React Server Components.
This examples shows a simple Next.js application using React Server Components.

TODO: Link back to docs.
Server Components are a new feature in React that let you reduce your JavaScript bundle size by separating server and client-side code. Server Components allow developers to build apps that span the server and client, combining the rich interactivity of client-side apps with the improved performance of traditional server rendering.

Check out the documentation to learn more:

- [React 18](https://nextjs.org/docs/advanced-features/react-18/overview)
- [Streaming SSR](https://nextjs.org/docs/advanced-features/react-18/streaming)
- [Server Components](https://nextjs.org/docs/advanced-features/react-18/server-components)

## Preview

Expand Down
7 changes: 5 additions & 2 deletions examples/react-server-components/components/profile.server.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { useData } from '../lib/use-data'

const url = `https://api.github.com/repos/vercel/next.js`

export default function Profile() {
const repo = useData('/api/repo', (key) => fetch(key).then((r) => r.json()))
const data = useData('star', () => fetch(url).then((r) => r.json()))

return (
<p>
<strong>Next.js: </strong>
<span>{repo.stars}</span>
<span>{data['stargazers_count']}</span>
</p>
)
}
13 changes: 3 additions & 10 deletions examples/react-server-components/lib/use-data.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
let endpoint = ''
const publicUrl = process.env.NEXT_PUBLIC_VERCEL_URL

if (publicUrl !== undefined && !publicUrl.includes('localhost')) {
endpoint = `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`
} else {
endpoint = 'http://localhost:3000'
}

const cache = {}

// This is temporary and will eventually be replaced
// with react-fetch
export function useData(key, fetcher, opts = {}) {
const now = Date.now()
function mutate() {
cache[key].isValidating = true
return fetcher(endpoint + key).then(
return fetcher(key).then(
(r) => {
cache[key].isValidating = false
cache[key].timestamp = Date.now()
Expand Down

0 comments on commit a222ff8

Please sign in to comment.