Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure to log errors from data fetching in dev mode in the console #10652

Merged
merged 1 commit into from Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/next/next-server/server/render.tsx
Expand Up @@ -526,6 +526,7 @@ export async function renderToHTML(
if (!dev || !err) throw err
ctx.err = err
renderOpts.err = err
console.error(err)
}

if (unstable_getServerProps && !isFallback) {
Expand Down
2 changes: 2 additions & 0 deletions test/integration/prerender/pages/index.js
Expand Up @@ -2,6 +2,7 @@ import Link from 'next/link'

// eslint-disable-next-line camelcase
export async function unstable_getStaticProps() {
// throw new Error('oops from getStaticProps')
return {
props: { world: 'world', time: new Date().getTime() },
// bad-prop
Expand All @@ -12,6 +13,7 @@ export async function unstable_getStaticProps() {
const Page = ({ world, time }) => {
return (
<>
{/* <div id='after-change'>idk</div> */}
<p>hello {world}</p>
<span>time: {time}</span>
<Link href="/another?hello=world" as="/another/?hello=world">
Expand Down
28 changes: 27 additions & 1 deletion test/integration/prerender/test/index.test.js
Expand Up @@ -19,11 +19,13 @@ import {
normalizeRegEx,
startStaticServer,
initNextServerScript,
getReactErrorOverlayContent,
} from 'next-test-utils'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
const appDir = join(__dirname, '..')
const nextConfig = join(appDir, 'next.config.js')
const indexPage = join(__dirname, '../pages/index.js')
let app
let appPort
let buildId
Expand Down Expand Up @@ -413,6 +415,31 @@ const runTests = (dev = false) => {
// )
// })

it('should log error in console and browser in dev mode', async () => {
const origContent = await fs.readFile(indexPage, 'utf8')

const browser = await webdriver(appPort, '/')
expect(await browser.elementByCss('p').text()).toMatch(/hello.*?world/)

await fs.writeFile(
indexPage,
origContent
.replace('// throw new', 'throw new')
.replace('{/* <div', '<div')
.replace('</div> */}', '</div>')
)
await browser.waitForElementByCss('#after-change')
// we need to reload the page to trigger getStaticProps
await browser.refresh()

const errOverlayContent = await getReactErrorOverlayContent(browser)

await fs.writeFile(indexPage, origContent)
const errorMsg = /oops from getStaticProps/
expect(stderr).toMatch(errorMsg)
expect(errOverlayContent).toMatch(errorMsg)
})

it('should always call getStaticProps without caching in dev', async () => {
const initialRes = await fetchViaHTTP(appPort, '/something')
expect(initialRes.headers.get('cache-control')).toBeFalsy()
Expand All @@ -433,7 +460,6 @@ const runTests = (dev = false) => {
})

it('should error on bad object from getStaticProps', async () => {
const indexPage = join(__dirname, '../pages/index.js')
const origContent = await fs.readFile(indexPage, 'utf8')
await fs.writeFile(
indexPage,
Expand Down