We can create server-side rendered React apps and static sites easily Next.js.
In this article, we’ll take a look at dynamic imports and render static pages with Next.js.
Dynamic Import
ES2020 supports dynamic imports natively.
We can use this within our Next.js to dynamically import our components.
It also works with server-side rendering.
This lets us split our code into manageable chunks.
For example, we can write:
components/name.js
function Name() {
return (
<span>world</span>
)
}
export default Name
pages/hello.js
import dynamic from 'next/dynamic'
const NameComponent = dynamic(() => import('../components/name'))
function Hello() {
return (
<div>hello <NameComponent /></div>
)
}
export default Hello
We imported the dynamic
function and passed in a callback that calls import
to import the component.
Then we can use the NameComponent
in our code.
Now we should see:
hello world
displayed.
Dynamic Component With Named Exports
We can create dynamic components with named exports.
To do that, we can write:
components/name.js
export function Name({ }) {
return (
<span>world</span>
)
}
pages/hello.js
import dynamic from 'next/dynamic'
const NameComponent = dynamic(() => import('../components/Name').then((mod) => mod.Name)
)
function Hello() {
return (
<div>hello <NameComponent /></div>
)
}
export default Hello
In the callback, we called import
with the then
callback to return the Name
component from the module.
With Custom Loading Component
We can also add a component to show the component when the component loads.
For example, we can write:
components/name.js
function Name() {
return (
<span>world</span>
)
}
export default Name
pages/hello.js
import dynamic from 'next/dynamic'
const NameComponent = dynamic(() => import('../components/Name'), { loading: () => <p>loading</p> })
function Hello() {
return (
<div>hello <NameComponent /></div>
)
}
export default Hello
We have an object with the loading
property in the code with a component as the value.
Disable Server-Side Rendering
Server-side rendering can be disabled by passing in an object into the dynamic
function with the ssr
property set to false
.
For example, we can write:
components/name.js
function Name() {
return (
<span>world</span>
)
}
export default Name
pages/hello.js
import dynamic from 'next/dynamic'
const NameComponent = dynamic(() => import('../components/Name'), { ssr: false }
)
function Hello() {
return (
<div>hello <NameComponent /></div>
)
}
export default Hello
Static HTML Export
We can export our app to static HTML with the next export
command.
It works by prerendering all the pages to HTML.
The pages can export a getStaticPaths
property that returns all the paths that we want to prerender.
next export
should be used when there are no dynamic data changes in our app.
To use it, we run:
next build && next export
next build
builds the app and next export
gets us the HTML pages.
We can also add both commands to our package.json
file:
"scripts": {
"build": "next build && next export"
}
and run npm run build
to run it.
getInitialProps
can’t be used with getStaticProps
or getStaticPaths
on any page.
Conclusion
We can dynamically import components with Next.js.
Also, we can build our Next.js app to static HTML files.