Skip to content

Commit

Permalink
Handle blob urls in image component (#27975)
Browse files Browse the repository at this point in the history
This PR resurrects #23622 which has not been updated in a while. Makes the `Image` component handle `blob:` object urls.

closes #23622
fixes #19291

credits: @sdn90 

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes
  • Loading branch information
stefanprobst committed Aug 13, 2021
1 parent d14da39 commit daadfd3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/next/client/image.tsx
Expand Up @@ -343,7 +343,7 @@ export default function Image({

let isLazy =
!priority && (loading === 'lazy' || typeof loading === 'undefined')
if (src.startsWith('data:')) {
if (src.startsWith('data:') || src.startsWith('blob:')) {
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
unoptimized = true
isLazy = false
Expand Down
26 changes: 26 additions & 0 deletions test/integration/image-component/default/pages/blob.js
@@ -0,0 +1,26 @@
import React, { useEffect, useState } from 'react'
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
20 changes: 20 additions & 0 deletions test/integration/image-component/default/test/index.test.js
Expand Up @@ -229,6 +229,26 @@ function runTests(mode) {
}
})

it('should work with image with blob src', async () => {
let browser
try {
browser = await webdriver(appPort, '/blob')

await check(
() => browser.eval(`document.getElementById("blob-image").src`),
/^blob:/
)
await check(
() => browser.eval(`document.getElementById("blob-image").srcset`),
''
)
} finally {
if (browser) {
await browser.close()
}
}
})

it('should work when using flexbox', async () => {
let browser
try {
Expand Down

0 comments on commit daadfd3

Please sign in to comment.