-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gatsby-plugin-image): fix image flickers (#35226)
- Loading branch information
Showing
29 changed files
with
808 additions
and
754 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
190 changes: 190 additions & 0 deletions
190
e2e-tests/production-runtime/cypress/integration/gatsby-plugin-image.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
const MutationObserver = | ||
window.MutationObserver || window.WebKitMutationObserver | ||
|
||
// helper function to observe DOM changes | ||
function observeDOM(obj, options, callback) { | ||
if (!obj || obj.nodeType !== window.Node.ELEMENT_NODE) { | ||
throw new Error("can not observe this element") | ||
} | ||
|
||
const obs = new MutationObserver(callback) | ||
|
||
obs.observe(obj, { childList: true, subtree: true, ...options }) | ||
|
||
return () => { | ||
obs.disconnect() | ||
} | ||
} | ||
|
||
describe(`gatsby-plugin-image`, () => { | ||
it(`doesn't recycle image nodes when not necessary`, () => { | ||
const mutationStub = cy.stub() | ||
let cleanup | ||
|
||
cy.visit(`/gatsby-plugin-image-page-1/`) | ||
|
||
// wait for the image to load | ||
cy.get("[data-main-image]").should("be.visible") | ||
|
||
// start watching mutations in the image-wrapper | ||
cy.get("body").then($element => { | ||
cleanup = observeDOM($element[0], {}, mutations => { | ||
const normalizedMutations = [] | ||
mutations.forEach(mutation => { | ||
if ( | ||
mutation.type === "childList" && | ||
mutation.target.classList.contains("gatsby-image-wrapper") | ||
) { | ||
normalizedMutations.push({ | ||
type: mutation.type, | ||
addedNodes: !!mutation.addedNodes.length, | ||
removedNodes: !!mutation.removedNodes.length, | ||
}) | ||
} | ||
}) | ||
|
||
if (normalizedMutations.length) { | ||
mutationStub(normalizedMutations) | ||
} | ||
}) | ||
}) | ||
|
||
cy.window() | ||
.then(win => win.___navigate(`/gatsby-plugin-image-page-2/`)) | ||
.waitForRouteChange() | ||
|
||
// wait for the image to load | ||
cy.get("[data-main-image]").should("be.visible") | ||
cy.wait(500) | ||
|
||
cy.then(() => { | ||
cleanup() | ||
expect(mutationStub).to.be.calledWith([ | ||
{ | ||
type: "childList", | ||
addedNodes: true, | ||
removedNodes: false, | ||
}, | ||
]) | ||
}) | ||
}) | ||
|
||
it(`rerenders when image src changed`, () => { | ||
const mutationStub = cy.stub() | ||
let cleanup | ||
|
||
cy.visit(`/gatsby-plugin-image-page-1/`) | ||
|
||
// wait for the image to load | ||
cy.get("[data-main-image]").should("be.visible") | ||
|
||
// start watching mutations in the image-wrapper | ||
cy.get("#image-wrapper").then($element => { | ||
cleanup = observeDOM($element[0], {}, mutations => { | ||
const normalizedMutations = [] | ||
mutationStub( | ||
mutations.map(mutation => { | ||
normalizedMutations.push({ | ||
addedNodes: mutation.addedNodes, | ||
removedNodes: mutation.removedNodes, | ||
}) | ||
|
||
return { | ||
type: mutation.type, | ||
addedNodes: !!mutation.addedNodes.length, | ||
removedNodes: !!mutation.removedNodes.length, | ||
} | ||
}) | ||
) | ||
|
||
Cypress.log({ | ||
name: "MutationObserver", | ||
message: `${normalizedMutations.length} mutations`, | ||
consoleProps: () => { | ||
return { | ||
mutations: normalizedMutations, | ||
} | ||
}, | ||
}) | ||
}) | ||
}) | ||
|
||
cy.get("#click").click() | ||
|
||
cy.wait(500) | ||
|
||
cy.get("[data-main-image]", { | ||
timeout: 5000, | ||
}).should("be.visible") | ||
|
||
cy.then(() => { | ||
cleanup() | ||
expect(mutationStub).to.be.calledOnce | ||
expect(mutationStub).to.be.calledWith([ | ||
{ | ||
type: "childList", | ||
addedNodes: true, | ||
removedNodes: true, | ||
}, | ||
]) | ||
}) | ||
}) | ||
|
||
it(`rerenders when background color changes`, () => { | ||
const mutationStub = cy.stub() | ||
let cleanup | ||
|
||
cy.visit(`/gatsby-plugin-image-page-2/`) | ||
|
||
// wait for the image to load | ||
cy.get("[data-main-image]").should("be.visible") | ||
|
||
// start watching mutations in the image-wrapper | ||
cy.get("#image-wrapper").then($element => { | ||
cleanup = observeDOM( | ||
$element[0], | ||
{ | ||
attributes: true, | ||
attributeFilter: ["style"], | ||
}, | ||
mutations => { | ||
mutationStub( | ||
mutations.map(mutation => ({ | ||
type: mutation.type, | ||
attributeName: mutation.attributeName, | ||
})) | ||
) | ||
} | ||
) | ||
}) | ||
|
||
cy.get("[data-gatsby-image-wrapper]").should( | ||
"have.css", | ||
"background-color", | ||
"rgb(102, 51, 153)" | ||
) | ||
|
||
cy.get("#click").click() | ||
|
||
cy.wait(500) | ||
|
||
// wait for the image to load | ||
cy.get("[data-main-image]").should("be.visible") | ||
|
||
cy.get("[data-gatsby-image-wrapper]").should( | ||
"have.css", | ||
"background-color", | ||
"rgb(255, 0, 0)" | ||
) | ||
cy.then(() => { | ||
cleanup() | ||
expect(mutationStub).to.be.calledOnce | ||
expect(mutationStub).to.be.calledWith([ | ||
{ | ||
type: "attributes", | ||
attributeName: "style", | ||
}, | ||
]) | ||
}) | ||
}) | ||
}) |
4 changes: 1 addition & 3 deletions
4
e2e-tests/production-runtime/cypress/integration/lifecycle-methods.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
e2e-tests/production-runtime/src/pages/gatsby-plugin-image-page-1.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { graphql } from "gatsby" | ||
import React from "react" | ||
|
||
import { GatsbyImage } from "gatsby-plugin-image" | ||
|
||
const PluginImage = ({ data }) => { | ||
const images = data.allMyRemoteFile.nodes | ||
const [nodeIndex, updateNode] = React.useState(0) | ||
|
||
return ( | ||
<div> | ||
<h1>Image Testing</h1> | ||
|
||
<div id="image-wrapper"> | ||
<GatsbyImage | ||
image={data.allMyRemoteFile.nodes[nodeIndex].fixed} | ||
alt="fixed" | ||
backgroundColor="rebeccapurple" | ||
/> | ||
</div> | ||
|
||
<button | ||
id="click" | ||
onClick={() => | ||
updateNode(nodeIndex === images.length - 1 ? 0 : nodeIndex + 1) | ||
} | ||
> | ||
Change Image | ||
</button> | ||
</div> | ||
) | ||
} | ||
|
||
export const pageQuery = graphql` | ||
{ | ||
allMyRemoteFile { | ||
nodes { | ||
id | ||
fixed: gatsbyImage(layout: FIXED, width: 100, placeholder: NONE) | ||
} | ||
} | ||
} | ||
` | ||
|
||
export default PluginImage |
44 changes: 44 additions & 0 deletions
44
e2e-tests/production-runtime/src/pages/gatsby-plugin-image-page-2.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { graphql } from "gatsby" | ||
import React from "react" | ||
|
||
import { GatsbyImage } from "gatsby-plugin-image" | ||
|
||
const PluginImage = ({ data }) => { | ||
const [bg, updateBg] = React.useState("rebeccapurple") | ||
|
||
return ( | ||
<div> | ||
<h1>Image Testing</h1> | ||
|
||
<div id="image-wrapper"> | ||
<GatsbyImage | ||
image={data.allMyRemoteFile.nodes[0].fixed} | ||
alt="fixed" | ||
backgroundColor={bg} | ||
/> | ||
</div> | ||
|
||
<button | ||
id="click" | ||
onClick={() => | ||
updateBg(bg === "rebeccapurple" ? "red" : "rebeccapurple") | ||
} | ||
> | ||
Change Image | ||
</button> | ||
</div> | ||
) | ||
} | ||
|
||
export const pageQuery = graphql` | ||
{ | ||
allMyRemoteFile { | ||
nodes { | ||
id | ||
fixed: gatsbyImage(layout: FIXED, width: 100, placeholder: NONE) | ||
} | ||
} | ||
} | ||
` | ||
|
||
export default PluginImage |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.