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

Add delay to placeholder removal #25916

Merged
merged 3 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/next/client/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ function defaultImageLoader(loaderProps: ImageLoaderProps) {

// See https://stackoverflow.com/q/39777833/266535 for why we use this ref
// handler instead of the img's onLoad attribute.
// 1500ms delay in removing placeholder is to prevent flash of white between
// image load and image render.
function removePlaceholder(
element: HTMLImageElement | null,
placeholder: PlaceholderValue
Expand All @@ -270,11 +272,15 @@ function removePlaceholder(
// If the real image fails to load, this will still remove the placeholder.
// This is the desired behavior for now, and will be revisited when error
// handling is worked on for the image component itself.
element.style.backgroundImage = 'none'
setTimeout(() => {
element.style.backgroundImage = 'none'
}, 1500)
} else {
element.onload = () => {
if (!element.src.startsWith('data:')) {
element.style.backgroundImage = 'none'
setTimeout(() => {
element.style.backgroundImage = 'none'
}, 1500)
}
}
}
Expand Down
35 changes: 19 additions & 16 deletions test/integration/image-component/default/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import webdriver from 'next-webdriver'
import { join } from 'path'

jest.setTimeout(1000 * 60)
jest.setTimeout(1000 * 80)

const appDir = join(__dirname, '../')

Expand Down Expand Up @@ -607,14 +607,15 @@ describe('Image Component Tests', () => {
let browser
try {
browser = await webdriver(appPort, '/blurry-placeholder')

expect(
await getComputedStyle(
browser,
'blurry-placeholder',
'background-image'
)
).toBe('none')
await check(
async () =>
await getComputedStyle(
browser,
'blurry-placeholder',
'background-image'
),
'none'
)
expect(
await getComputedStyle(
browser,
Expand All @@ -627,13 +628,15 @@ describe('Image Component Tests', () => {

await browser.eval('document.getElementById("spacer").remove()')

expect(
await getComputedStyle(
browser,
'blurry-placeholder-with-lazy',
'background-image'
)
).toBe('none')
await check(
async () =>
await getComputedStyle(
browser,
'blurry-placeholder-with-lazy',
'background-image'
),
'none'
)
} finally {
if (browser) {
await browser.close()
Expand Down