mirror of
https://github.com/hanzoai/next.js.git
synced 2026-10-30 14:46:38 +00:00
This fixes a case where depending on the timings of compilers being done could cause an infinite invalidation loop. **Before** https://user-images.githubusercontent.com/22380829/221786587-1a4cc6ab-f273-4191-92af-a57e9fff1261.mp4 **After** https://user-images.githubusercontent.com/22380829/221786611-f55c3da9-0201-40be-95a8-3ef1869d6a66.mp4 x-ref: [slack thread](https://vercel.slack.com/archives/C03KAR5DCKC/p1677441845988529?thread_ts=1677429424.151329&cid=C03KAR5DCKC) ## Bug - [x] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
38 lines
1017 B
TypeScript
38 lines
1017 B
TypeScript
import sizeOf from 'image-size'
|
|
import { join } from 'path'
|
|
import { readFile } from 'fs/promises'
|
|
import NextImage from 'next/image'
|
|
|
|
export async function Image({ src, alt = null, width = null, height = null }) {
|
|
if (!src.startsWith('data:') && (width === null || height === null)) {
|
|
let imageBuffer = null
|
|
|
|
if (src.startsWith('http')) {
|
|
imageBuffer = Buffer.from(
|
|
await fetch(src).then((res) => res.arrayBuffer())
|
|
)
|
|
} else {
|
|
imageBuffer = await readFile(
|
|
new URL(join(import.meta.url, '..', '..', '..', '..', 'public', src))
|
|
.pathname
|
|
)
|
|
}
|
|
|
|
;({ width, height } = sizeOf(imageBuffer))
|
|
}
|
|
|
|
return (
|
|
<span className="my-5 flex flex-col items-center">
|
|
{src.startsWith('data:') ? (
|
|
<img src={src} alt={alt} />
|
|
) : (
|
|
<NextImage width={width} height={height} alt={alt} src={src} />
|
|
)}
|
|
|
|
{alt && (
|
|
<span className="block font-mono text-xs mt-5 text-center">{alt}</span>
|
|
)}
|
|
</span>
|
|
)
|
|
}
|