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

Render error when dynamic import throws #3897

Closed
wants to merge 10 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
6 changes: 6 additions & 0 deletions lib/dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ export default function dynamicComponent (p, o) {
this.state.AsyncComponent = AsyncComponent
}
})

if (!this.ssr) {
promise.catch((error) => {
window.next.renderError(error)
Copy link

Choose a reason for hiding this comment

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

Should be window.next.renderError({err}).

export async function renderError (props) {
  const {err} = props

})
}
}

loadBundle (props) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"posttestonly": "taskr posttest",
"testall": "npm run testonly -- --coverage --forceExit --runInBand --verbose --bail",
"pretest": "npm run lint",
"test": "cross-env npm run testall || npm run testall",
"test": "cross-env npm run testonly --watch --testPathPattern basic -t \"should render an error when no ssr with error in chunk\"",
"coveralls": "nyc --instrument=false --source-map=false report --temp-directory=./coverage --reporter=text-lcov | coveralls",
"lint": "standard 'bin/*' 'client/**/*.js' 'examples/**/*.js' 'lib/**/*.js' 'pages/**/*.js' 'server/**/*.js' 'test/**/*.js'",
"prepublish": "npm run release",
Expand Down
4 changes: 2 additions & 2 deletions server/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,14 @@ export class NextScript extends Component {
__NEXT_LOADED_CHUNKS__.push({ chunkName: chunkName, fn: fn })
}

${page === '_error' && `
${page === '_error' ? `
__NEXT_REGISTER_PAGE(${htmlescape(pathname)}, function() {
var error = new Error('Page does not exist: ${htmlescape(pathname)}')
error.statusCode = 404

return { error: error }
})
`}
` : ''}
`
}} />}
{page !== '/_error' && <script async id={`__NEXT_PAGE__${pathname}`} src={`${assetPrefix}/_next/${buildId}/page${pagePathname}`} />}
Expand Down
1 change: 1 addition & 0 deletions test/integration/basic/components/hello-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error('Expected error')
5 changes: 5 additions & 0 deletions test/integration/basic/pages/dynamic/no-ssr-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import dynamic from 'next/dynamic'

const Hello = dynamic(import('../../components/hello-error'), { ssr: false })

export default Hello
14 changes: 14 additions & 0 deletions test/integration/basic/test/dynamic.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ export default (context, render) => {
browser.close()
})

it('should render an error when no ssr with error in chunk', async () => {
const browser = await webdriver(context.appPort, '/dynamic/no-ssr-error')

while (true) {
const bodyText = await browser
.elementByCss('body').text()
console.log(bodyText)
if (/Expected error/.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
1 change: 1 addition & 0 deletions test/integration/basic/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ describe('Basic Features', () => {
renderViaHTTP(context.appPort, '/nav/as-path-using-router'),

renderViaHTTP(context.appPort, '/nested-cdm/index'),
renderViaHTTP(context.appPort, '/dynamic/no-ssr-error'),

renderViaHTTP(context.appPort, '/hmr/about'),
renderViaHTTP(context.appPort, '/hmr/contact'),
Expand Down