From 4aa1d958d4ae8a2ec4d42bf77f66d6035180bc90 Mon Sep 17 00:00:00 2001 From: handtrix Date: Tue, 18 Sep 2018 22:49:35 +0200 Subject: [PATCH] Fix with-dynamic-import example Based on the webpack4 changes: https://github.com/zeit/next.js/commit/75476a91362d6dc4e9fe7bda103adeab1e81f2d8#diff-0730bb7c2e8f9ea2438b52e419dd86c9L951 --- examples/with-dynamic-import/next.config.js | 2 +- examples/with-dynamic-import/pages/index.js | 77 ++++++++++++--------- 2 files changed, 45 insertions(+), 34 deletions(-) diff --git a/examples/with-dynamic-import/next.config.js b/examples/with-dynamic-import/next.config.js index 919b7305071f0..b12eae1832904 100644 --- a/examples/with-dynamic-import/next.config.js +++ b/examples/with-dynamic-import/next.config.js @@ -1,7 +1,7 @@ module.exports = { exportPathMap: function () { return { - '/': { page: '/' }, + '/': { page: '/', query: { showMore: false } }, '/about': { page: '/about' } } } diff --git a/examples/with-dynamic-import/pages/index.js b/examples/with-dynamic-import/pages/index.js index 6e4a9a0165e3b..7af408fb3e2e3 100644 --- a/examples/with-dynamic-import/pages/index.js +++ b/examples/with-dynamic-import/pages/index.js @@ -4,35 +4,39 @@ import Header from '../components/Header' import Counter from '../components/Counter' import dynamic from 'next/dynamic' -const DynamicComponent = dynamic(import('../components/hello1')) -const DynamicComponentWithCustomLoading = dynamic( - import('../components/hello2'), - { - loading: () => (

...

) - } -) -const DynamicComponentWithNoSSR = dynamic( - import('../components/hello3'), - { ssr: false } -) -const DynamicComponent5 = dynamic(import('../components/hello5')) +const DynamicComponent1 = dynamic(import('../components/hello1')) + +const DynamicComponent2WithCustomLoading = dynamic({ + loader: () => import('../components/hello2'), + loading: () => (

Loading caused by client page transition ...

) +}) + +const DynamicComponent3WithNoSSR = dynamic({ + loader: () => import('../components/hello3'), + loading: () => (

Loading ...

), + ssr: false +}) + +const DynamicComponent4 = dynamic({ + loader: () => import('../components/hello4') +}) + +const DynamicComponent5 = dynamic({ + loader: () => import('../components/hello5') +}) const DynamicBundle = dynamic({ - modules: (props) => { + modules: () => { const components = { - Hello6: import('../components/hello6') + Hello6: import('../components/hello6'), + Hello7: import('../components/hello7') } - - if (props.showMore) { - components.Hello7 = import('../components/hello7') - } - return components }, render: (props, { Hello6, Hello7 }) => (
- {Hello7 ? : null} +
) }) @@ -42,7 +46,7 @@ export default class Index extends React.Component { return { showMore: Boolean(query.showMore) } } - toggleShowMore () { + toggleShowMore = () => { const { showMore } = this.props if (showMore) { Router.push('/') @@ -58,18 +62,25 @@ export default class Index extends React.Component { return (
- - - - - - { - /* - Since DynamicComponent5 does not render in the client, - it won't get downloaded. - */ - } - { React.noSuchField === true ? : null } + + {/* Load immediately, but in a sepeate bundle */} + + + {/* Show a progress indicator while loading */} + + + {/* Load only on the client side */} + + + {/* This component will never be loaded */} + {React.noSuchField && } + + {/* Load on demand */} + {showMore && } + + + {/* Load multible components in one bundle */} +

HOME PAGE is here!