1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- const path = require('path')
- const url =
- 'https://nextjs.org/docs/messages/no-before-interactive-script-outside-document'
- module.exports = {
- meta: {
- docs: {
- description:
- "Prevent usage of `next/script`'s `beforeInteractive` strategy outside of `pages/_document.js`.",
- recommended: true,
- url,
- },
- type: 'problem',
- schema: [],
- },
- 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 ||
- 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\`'s \`beforeInteractive\` strategy should not be used outside of \`pages/_document.js\`. See: ${url}`,
- })
- },
- }
- },
- }
|