Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a lint rule for using next script component when using inline scr…
…ipt for Google Analytics.
- Loading branch information
Showing
3 changed files
with
168 additions
and
1 deletion.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
packages/eslint-plugin-next/lib/rules/next-script-for-ga.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,48 @@ | ||
const NodeAttributes = require('../utils/nodeAttributes.js') | ||
|
||
const GA_URL = 'www.google-analytics.com/analytics.js' | ||
|
||
module.exports = function (context) { | ||
return { | ||
JSXOpeningElement(node) { | ||
if (node.name.name !== 'script') { | ||
return | ||
} | ||
if (node.attributes.length === 0) { | ||
return | ||
} | ||
const attributes = new NodeAttributes(node) | ||
|
||
// Check if the Alternative async tag is being used to add GA. https://developers.google.com/analytics/devguides/collection/analyticsjs#alternative_async_tag | ||
if ( | ||
typeof attributes.value('src') === 'string' && | ||
attributes.value('src').includes(GA_URL) | ||
) { | ||
return context.report({ | ||
node, | ||
message: | ||
'Use the Script component for loading third party scripts. See: https://nextjs.org/docs/messages/next-script-for-ga.', | ||
}) | ||
} | ||
|
||
// Check if inline script is being used to add GA. https://developers.google.com/analytics/devguides/collection/analyticsjs#the_google_analytics_tag | ||
if (attributes.has('dangerouslySetInnerHTML')) { | ||
const htmlContent = attributes.value('dangerouslySetInnerHTML')[0].value | ||
.quasis[0].value.raw | ||
|
||
if ( | ||
htmlContent && | ||
htmlContent.includes('www.google-analytics.com/analytics.js') | ||
) { | ||
context.report({ | ||
node, | ||
message: | ||
'Use the Script component for loading third party scripts. See: https://nextjs.org/docs/messages/next-script-for-ga.', | ||
}) | ||
} | ||
} | ||
}, | ||
} | ||
} | ||
|
||
module.exports.schema = [] |
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
118 changes: 118 additions & 0 deletions
118
test/eslint-plugin-next/next-script-for-ga.unit.test.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,118 @@ | ||
const rule = require('@next/eslint-plugin-next/lib/rules/next-script-for-ga') | ||
|
||
const RuleTester = require('eslint').RuleTester | ||
|
||
RuleTester.setDefaultConfig({ | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
modules: true, | ||
jsx: true, | ||
}, | ||
}, | ||
}) | ||
|
||
var ruleTester = new RuleTester() | ||
ruleTester.run('sync-scripts', rule, { | ||
valid: [ | ||
`import Script from 'next/experimental-script' | ||
export class Blah extends Head { | ||
render() { | ||
return ( | ||
<div> | ||
<h1>Hello title</h1> | ||
<Script> | ||
{\`(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ | ||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), | ||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) | ||
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); | ||
ga('create', 'UA-XXXXX-Y', 'auto'); | ||
ga('send', 'pageview'); | ||
})\`} | ||
</Script> | ||
</div> | ||
); | ||
} | ||
}`, | ||
`import Script from 'next/experimental-script' | ||
export class Blah extends Head { | ||
render() { | ||
return ( | ||
<div> | ||
<h1>Hello title</h1> | ||
<Script> | ||
{\`window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date; | ||
ga('create', 'UA-XXXXX-Y', 'auto'); | ||
ga('send', 'pageview'); | ||
})\`} | ||
</Script> | ||
</div> | ||
); | ||
} | ||
}`, | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: ` | ||
export class Blah extends Head { | ||
render() { | ||
return ( | ||
<div> | ||
<h1>Hello title</h1> | ||
<script dangerouslySetInnerHTML={{ | ||
__html: \` | ||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ | ||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), | ||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) | ||
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); | ||
ga('create', 'UA-XXXXX-Y', 'auto'); | ||
ga('send', 'pageview'); | ||
\`, | ||
}}/> | ||
</div> | ||
); | ||
} | ||
}`, | ||
errors: [ | ||
{ | ||
message: | ||
'Use the Script component for loading third party scripts. See: https://nextjs.org/docs/messages/next-script-for-ga.', | ||
type: 'JSXOpeningElement', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
export class Blah extends Head { | ||
render() { | ||
return ( | ||
<div> | ||
<h1>Hello title</h1> | ||
<script dangerouslySetInnerHTML={{ | ||
__html: \` | ||
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date; | ||
ga('create', 'UA-XXXXX-Y', 'auto'); | ||
ga('send', 'pageview'); | ||
\`, | ||
}}/> | ||
<script async src='https://www.google-analytics.com/analytics.js'></script> | ||
</div> | ||
); | ||
} | ||
}`, | ||
errors: [ | ||
{ | ||
message: | ||
'Use the Script component for loading third party scripts. See: https://nextjs.org/docs/messages/next-script-for-ga.', | ||
type: 'JSXOpeningElement', | ||
}, | ||
], | ||
}, | ||
], | ||
}) |