Last active
August 8, 2024 07:19
-
-
Save wyozi/2992ee8133efd5e0ac62deabd6c51d65 to your computer and use it in GitHub Desktop.
Next.js ESLint rule to enforce prefetch={false} for links
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
const rulesDirPlugin = require("eslint-plugin-rulesdir"); | |
rulesDirPlugin.RULES_DIR = "eslint-rules"; | |
module.exports = { | |
rules: { | |
"rulesdir/no-link-prefetch": "error", | |
} | |
} |
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
module.exports = { | |
meta: { | |
type: "problem", | |
docs: { | |
description: "Enforce that Next.js <Link/> are not prefetched", | |
}, | |
schema: [], | |
}, | |
create(context) { | |
return { | |
JSXOpeningElement(node) { | |
if (node.name.type !== "JSXIdentifier" || node.name.name !== "Link") { | |
return; | |
} | |
const hasPrefetch = node.attributes.some( | |
(a) => | |
a.type === "JSXAttribute" && | |
a.name.name === "prefetch" && | |
a.value && | |
a.value.type === "JSXExpressionContainer" && | |
a.value.expression.type === "Literal" && | |
a.value.expression.value === false | |
); | |
if (hasPrefetch) { | |
return; | |
} | |
context.report({ | |
node, | |
message: | |
"<Link/> should have prefetch={false} to prevent unnecessary supabase calls", | |
}); | |
}, | |
}; | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment