src/content/docs/linter/rules/no-before-interactive-script-outside-document.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JSX and TSX" icon="seti:javascript"> :::caution This rule is part of the [nursery](/linter/#nursery) group. This means that it is experimental and the behavior can change at any time. ::: ## Summary - Rule available since: `v2.3.11` - Diagnostic Category: [`lint/nursery/noBeforeInteractiveScriptOutsideDocument`](/reference/diagnostics#diagnostic-category) - This rule doesn't have a fix. - The default severity of this rule is [**warning**](/reference/diagnostics#warning). - This rule belongs to the following domains: - [`next`](/linter/domains#next) - Sources: - Same as [`@next/next/no-before-interactive-script-outside-document`](https://nextjs.org/docs/messages/no-before-interactive-script-outside-document){
"linter": {
"rules": {
"nursery": {
"noBeforeInteractiveScriptOutsideDocument": "error"
}
}
}
}
Prevent usage of next/script's beforeInteractive strategy outside of pages/_document.js in a Next.js project.
Next.js provides a next/script component to optimize the loading of third-party scripts. Using the beforeInteractive
strategy allows scripts to be preloaded before any first-party code. beforeInteractive scripts must be placed in pages/_document.js.
This rule checks for any usage of the beforeInteractive scripts outside of these files.
// pages/index.jsx
import Script from 'next/script'
export default function Index() {
return (
<div>
<Script
src="https://example.com/script.js"
strategy="beforeInteractive"
></Script>
</div>
)
}
// pages/_document.jsx
import { Html, Head, Main, NextScript } from 'next/document'
import Script from 'next/script'
export default function Document() {
return (
<Html>
<Head />
<body>
<Main />
<NextScript />
<Script
src="https://example.com/script.js"
strategy="beforeInteractive"
></Script>
</body>
</Html>
)
}