Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle blob urls in image component #27975

Merged
merged 6 commits into from Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/next/client/image.tsx
Expand Up @@ -348,6 +348,10 @@ export default function Image({
unoptimized = true
isLazy = false
}
if (src.startsWith('blob:')) {
stefanprobst marked this conversation as resolved.
Show resolved Hide resolved
unoptimized = true
isLazy = false
}
if (typeof window !== 'undefined' && loadedImageURLs.has(src)) {
isLazy = false
}
Expand Down
26 changes: 26 additions & 0 deletions test/integration/image-component/basic/pages/blob.js
@@ -0,0 +1,26 @@
import React, { useEffect, useState } from 'react'
stefanprobst marked this conversation as resolved.
Show resolved Hide resolved
import Image from 'next/image'

const Page = () => {
const [src, setSrc] = useState()

useEffect(() => {
fetch('/test.jpg')
.then((res) => {
return res.blob()
})
.then((blob) => {
const url = URL.createObjectURL(blob)
setSrc(url)
})
}, [])

return (
<div>
<p>Blob URL</p>
{src ? <Image id="blob-image" src={src} width="10" height="10" /> : null}
</div>
)
}

export default Page
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions test/integration/image-component/basic/test/index.test.js
Expand Up @@ -406,4 +406,20 @@ describe('Image Component Tests', () => {
)
})
})
describe('Blob URL Tests', () => {
beforeAll(async () => {
browser = await webdriver(appPort, '/blob')
})
afterAll(async () => {
browser = null
})
it('should render image with blob src', async () => {
expect(
await browser.elementById('blob-image').getAttribute('src')
).toMatch(/^blob:/)
expect(
await browser.elementById('blob-image').getAttribute('srcset')
).toBeFalsy()
})
})
})