Skip to content

Commit

Permalink
Merge branch 'canary' into fix/breaking-file-loading
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] authored Oct 8, 2020
2 parents cd6f2b5 + c021662 commit 6e064da
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 13 deletions.
4 changes: 0 additions & 4 deletions examples/with-msw/.env

This file was deleted.

2 changes: 1 addition & 1 deletion examples/with-msw/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"license": "MIT",
"dependencies": {
"msw": "^0.21.0",
"msw": "^0.21.2",
"next": "latest",
"react": "^16.13.1",
"react-dom": "^16.13.1"
Expand Down
4 changes: 3 additions & 1 deletion examples/with-msw/pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
if (process.env.NEXT_PUBLIC_API_MOCKING === 'enabled') {
// Enable API mocking in all environments except production.
// This is recommended for real-world apps.
if (process.env.NODE_ENV !== 'production') {
require('../mocks')
}

Expand Down
35 changes: 28 additions & 7 deletions examples/with-msw/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState } from 'react'

export default function Home({ book }) {
export default function Home({ book, inProduction }) {
const [reviews, setReviews] = useState(null)

const handleGetReviews = () => {
Expand All @@ -10,6 +10,18 @@ export default function Home({ book }) {
.then(setReviews)
}

if (inProduction) {
return (
<div>
<p>
This example does not work in production, as MSW is not intended for
use in production. In a real-world app, your request will hit the
actual backend instead.
</p>
</div>
)
}

return (
<div>
<img src={book.imageUrl} alt={book.title} width="250" />
Expand All @@ -32,12 +44,21 @@ export default function Home({ book }) {

export async function getServerSideProps() {
// Server-side requests are mocked by `mocks/server.js`.
const res = await fetch('https://my.backend/book')
const book = await res.json()
// In a real-world app this request would hit the actual backend.
try {
const res = await fetch('https://my.backend/book')
const book = await res.json()

return {
props: {
book,
},
return {
props: {
book,
},
}
} catch (error) {
return {
props: {
inProduction: true,
},
}
}
}

0 comments on commit 6e064da

Please sign in to comment.