Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes to the beforeInteractive strategy to make it work for streami…
…ng (#31936) Changes to the beforeInteractive strategy to make it work for streaming Splitting `beforeInteractive` into two strategies `beforeInteractive` at the _document level and `beforePageRender` for page level <Scripts>
- Loading branch information
1 parent
3b8e2f6
commit 0441f81
Showing
17 changed files
with
328 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# beforeInteractive Script component outside \_document.js | ||
|
||
#### Why This Error Occurred | ||
|
||
You can't use the `next/script` component with the `beforeInteractive` strategy outside the `_document.js` page. That's because `beforeInteractive` strategy only works inside **\_document.js** and is designed to load scripts that are needed by the entire site (i.e. the script will load when any page in the application has been loaded server-side). | ||
|
||
#### Possible Ways to Fix It | ||
|
||
If you want a global script, move the script inside `_document.js` page. | ||
|
||
```jsx | ||
// In _document.js | ||
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://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js" | ||
strategy="beforeInteractive" | ||
></Script> | ||
</body> | ||
</Html> | ||
) | ||
} | ||
``` | ||
|
||
- [next-script](https://nextjs.org/docs/basic-features/script#usage) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/eslint-plugin-next/lib/rules/no-before-interactive-script-outside-document.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const path = require('path') | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: | ||
'Disallow using next/script beforeInteractive strategy outside the next/_document component', | ||
recommended: true, | ||
url: 'https://nextjs.org/docs/messages/no-before-interactive-script-outside-document', | ||
}, | ||
}, | ||
create: function (context) { | ||
let scriptImportName = null | ||
|
||
return { | ||
'ImportDeclaration[source.value="next/script"] > ImportDefaultSpecifier'( | ||
node | ||
) { | ||
scriptImportName = node.local.name | ||
}, | ||
JSXOpeningElement(node) { | ||
if (!scriptImportName) { | ||
return | ||
} | ||
|
||
if (node.name && node.name.name !== scriptImportName) { | ||
return | ||
} | ||
|
||
const strategy = node.attributes.find( | ||
(child) => child.name && child.name.name === 'strategy' | ||
) | ||
|
||
if (!strategy || strategy?.value?.value !== 'beforeInteractive') { | ||
return | ||
} | ||
|
||
const document = context.getFilename().split('pages')[1] | ||
if (document && path.parse(document).name.startsWith('_document')) { | ||
return | ||
} | ||
|
||
context.report({ | ||
node, | ||
message: | ||
'next/script beforeInteractive strategy should only be used inside next/_document. See: https://nextjs.org/docs/messages/no-before-interactive-script-outside-document', | ||
}) | ||
}, | ||
} | ||
}, | ||
} |
30 changes: 0 additions & 30 deletions
30
packages/eslint-plugin-next/lib/rules/no-script-in-document.js
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.