chore: Add Babel plugin to disable Next.js link prefetching

This commit is contained in:
mauro 🤙
2024-08-27 23:13:46 +00:00
parent 8badee4776
commit 021b2cc4e4
3 changed files with 7464 additions and 150 deletions

4
.babelrc Normal file
View File

@@ -0,0 +1,4 @@
{
"presets": ["next/babel"],
"plugins": ["./babel/disable-nextjs-link-prefetching"]
}

View File

@@ -0,0 +1,26 @@
/**
* Based on the docs at https://nextjs.org/docs/api-reference/next/link, the
* only way to disable prefetching is to make sure every <Link /> has <Link
* prefetch={false} />
*
* We don't want to create a wrapper Component or go around changing every
* single <Link />, so we use this Babel Plugin to add them in at build-time.
*/
module.exports = function (babel) {
const { types: t } = babel
return {
name: 'disable-link-prefetching',
visitor: {
JSXOpeningElement(path) {
if (path.node.name.name === 'Link') {
path.node.attributes.push(
t.jSXAttribute(
t.jSXIdentifier('prefetch'),
t.stringLiteral("force-off"),
),
)
}
},
},
}
}

7584
package-lock.json generated

File diff suppressed because it is too large Load Diff