next.js ์์๋ ํ์ด์ง ์ฑ๋ฅ ๊ฐ์ ์ ์ํด ์๋ํํฐ script๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๋ next/script ์ปดํฌ๋ํธ๋ฅผ ์ ๊ณตํ๋ค.
์ฌ์ฉ๋ฒ
import
import Script from 'next/script'Page script
ํ์ด์ง ์ปดํฌ๋ํธ์์ ์ฌ์ฉ๋ ์ ์์ผ๋ฉฐ, ํ์ด์ง๊ฐ ๋ธ๋ผ์ฐ์ ์ ๋ก๋๋๋ฉด script๋ฅผ fetchํ๊ณ ์คํํ๋ค.
import Script from 'next/script'
export default function Dashboard() {
return (
<>
<Script src="https://example.com/script.js" />
</>
)
}Application script
๋ชจ๋ ๊ฒฝ๋ก์์ ๊ฐ์ script๋ฅผ ์ถ๊ฐํ๊ณ ์ถ๋ค๋ฉด Custom App ์์ ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉํ ์ ์๋ค. Page script์ ๋ง์ฐฌ๊ฐ์ง๋ก ํ์ด์ง๊ฐ ๋ก๋๋๋ฉด ์คํฌ๋ฆฝํธ๋ฅผ ๊ฐ์ ธ์จ๋ค.
// pages/_app.js
import Script from 'next/script'
export default function MyApp({ Component, pageProps }) {
return (
<>
<Script src="https://example.com/script.js" />
<Component {...pageProps} />
</>
)
}์ด๋ค ๊ฒฝ๋ก๋ก ์ ๊ทผํ๋ ์ ์คํฌ๋ฆฝํธ๋ ๋ถ๋ฌ์์ง๊ณ , next.js๋ ์ script๊ฐ ๋ฑ ํ๋ฒ๋ง ๋ถ๋ฌ์์ง์ ์๋๋ก ๋ณด์ฅํ๋ค. ํ์ง๋ง, ๋ชจ๋ ํ์ด์ง์์ ๊ฐ์ script๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๋ ๊ฑฐ์ ์์ผ๋ ์ธ๋ชจ์๋ ์ฑ๋ฅ ์ ํ๋ฅผ ๋ง๊ธฐ ์ํด ํน์ ํ์ด์ง์์๋ง script๋ฅผ ๋ถ๋ฌ์ค๋๋ก ์ค์ ํ๋ ๊ฒ์ด ์ข๋ค.
Strategy
next/script ์ปดํฌ๋ํธ์ ์์ฑ์ธ strategy๋ฅผ ํตํด ์คํฌ๋ฆฝํธ๊ฐ ๋ถ๋ฌ์ค๋ ํ์ด๋ฐ์ ๋ํด ์ค์ ํ ์ ์๋ค.
-
beforeInteractive: ์ ์ ์๊ฒ ์ ๋ฌ๋๋ ์ต์ด HTML์ ์ฝ์ ๋๋ฉฐ, next.js ์ฝ๋๋ฅผ ๋ถ๋ฌ์ hydration์ด ์ผ์ด๋๊ธฐ ์ ์ ๋ก๋ํ๋ค. ์คํฌ๋ฆฝํธ ๋ก๋๋ next.js ๋ก๋ ์ ์ ์ด๋ค์ง์ง๋ง, ์คํฌ๋ฆฝํธ ์คํ์ next.js ๋ก๋๋ฅผ ๋ง์ง ์๋๋ค.- Custom Document ์์์๋ง ์ฌ์ฉํด์ผํ๋ค. ๋ฐ๋ผ์ ๋ชจ๋ ํ์ด์ง์์ ํ์ํ ์คํฌ๋ฆฝํธ์ผ ๊ฒฝ์ฐ์๋ง ์ฌ์ฉํ๋ค.
- ๋๋ ๋ฐ๋์ ์ ์ผ ๋จผ์ ์คํํด์ผํ๋ ๊ฒฝ์ฐ์๋ง ์ฌ์ฉํ๋ค.
- ex) bot detector, cookie consent managers
-
afterInteractive: ๋ํดํธ ์ค์ ์ผ๋ก, hydration์ด ์ด๋์ ๋ ์งํ๋ ํ์ ๋ถ๋ฌ์จ๋ค.- ๊ฐ๋ฅํํ ๋นจ๋ฆฌ ๋ถ๋ฌ์์ผํ๋ ์คํฌ๋ฆฝํธ์ง๋ง, next.js ์ฝ๋๋ณด๋ค๋ ๋ฆ๊ฒ ๋ถ๋ฌ์๋ ๋ ๋ ์ฌ์ฉํ๋ค.
- ex) Analytics
-
lazyOnload: ๋ธ๋ผ์ฐ์ ๊ฐ ํ๊ฐํ ๋ ๊ฐ์ ธ์จ๋ค.(ํ์ํ ๋ชจ๋ ๋ฆฌ์์ค๋ฅผ ๋ค ๊ฐ์ ธ์จ ์ํ).- ex) Chat support plugins, Social media widgets
-
worker: ์น ์์ปค ์์์ ์คํฌ๋ฆฝํธ๋ฅผ ๋ถ๋ฌ์จ๋ค. next.js v13.1.2 ๊ธฐ์ค์ผ๋ก ๋ฒ ํ ๋จ๊ณ์ธ ๊ธฐ๋ฅ์ด๋ค.- ์ฌ์ฉํ๊ธฐ ์ํด์ ๋ค์๊ณผ ๊ฐ์ด
next.config.js์ ์ค์ ํด์ค์ผํ๋ค.
module.exports = { experimental: { nextScriptWorkers: true, }, } - ์ฌ์ฉํ๊ธฐ ์ํด์ ๋ค์๊ณผ ๊ฐ์ด
Inline Scripts
๋ค์๊ณผ ๊ฐ์ด inline์ผ๋ก ์คํฌ๋ฆฝํธ ์ฝ๋๋ฅผ ์ง์ ์์ฑํ ์ ์๋ค. ๋ค๋ง ์ด๋๋ id ์์ฑ์ ๋ง๋์ ์ง์ ํด์ผํ๋ค.
<Script id="show-banner">
{`document.getElementById('banner').classList.remove('hidden')`}
</Script>dangerouslySetInnerHTML๋ฅผ ์ฌ์ฉํ ์๋ ์๋ค.
<Script
id="show-banner"
dangerouslySetInnerHTML={{
__html: `document.getElementById('banner').classList.remove('hidden')`,
}}
/>onLoad, onReady, onError
์คํฌ๋ฆฝํธ ๋ก๋ ์ํ์ ๋ฐ๋ผ ๋ณ๋์ ์ฝ๋๋ฅผ ์คํ์ํค๊ณ ์ถ์ ์ ์๋ค. next/script์์๋ ์ด๋ฅผ ์ํด ๋ค์๊ณผ ๊ฐ์ ์์ฑ์ ์ ๊ณตํ๋ค. ์ฝ๋ฐฑํจ์ ์์์๋ ์คํฌ๋ฆฝํธ์ ์ฝ๋๋ฅผ ์ฌ์ฉํ ์ ์ด๋ค.
onLoad: ์คํฌ๋ฆฝํธ์ ๋ก๋ฉ์ด ์ข ๋ฃ๋๋ฉด ์ฝ๋ฐฑํจ์ ์คํbeforeInteractive์ผ๋๋ ์ฌ์ฉํ ์ ์์.
onReady: ์คํฌ๋ฆฝํธ์ ๋ก๋ฉ์ด ์ข ๋ฃ๋๊ณ , ๋ค๋น๊ฒ์ด์ ๋ฑ์ผ๋ก ์ปดํฌ๋ํธ๊ฐ ๋ง์ดํธ ๋ ๋๋ง๋ค ์ฝ๋ฐฑํจ์ ์คํํ๋ค.onError: ์คํฌ๋ฆฝํธ ๋ก๋ฉ์ ์คํจํ์ ๊ฒฝ์ฐ ์ฝ๋ฐฑํจ์ ์คํ
import Script from 'next/script'
export default function Page() {
return (
<>
<Script
src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"
onLoad={() => {
console.log(_.sample([1, 2, 3, 4])) // lodash ์ฝ๋ ์ฌ์ฉ๊ฐ๋ฅ
}}
/>
</>
)
}์ถ๊ฐ์ ์ธ attributes
next/script์์๋ ์ผ๋ฐ์ ์ธ <script> ํ๊ทธ์ DOM attribute ์ค next/script์์ ์ฌ์ฉํ์ง ์๋ attribute๋ฅผ <script>ํ๊ทธ์ ๋ถ์ฌํ๋ค. ๋ฐ๋ผ์ nonce๋ data-*๋ก ์์ํ๋ custom data attribute๋ ์ฌ์ฉํ ์ ์๋ค.
import Script from 'next/script'
export default function Page() {
return (
<>
<Script
src="https://example.com/script.js"
id="example-script"
nonce="XUENAJFW"
data-test="script"
/>
</>
)
}