Skip to content

Commit

Permalink
fix: eslint not ignoring "/app" (vercel#50261)
Browse files Browse the repository at this point in the history
## Why?
```
src/app/layout.tsx
```
![code](https://github.com/vercel/next.js/assets/120007119/3cc991f8-a96d-4ffb-9f84-4dfdbcac5318)

**eslint[@next/next/no-before-interactive-script-outside-document](https://nextjs.org/docs/messages/no-before-interactive-script-outside-document)**
> `next/script`'s `beforeInteractive` strategy should not be used outside of `pages/_document.js`. 

## How?
- Possibility of pathname starts with "/" on OS X and Linux, so added condition to check file path starting with "/"
- e.g. "/src/app" or "/app"
- Added test code



Passed 
- `pnpm build && pnpm lint`
- `pnpm jest test/unit/eslint-plugin-next/no-before-interactive-script-outside-document.test.ts`

Extends vercel#46609
Fixes vercel#50096
Fixes NEXT-1230
  • Loading branch information
devjiwonchoi authored and hydRAnger committed Jun 12, 2023
1 parent 8617d57 commit 836ca8f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ export = defineRule({

if (startsWithUsingCorrectSeparators(pathname, 'src/')) {
pathname = pathname.slice(4)
} else if (startsWithUsingCorrectSeparators(pathname, '/src/')) {
pathname = pathname.slice(5)
}

// This rule shouldn't fire in `app/`
if (startsWithUsingCorrectSeparators(pathname, 'app/')) {
if (
startsWithUsingCorrectSeparators(pathname, 'app/') ||
startsWithUsingCorrectSeparators(pathname, '/app/')
) {
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,40 @@ ruleTester.run('no-before-interactive-script-outside-document', rule, {
}`,
filename: 'src/app/deep/randomFile.tsx',
},
{
code: `
import Script from "next/script";
export default function Index() {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
<Script
src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js?a=scriptBeforeInteractive"
strategy='beforeInteractive'
/>
</html>
);
}`,
filename: '/src/app/layout.tsx',
},
{
code: `
import Script from "next/script";
export default function Index() {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
<Script
src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js?a=scriptBeforeInteractive"
strategy='beforeInteractive'
/>
</html>
);
}`,
filename: '/app/layout.tsx',
},
].map((obj, idx) => ({
...obj,
code: `// valid-${idx}
Expand Down

0 comments on commit 836ca8f

Please sign in to comment.