๋ด๋ณด๋ด๋ฒ(๋ด๊ฐ ๋ณด๋ ค๊ณ ๋ด๊ฐ ๋ฒ์ญํ...) Next.js docs
2021๋ 8์ 3์ผ ๊ธฐ์ค Next.js ๊ณต์ ๋ฌธ์๋ฅผ ๋ฒ์ญํ๋ค.
โป ์์ด ์ ๊ณต์๋ ํด์ธ ์ ํํ๋ ์๋๊ธฐ์ ๋ฒ์ญ์๋ ์์ญ, ์ค์ญ, ๊ตฌ๊ธ ๋ฒ์ญ์ด ๋ฌด์ํ ๋ง์ ์ ์์ผ๋ฉฐ, ํผ์ ๊ณต์๋ฌธ์๋ฅผ ์ฐธ์กฐํด๊ฐ๋ฉฐ ๋ฒ์ญํ๋ค ๋ณด๋ ์คํ๋ ๋ง์ ์ ์๋ค. ์ ํํ ๋ด์ฉ์ ๊ณต์๋ฌธ์๋ฅผ ์ง์ ์ดํด๋ณด๊ฑฐ๋ ๋ค๋ฅธ ์ ๋ณด๋ค์ ๋ ์ฐพ์๋ณด๋ ๊ฒ์ ์ถ์ฒํ๋ค.
(ํ์ง๋ง ๋๊ธ ํผ๋๋ฐฑ๋ ํ์ํฉ๋๋ค๐ )
Next.js ๊ณต์๋ฌธ์ ํ์ธํ๊ธฐ>>
Next.js supports ES2020 dynamic import() for JavaScript. With it you can import JavaScript modules dynamically and work with them. They also work with SSR.
In the following example, we implement fuzzy search using fuse.js and only load the module dynamically in the browser after the user types in the search input:
Next.js๋ ์๋ฐ์คํฌ๋ฆฝํธ์ ES2020 dynamic import()๋ฅผ ์ง์ํ๋ค. ์ด๊ฒ์ผ๋ก ์๋ฐ์คํฌ๋ฆฝํธ ๋ชจ๋์ ๋์ ์ผ๋ก ๊ฐ์ ธ์์ ์์ ํ ์ ์๋ค. SSR๊ณผ๋ ์ ๋์ํ๋ค.
์๋์ ์์ ์์๋ ์ฐ๋ฆฌ๊ฐ fuzzy ๊ฒ์์ fuse.js๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌํํด๋์๋๋ฐ ์ฌ์ฉ์๊ฐ ๊ฒ์์ด๋ฅผ ์ ๋ ฅํ ํ์๋ง ๋ธ๋ผ์ฐ์ ์์ ๋์ ์ผ๋ก ๋ชจ๋์ ๋ก๋ํ๋ ๊ฒ์ ๋ณด์ฌ์ค๋ค.
import { useState } from 'react'
const names = ['Tim', 'Joe', 'Bel', 'Max', 'Lee']
export default function Page() {
const [results, setResults] = useState()
return (
<div>
<input
type="text"
placeholder="Search"
onChange={async (e) => {
const { value } = e.currentTarget
// Dynamically load fuse.js
const Fuse = (await import('fuse.js')).default
const fuse = new Fuse(names)
setResults(fuse.search(value))
}}
/>
<pre>Results: {JSON.stringify(results, null, 2)}</pre>
</div>
)
}
You can think of dynamic imports as another way to split your code into manageable chunks.
React components can also be imported using dynamic imports, but in this case we use it in conjunction with next/dynamic to make sure it works like any other React Component. Check out the sections below for more details on how it works.
dynamic import๋ฅผ ์ฝ๋๋ฅผ ๊ด๋ฆฌํ๊ธฐ ์ข์ ๋ฉ์ด๋ฆฌ๋ก ๋๋๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก๋ ์๊ฐํ ์๋ ์๋ค.
React ์ปดํฌ๋ํธ ์ญ์ dynamic import๋ก ๊ฐ์ ธ์ฌ ์ ์์ง๋ง ์ด ๊ฒฝ์ฐ์ next/dynamic๊ณผ ํจ๊ป ์ฌ์ฉํ์ฌ ๋ค๋ฅธ React ์ปดํฌ๋ํธ์ฒ๋ผ ๋์ํ๋์ง ํ์ธํ๋ค. ์ด๋ป๊ฒ ๋์ํ๋์ง์ ๋ํ ๋ ์์ธํ ์ ์ ์๋ ๋ถ๋ถ์ ํ์ธํด๋ณด๋ผ.
Basic usage
In the following example, the module ../components/hello will be dynamically loaded by the page:
์๋์ ์์ ์์๋ ../components/hello ๋ชจ๋์ด ํ์ด์ง์์ ๋์ ์ผ๋ก ๋ก๋๋ ๊ฒ์ด๋ค.
import dynamic from 'next/dynamic'
const DynamicComponent = dynamic(() => import('../components/hello'))
function Home() {
return (
<div>
<Header />
<DynamicComponent />
<p>HOME PAGE is here!</p>
</div>
)
}
export default Home
DynamicComponent will be the default component returned by ../components/hello. It works like a regular React Component, and you can pass props to it as you normally would.
์์ ์ DynamicComponent๋ ../components/hello์์ ๋ฐํ๋ default ์ปดํฌ๋ํธ์ด๋ค. ๋ณดํต์ React ์ปดํฌ๋ํธ์ฒ๋ผ ๋์ํ๋ฉฐ, ๋ณดํตํ๋ ๊ฒ๊ณผ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก props์ ๋๊ฒจ์ค ์ ์๋ค.
Note: In import('path/to/component'), the path must be explicitly written. It can't be a template string nor a variable. Furthermore the import() has to be inside the dynamic() call for Next.js to be able to match webpack bundles / module ids to the specific dynamic() call and preload them before rendering. dynamic() can't be used inside of React rendering as it needs to be marked in the top level of the module for preloading to work, similar to React.lazy.
import('path/to/component')์์ ๊ฒฝ๋ก๋ ๋ฐ๋์ ๋ช ์์ ์ผ๋ก ์์ฑ๋์ด์ผํ๋ค. ๊ฒฝ๋ก๋ ํ ํ๋ฆฟ ๋ฌธ์์ด์ด๋ ๋ณ์์ฌ์๋ ์๋๋ค. ๋ํ import()๋ Next.js๊ฐ ์นํฉ ๋ฒ๋ค/๋ชจ๋ id๋ฅผ ํน์ dynamic() ํธ์ถ๊ณผ ์ผ์น์ํค๊ณ ๋ ๋๋ง ์ ์ ํ๋ฆฌ๋ก๋ํ ์ ์๋๋ก ํ๊ธฐ ์ํด์ dynamic() ํธ์ถ ๋ด๋ถ์ ์์ด์ผ๋ง ํ๋ค. dynamic()์ React.lazy์ ๋น์ทํ๊ฒ ํ๋ฆฌ๋ก๋๊ฐ ๋์ํ๊ธฐ ์ํด ๋ชจ๋์ ์ต์์ ๋ ๋ฒจ์ ํ์๋์ด์ผํ๊ธฐ ๋๋ฌธ์ React ๋ ๋ ์์์ ์ฌ์ฉํ ์ ์๋ค.
With named exports
If the dynamic component is not the default export, you can use a named export too. Consider the module ../components/hello.js which has a named export Hello:
dynamic ์ปดํฌ๋ํธ๊ฐ default export๊ฐ ์๋๋ผ๋ฉด named ์ปดํฌ๋ํธ ์ญ์ ์ฌ์ฉํ ์ ์๋ค. Hello๋ผ๋ named export๊ฐ ์๋ ../component/hello.js ๋ชจ๋์ ์ดํด๋ณด๋ผ.
export function Hello() {
return <p>Hello!</p>
}
To dynamically import the Hello component, you can return it from the Promise returned by import(), like so:
Hello ์ปดํฌ๋ํธ๋ฅผ ๋์ ์ผ๋ก ๊ฐ์ ธ์ค๋ ค๋ฉด ์๋์ฒ๋ผ import()์ ์ํด์ ๋ฐํ๋ Promise๋ก๋ถํฐ ๋ฐํํ ์ ์๋ค.
import dynamic from 'next/dynamic'
const DynamicComponent = dynamic(() =>
import('../components/hello').then((mod) => mod.Hello)
)
function Home() {
return (
<div>
<Header />
<DynamicComponent />
<p>HOME PAGE is here!</p>
</div>
)
}
export default Home
With custom loading component
An optional loading component can be added to render a loading state while the dynamic component is being loaded. For example:
dynamic ์ปดํฌ๋ํธ๊ฐ ๋ก๋๋๋ ๋์ ๋ก๋ฉ ์ํ๋ฅผ ๋ ๋ํ๋ ์ ํ์ ์ธ ๋ก๋ฉ ์ปดํฌ๋ํธ๊ฐ ์ถ๊ฐ๋ ์ ์๋ค. ์๋ฅผ ๋ค๋ฉด:
import dynamic from 'next/dynamic'
const DynamicComponentWithCustomLoading = dynamic(
() => import('../components/hello'),
{ loading: () => <p>...</p> }
)
function Home() {
return (
<div>
<Header />
<DynamicComponentWithCustomLoading />
<p>HOME PAGE is here!</p>
</div>
)
}
export default Home
With no SSR
You may not always want to include a module on server-side. For example, when the module includes a library that only works in the browser.
Take a look at the following example:
ํญ์ ์๋ฒ ์ธก์ ๋ชจ๋์ ์ถ๊ฐํ๊ณ ์ถ์ง ์์ ์ ์๋ค. ์๋ฅผ ๋ค๋ฉด, ๋ชจ๋์ด ๋ธ๋ผ์ฐ์ ์์๋ง ๋์ํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ํฌํจํ ๊ฒฝ์ฐ์ด๋ค.
์๋์ ์์ ๋ฅผ ์ดํด๋ณด๋ผ:
import dynamic from 'next/dynamic'
const DynamicComponentWithNoSSR = dynamic(
() => import('../components/hello3'),
{ ssr: false }
)
function Home() {
return (
<div>
<Header />
<DynamicComponentWithNoSSR />
<p>HOME PAGE is here!</p>
</div>
)
}
export default Home
'Next.js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ด๋ณด๋ด๋ฒ] How to choose between Next.js CSR, SSR, SSG, and ISR (0) | 2022.03.07 |
---|---|
[๋ด๋ณด๋ด๋ฒ] Understanding Next.js Data Fetching (CSR, SSR, SSG, ISR) (0) | 2022.03.06 |
[๋ด๋ณด๋ด๋ฒ] Next.js docs - Authentication (2) | 2021.08.02 |
[๋ด๋ณด๋ด๋ฒ] Next.js docs - Deployment (0) | 2021.07.11 |
[๋ด๋ณด๋ด๋ฒ] Next.js docs - Response Helpers (0) | 2021.04.17 |