Commit 1882acb2 authored by Jiachi Liu's avatar Jiachi Liu

fix: eslint rule of using img in metadata routes (#74864)

parent e204a30a
import path = require('path')
import { defineRule } from '../utils/define-rule' import { defineRule } from '../utils/define-rule'
const url = 'https://nextjs.org/docs/messages/no-img-element' const url = 'https://nextjs.org/docs/messages/no-img-element'
...@@ -15,6 +16,14 @@ export = defineRule({ ...@@ -15,6 +16,14 @@ export = defineRule({
schema: [], schema: [],
}, },
create(context) { create(context) {
// Get relative path of the file
const relativePath = context.filename
.replace(path.sep, '/')
.replace(context.cwd, '')
.replace(/^\//, '')
const isAppDir = /^(src\/)?app\//.test(relativePath)
return { return {
JSXOpeningElement(node) { JSXOpeningElement(node) {
if (node.name.name !== 'img') { if (node.name.name !== 'img') {
...@@ -29,6 +38,14 @@ export = defineRule({ ...@@ -29,6 +38,14 @@ export = defineRule({
return return
} }
// If is metadata route files, ignore
// e.g. opengraph-image.js, twitter-image.js, icon.js
if (
isAppDir &&
/\/opengraph-image|twitter-image|icon\.\w+$/.test(relativePath)
)
return
context.report({ context.report({
node, node,
message: `Using \`<img>\` could result in slower LCP and higher bandwidth. Consider using \`<Image />\` from \`next/image\` or a custom image loader to automatically optimize images. This may incur additional usage or cost from your provider. See: ${url}`, message: `Using \`<img>\` could result in slower LCP and higher bandwidth. Consider using \`<Image />\` from \`next/image\` or a custom image loader to automatically optimize images. This may incur additional usage or cost from your provider. See: ${url}`,
......
...@@ -55,6 +55,46 @@ const tests = { ...@@ -55,6 +55,46 @@ const tests = {
); );
} }
}`, }`,
{
code: `\
import { ImageResponse } from "next/og";
export default function icon() {
return new ImageResponse(
(
<img
alt="avatar"
style={{ borderRadius: "100%" }}
width="100%"
height="100%"
src="https://example.com/image.png"
/>
)
);
}
`,
filename: `src/app/icon.js`,
},
{
code: `\
import { ImageResponse } from "next/og";
export default function Image() {
return new ImageResponse(
(
<img
alt="avatar"
style={{ borderRadius: "100%" }}
width="100%"
height="100%"
src="https://example.com/image.png"
/>
)
);
}
`,
filename: `app/opengraph-image.tsx`,
},
], ],
invalid: [ invalid: [
{ {
...@@ -91,6 +131,27 @@ const tests = { ...@@ -91,6 +131,27 @@ const tests = {
}`, }`,
errors: [{ message, type: 'JSXOpeningElement' }], errors: [{ message, type: 'JSXOpeningElement' }],
}, },
{
code: `\
import { ImageResponse } from "next/og";
export default function Image() {
return new ImageResponse(
(
<img
alt="avatar"
style={{ borderRadius: "100%" }}
width="100%"
height="100%"
src="https://example.com/image.png"
/>
)
);
}
`,
filename: `some/non-metadata-route-image.tsx`,
errors: [{ message, type: 'JSXOpeningElement' }],
},
], ],
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment