<Script> (指令碼)
此 API 參考將幫助您了解如何使用 <Script> 元件可用的屬性。有關功能和用法,請參閱優化腳本頁面。
import Script from 'next/script'
export default function Dashboard() {
return (
<>
<Script src="https://example.com/script.js" />
</>
)
}
屬性
以下是 Script 元件可用屬性的摘要
屬性 | 範例 | 類型 | 必要 |
---|---|---|---|
來源 (src) | src="http://example.com/script" | 字串 | 除非使用內嵌程式碼,否則為必要 |
策略 (strategy) | strategy="lazyOnload" | 字串 | - |
載入時 (onLoad) | onLoad={onLoadFunc} | 函式 | - |
準備就緒時 (onReady) | onReady={onReadyFunc} | 函式 | - |
發生錯誤時 (onError) | onError={onErrorFunc} | 函式 | - |
必要屬性
<Script />
元件需要下列屬性。
src
使用 beforeInteractive
策略載入的腳本會從伺服器注入到初始 HTML 中,在任何 Next.js 模組之前下載,並按照它們放置的順序執行,在頁面上發生_任何_水合作用之前。
標記為此策略的腳本會在任何第一方程式碼之前預載和擷取,但它們的執行不會阻止頁面水合作用的發生。
beforeInteractive
腳本必須放置在根佈局 (app/layout.tsx
) 內,並且設計用於載入整個網站所需的腳本(例如,當應用程式中的任何頁面已在伺服器端載入時,腳本將會載入)。
此策略應該僅用於在頁面的任何部分變得可互動之前需要擷取的關鍵腳本。
import Script from 'next/script'
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>
{children}
<Script
src="https://example.com/script.js"
strategy="beforeInteractive"
/>
</body>
</html>
)
}
注意事項:無論
beforeInteractive
腳本放置在元件的何處,它們都將始終注入到 HTML 文件的head
內。
一些應該使用 beforeInteractive
盡快載入的腳本示例包括:
- 機器人偵測器
- Cookie 同意管理員
afterInteractive
app/page.jsimport Script from 'next/script'
export default function Page() {
return (
<>
<Script src="https://example.com/script.js" strategy="afterInteractive" />
</>
)
}
import Script from 'next/script'
export default function Page() {
return (
<>
<Script src="https://example.com/script.js" strategy="afterInteractive" />
</>
)
}
一些適合使用 afterInteractive
的腳本示例包括:
- 標籤管理員
- 分析
lazyOnload
使用 lazyOnload
策略的腳本會在瀏覽器閒置期間注入到用戶端 HTML 中,並將在頁面上所有資源都已擷取後載入。此策略應用於任何不需要及早載入的背景或低優先順序腳本。
lazyOnload
腳本可以放置在任何頁面或佈局內,並且只會在瀏覽器中開啟該頁面(或頁面群組)時載入和執行。
import Script from 'next/script'
export default function Page() {
return (
<>
<Script src="https://example.com/script.js" strategy="lazyOnload" />
</>
)
}
不需要立即載入且可以使用 lazyOnload
擷取的腳本示例包括:
- 線上客服外掛程式
- 社群媒體小工具
worker
警告:
worker
策略尚未穩定,目前無法與app
目錄一起使用。請謹慎使用。
使用 worker
策略的腳本會卸載到 Web Worker,以釋放主執行緒,並確保只有關鍵的第一方資源在主執行緒上處理。雖然此策略可用於任何腳本,但這是一種進階的使用案例,不保證支援所有第三方腳本。
要使用 worker
作為策略,必須在 next.config.js
中啟用 nextScriptWorkers
旗標。
module.exports = {
experimental: {
nextScriptWorkers: true,
},
}
worker
腳本目前只能在 pages/
目錄中使用
import Script from 'next/script'
export default function Home() {
return (
<>
<Script src="https://example.com/script.js" strategy="worker" />
</>
)
}
onLoad
警告:
onLoad
目前無法與伺服器元件一起使用,只能在客戶端元件中使用。此外,onLoad
無法與beforeInteractive
一起使用 – 請考慮改用onReady
。
某些第三方腳本要求使用者在腳本載入完成後執行 JavaScript 程式碼,才能實例化內容或呼叫函式。如果您使用 afterInteractive 或 lazyOnload 作為載入策略載入腳本,則可以使用 onLoad 屬性在腳本載入後執行程式碼。
以下是在載入程式庫後才執行 lodash 方法的範例。
'use client'
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]))
}}
/>
</>
)
}
onReady
警告:onReady
目前無法與伺服器元件一起使用,只能在客戶端元件中使用。
某些第三方腳本要求使用者在腳本載入完成後以及每次元件掛載時(例如,在路由導航後)執行 JavaScript 程式碼。您可以使用 onReady 屬性在腳本第一次載入時的載入事件之後,以及後續每次元件重新掛載後執行程式碼。
以下是如何在每次掛載元件時重新實例化 Google Maps JS 嵌入的範例。
app/page.tsx'use client'
import { useRef } from 'react'
import Script from 'next/script'
export default function Page() {
const mapRef = useRef()
return (
<>
<div ref={mapRef}></div>
<Script
id="google-maps"
src="https://maps.googleapis.com/maps/api/js"
onReady={() => {
new google.maps.Map(mapRef.current, {
center: { lat: -34.397, lng: 150.644 },
zoom: 8,
})
}}
/>
</>
)
}
onError
警告:onError
尚未與伺服器組件 (Server Components) 相容,目前只能在客戶端組件 (Client Components) 中使用。onError
無法與 beforeInteractive
載入策略搭配使用。
當腳本載入失敗時,攔截錯誤訊息是很有幫助的。這些錯誤可以使用 onError 屬性來處理。
app/page.tsx'use client'
import Script from 'next/script'
export default function Page() {
return (
<>
<Script
src="https://example.com/script.js"
onError={(e: Error) => {
console.error('Script failed to load', e)
}}
/>
</>
)
}
版本歷史
版本 變更 v13.0.0
修改 beforeInteractive
和 afterInteractive
以支援 app
目錄。 v12.2.4
新增 onReady
屬性。 v12.2.2
允許在 _document
中放置使用 beforeInteractive
的 next/script
。 v11.0.0
引入 next/script
。
警告:onReady
目前無法與伺服器元件一起使用,只能在客戶端元件中使用。
某些第三方腳本要求使用者在腳本載入完成後以及每次元件掛載時(例如,在路由導航後)執行 JavaScript 程式碼。您可以使用 onReady 屬性在腳本第一次載入時的載入事件之後,以及後續每次元件重新掛載後執行程式碼。
以下是如何在每次掛載元件時重新實例化 Google Maps JS 嵌入的範例。
'use client'
import { useRef } from 'react'
import Script from 'next/script'
export default function Page() {
const mapRef = useRef()
return (
<>
<div ref={mapRef}></div>
<Script
id="google-maps"
src="https://maps.googleapis.com/maps/api/js"
onReady={() => {
new google.maps.Map(mapRef.current, {
center: { lat: -34.397, lng: 150.644 },
zoom: 8,
})
}}
/>
</>
)
}
onError
警告:onError
尚未與伺服器組件 (Server Components) 相容,目前只能在客戶端組件 (Client Components) 中使用。onError
無法與 beforeInteractive
載入策略搭配使用。
當腳本載入失敗時,攔截錯誤訊息是很有幫助的。這些錯誤可以使用 onError 屬性來處理。
app/page.tsx'use client'
import Script from 'next/script'
export default function Page() {
return (
<>
<Script
src="https://example.com/script.js"
onError={(e: Error) => {
console.error('Script failed to load', e)
}}
/>
</>
)
}
版本歷史
版本 變更 v13.0.0
修改 beforeInteractive
和 afterInteractive
以支援 app
目錄。 v12.2.4
新增 onReady
屬性。 v12.2.2
允許在 _document
中放置使用 beforeInteractive
的 next/script
。 v11.0.0
引入 next/script
。
警告:onError
尚未與伺服器組件 (Server Components) 相容,目前只能在客戶端組件 (Client Components) 中使用。onError
無法與 beforeInteractive
載入策略搭配使用。
當腳本載入失敗時,攔截錯誤訊息是很有幫助的。這些錯誤可以使用 onError 屬性來處理。
'use client'
import Script from 'next/script'
export default function Page() {
return (
<>
<Script
src="https://example.com/script.js"
onError={(e: Error) => {
console.error('Script failed to load', e)
}}
/>
</>
)
}
版本歷史
版本 | 變更 |
---|---|
v13.0.0 | 修改 beforeInteractive 和 afterInteractive 以支援 app 目錄。 |
v12.2.4 | 新增 onReady 屬性。 |
v12.2.2 | 允許在 _document 中放置使用 beforeInteractive 的 next/script 。 |
v11.0.0 | 引入 next/script 。 |