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

[WIP] fixing #2898: no error output if dynamic component with ssr: false crashes #3009

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .idea/codeStyleSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions lib/dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ export default function dynamicComponent (p, o) {
this.state.AsyncComponent = AsyncComponent
}
})
.catch(
this.ssr
? undefined
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this silently swallow the error when it happens server side? 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it would not make anything worse than it is. Both .catch(cb?) and .then(cb?) are actually a wrappers around .then(successHandler?, errorHandler?). Let me explain it with shell example:

# jabher @ DESKTOP-DTTUH1G in /mnt/c/Users/jabher [1:05:39]
$ node
> process.on("unhandledRejection", console.warn.bind(null, 'in uncaught rejecton')); void 0
undefined
> Promise.reject('test').catch(undefined); void 0
undefined
> in uncaught rejecton test Promise {
  <rejected> 'test',
  domain:
   Domain {
     domain: null,
     _events: { error: [Function: debugDomainError] },
     _eventsCount: 1,
     _maxListeners: undefined,
     members: [] } }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, nice! 👌 So returning undefined actually will bubble it up 😄 TIL 👍

: (error) => {
window.next.renderError(error)
}
)
}

loadBundle (props) {
Expand Down
3 changes: 3 additions & 0 deletions test/integration/basic/components/crashing-in-component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default () => {
throw new Error('intentionally crashed')
}
1 change: 1 addition & 0 deletions test/integration/basic/components/crashing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error('intentionally crashed')
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import dynamic from 'next/dynamic'

const Error = dynamic(import('../../components/crashing-in-component'), { ssr: false })

export default Error
5 changes: 5 additions & 0 deletions test/integration/basic/pages/dynamic/no-ssr-crashing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import dynamic from 'next/dynamic'

const Error = dynamic(import('../../components/crashing'), { ssr: false })

export default Error
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import dynamic from 'next/dynamic'

const Error = dynamic(import('../../components/crashing-in-component'))

export default Error
5 changes: 5 additions & 0 deletions test/integration/basic/pages/dynamic/ssr-crashing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import dynamic from 'next/dynamic'

const Error = dynamic(import('../../components/crashing'))

export default Error
36 changes: 36 additions & 0 deletions test/integration/basic/test/dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ export default (context, render) => {
expect($('p').text()).toBe('Hello World 1')
})

it('should render dynamic import components crash if error was thrown in module', async () => {
const $ = await get$('/dynamic/ssr-crashing')
expect($('#__next-error').text()).toBe(/intentionally crashed/)
})

it('should render dynamic import components crash if error was thrown in component render', async () => {
const $ = await get$('/dynamic/ssr-crashing-in-component')
expect($('#__next-error').text()).toMatch(/intentionally crashed/)
})

it('should stop render dynmaic import components', async () => {
const $ = await get$('/dynamic/no-ssr')
expect($('p').text()).toBe('loading...')
Expand Down Expand Up @@ -57,6 +67,32 @@ export default (context, render) => {
browser.close()
})

it('should render the thrown error on client side if error was thrown in module', async () => {
const browser = await webdriver(context.appPort, '/dynamic/no-ssr-crashing')

while (true) {
const bodyText = await browser
.elementByCss('body').text()
if (/intentionally crashed/.test(bodyText)) break
await waitFor(1000)
}

browser.close()
})

it('should render the thrown error on client side if error was thrown in component render', async () => {
const browser = await webdriver(context.appPort, '/dynamic/no-ssr-crashing-in-component')

while (true) {
const bodyText = await browser
.elementByCss('body').text()
if (/intentionally crashed/.test(bodyText)) break
await waitFor(1000)
}

browser.close()
})

it('should render even there are no physical chunk exists', async () => {
const browser = await webdriver(context.appPort, '/dynamic/no-chunk')

Expand Down