Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
b1ea19a5
Unverified
Commit
b1ea19a5
authored
Aug 20, 2020
by
Janicklas Ralph
Committed by
GitHub
Aug 20, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Eslint preload (#16199)
Eslint rule checks for missing preloads for stylesheets. cc: @prateekbh
parent
6c9dd6c3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
168 additions
and
0 deletions
+168
-0
index.js
packages/eslint-plugin-next/lib/index.js
+2
-0
missing-preload.js
packages/eslint-plugin-next/lib/rules/missing-preload.js
+57
-0
missing-preload.test.js
test/eslint-plugin-next/missing-preload.test.js
+109
-0
No files found.
packages/eslint-plugin-next/lib/index.js
View file @
b1ea19a5
...
@@ -4,6 +4,7 @@ module.exports = {
...
@@ -4,6 +4,7 @@ module.exports = {
'no-sync-scripts'
:
require
(
'./rules/no-sync-scripts'
),
'no-sync-scripts'
:
require
(
'./rules/no-sync-scripts'
),
'no-html-link-for-pages'
:
require
(
'./rules/no-html-link-for-pages'
),
'no-html-link-for-pages'
:
require
(
'./rules/no-html-link-for-pages'
),
'no-unwanted-polyfillio'
:
require
(
'./rules/no-unwanted-polyfillio'
),
'no-unwanted-polyfillio'
:
require
(
'./rules/no-unwanted-polyfillio'
),
'missing-preload'
:
require
(
'./rules/missing-preload'
),
},
},
configs
:
{
configs
:
{
recommended
:
{
recommended
:
{
...
@@ -13,6 +14,7 @@ module.exports = {
...
@@ -13,6 +14,7 @@ module.exports = {
'@next/next/no-sync-scripts'
:
1
,
'@next/next/no-sync-scripts'
:
1
,
'@next/next/no-html-link-for-pages'
:
1
,
'@next/next/no-html-link-for-pages'
:
1
,
'@next/next/no-unwanted-polyfillio'
:
1
,
'@next/next/no-unwanted-polyfillio'
:
1
,
'@next/next/missing-preload'
:
1
,
},
},
},
},
},
},
...
...
packages/eslint-plugin-next/lib/rules/missing-preload.js
0 → 100644
View file @
b1ea19a5
module
.
exports
=
{
meta
:
{
type
:
'suggestion'
,
fixable
:
'code'
,
docs
:
{
description
:
'Ensure stylesheets are preloaded'
,
category
:
'Optimizations'
,
recommended
:
true
,
},
},
create
:
function
(
context
)
{
const
preloads
=
new
Set
()
const
links
=
new
Map
()
return
{
'Program:exit'
:
function
(
node
)
{
for
(
let
[
href
,
linkNode
]
of
links
.
entries
())
{
if
(
!
preloads
.
has
(
href
))
{
context
.
report
({
node
:
linkNode
,
message
:
'Stylesheet does not have an associated preload tag. This could potentially impact First paint.'
,
fix
:
function
(
fixer
)
{
return
fixer
.
insertTextBefore
(
linkNode
,
`<link rel="preload" href="
${
href
}
" as="style" />`
)
},
})
}
}
links
.
clear
()
preloads
.
clear
()
},
'JSXOpeningElement[name.name=link][attributes.length>0]'
:
function
(
node
)
{
const
attributes
=
node
.
attributes
.
filter
(
(
attr
)
=>
attr
.
type
===
'JSXAttribute'
)
const
rel
=
attributes
.
find
((
attr
)
=>
attr
.
name
.
name
===
'rel'
)
const
relValue
=
rel
&&
rel
.
value
.
value
const
href
=
attributes
.
find
((
attr
)
=>
attr
.
name
.
name
===
'href'
)
const
hrefValue
=
href
&&
href
.
value
.
value
const
media
=
attributes
.
find
((
attr
)
=>
attr
.
name
.
name
===
'media'
)
const
mediaValue
=
media
&&
media
.
value
.
value
if
(
relValue
===
'preload'
)
{
preloads
.
add
(
hrefValue
)
}
else
if
(
relValue
===
'stylesheet'
&&
mediaValue
!==
'print'
)
{
links
.
set
(
hrefValue
,
node
)
}
},
}
},
}
test/eslint-plugin-next/missing-preload.test.js
0 → 100644
View file @
b1ea19a5
const
rule
=
require
(
'@next/eslint-plugin-next/lib/rules/missing-preload'
)
const
RuleTester
=
require
(
'eslint'
).
RuleTester
RuleTester
.
setDefaultConfig
({
parserOptions
:
{
ecmaVersion
:
2018
,
sourceType
:
'module'
,
ecmaFeatures
:
{
modules
:
true
,
jsx
:
true
,
},
},
})
var
ruleTester
=
new
RuleTester
()
ruleTester
.
run
(
'missing-preload'
,
rule
,
{
valid
:
[
`import {Head} from 'next/document';
export class Blah extends Head {
render() {
return (
<div>
<h1>Hello title</h1>
<link href="/_next/static/css/styles.css" rel="preload" />
<link href="/_next/static/css/styles.css" rel="stylesheet" />
</div>
);
}
}`
,
`import {Head} from 'next/document';
export class Blah extends Head {
render() {
return (
<div>
<h1>Hello title</h1>
<link href="/_next/static/css/styles.css" rel="stylesheet" media="print" />
</div>
);
}
}`
,
`import {Head} from 'next/head';
export class Blah {
render() {
return (
<div>
<div>
<Head><link href="/_next/static/css/styles.css" rel="preload" /></Head>
</div>
<h1>Hello title</h1>
<Head><link href="/_next/static/css/styles.css" rel="stylesheet" /></Head>
</div>
);
}
}`
,
],
invalid
:
[
{
code
:
`
import {Head} from 'next/document';
export class Blah extends Head {
render() {
return (
<div>
<h1>Hello title</h1>
<link href="/_next/static/css/styles.css" rel="stylesheet" />
</div>
);
}
}`
,
errors
:
[
{
message
:
'Stylesheet does not have an associated preload tag. This could potentially impact First paint.'
,
type
:
'JSXOpeningElement'
,
},
],
output
:
`
import {Head} from 'next/document';
export class Blah extends Head {
render() {
return (
<div>
<h1>Hello title</h1>
<link rel="preload" href="/_next/static/css/styles.css" as="style" /><link href="/_next/static/css/styles.css" rel="stylesheet" />
</div>
);
}
}`
,
},
{
code
:
`
<div>
<link href="/_next/static/css/styles.css" rel="stylesheet" />
</div>`
,
errors
:
[
{
message
:
'Stylesheet does not have an associated preload tag. This could potentially impact First paint.'
,
type
:
'JSXOpeningElement'
,
},
],
output
:
`
<div>
<link rel="preload" href="/_next/static/css/styles.css" as="style" /><link href="/_next/static/css/styles.css" rel="stylesheet" />
</div>`
,
},
],
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment