-
Notifications
You must be signed in to change notification settings - Fork 10.3k
feat: image and file cdn url generator adapter implementation #38685
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
Merged
Merged
Changes from 19 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
3ff1518
alternate image url construction
kathmbeck 8c4b42a
Merge branch 'master' into alternate-image-url
kathmbeck aee50e6
try using image cdn in e2e site
pieh 6b3bdbe
Update netlify.toml
kathmbeck 3f8efd2
have separate check for dispatching image and file service
pieh 40d2add
fix tests?
pieh 9ebede5
try to use images from deploy (so we can avoid using ones hosted exte…
pieh 599f34d
replicate prod-runtime imagecdn tests in adapters
pieh 011228a
fix import
pieh d9dea3f
adjusting remote-file tests
pieh f89b551
adjusting remote-file tests 2
pieh 57fdd3a
cleanup/test
kathmbeck 1a8df77
assert naturalWidth/height in image-cdn tests (both adapters and prod…
pieh 36b918a
remove unused
pieh 5b81ecd
don't use path prefix for alternate image cdn url
pieh d324338
Merge remote-tracking branch 'origin/master' into alternate-image-url
pieh 0975496
_gatsby/file is prefixed
pieh d31412a
feat: move custom image cdn url generator implementation to adapter (…
pieh ed5f3bf
Merge branch 'master' into alternate-image-url
kathmbeck 370d2b4
use position/cover
kathmbeck 3527298
update comment
kathmbeck a175d3b
update docs
kathmbeck 4ba752e
Merge remote-tracking branch 'origin/master' into alternate-image-url
pieh 4514117
chore: types/jsdocs shuffle
pieh c7b3da3
apply suggestion from https://github.com/gatsbyjs/gatsby/pull/38685\#…
pieh 78b108c
remove docs from feature branch
pieh d82f88f
feat: provide custom FILE_CDN url generator from adapter (#38735)
pieh 53ce56d
add note that generated urls ideally are relative, but can be absolut…
pieh 7de5336
feat: allow adding remote file allowed url patterns (#38719)
pieh 8f1bc5d
chore: update adapter README about imageCDN
pieh 434a946
Merge remote-tracking branch 'origin/master' into alternate-image-url
pieh 1bb2e01
use correct remote_images for adapters e2e site
pieh 1c79840
Merge remote-tracking branch 'origin/master' into alternate-image-url
pieh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,226 @@ | ||
| Cypress.on("uncaught:exception", err => { | ||
| if ( | ||
| (err.message.includes("Minified React error #418") || | ||
| err.message.includes("Minified React error #423") || | ||
| err.message.includes("Minified React error #425")) && | ||
| Cypress.env(`TEST_PLUGIN_OFFLINE`) | ||
| ) { | ||
| return false | ||
| } | ||
| }) | ||
|
|
||
| const PATH_PREFIX = Cypress.env(`PATH_PREFIX`) || `` | ||
|
|
||
| // there are multiple scenarios we want to test and ensure that custom image cdn url is used: | ||
| // - child build process (SSG, Page Query) | ||
| // - main build process (SSG, Page Context) | ||
| // - query engine (SSR, Page Query) | ||
| const configs = [ | ||
| { | ||
| title: `remote-file (SSG, Page Query)`, | ||
| pagePath: `/routes/remote-file/`, | ||
| fileCDN: true, | ||
| placeholders: true, | ||
| }, | ||
| { | ||
| title: `remote-file (SSG, Page Context)`, | ||
| pagePath: `/routes/remote-file-data-from-context/`, | ||
| fileCDN: true, | ||
| placeholders: true, | ||
| }, | ||
| { | ||
| title: `remote-file (SSR, Page Query)`, | ||
| pagePath: `/routes/ssr/remote-file/`, | ||
| fileCDN: false, | ||
| placeholders: false, | ||
| }, | ||
| ] | ||
|
|
||
| for (const config of configs) { | ||
| describe( | ||
| config.title, | ||
| { | ||
| retries: { | ||
| runMode: 4, | ||
| }, | ||
| }, | ||
| () => { | ||
| beforeEach(() => { | ||
| cy.visit(config.pagePath).waitForRouteChange() | ||
|
|
||
| // trigger intersection observer | ||
| cy.scrollTo("top") | ||
| cy.wait(200) | ||
| cy.scrollTo("bottom", { | ||
| duration: 600, | ||
| }) | ||
| cy.wait(600) | ||
| }) | ||
|
|
||
| async function testImages(images, expectations) { | ||
| for (let i = 0; i < images.length; i++) { | ||
| const expectation = expectations[i] | ||
|
|
||
| const url = images[i].currentSrc | ||
|
|
||
| const { href, origin } = new URL(url) | ||
| const urlWithoutOrigin = href.replace(origin, ``) | ||
|
|
||
| // using Netlify Image CDN | ||
| expect(urlWithoutOrigin).to.match(/^\/.netlify\/images/) | ||
|
|
||
| const res = await fetch(url, { | ||
| method: "HEAD", | ||
| }) | ||
| expect(res.ok).to.be.true | ||
|
|
||
| const expectedNaturalWidth = | ||
| expectation.naturalWidth ?? expectation.width | ||
| const expectedNaturalHeight = | ||
| expectation.naturalHeight ?? expectation.height | ||
|
|
||
| if (expectation.width) { | ||
| expect( | ||
| Math.ceil(images[i].getBoundingClientRect().width) | ||
| ).to.be.equal(expectation.width) | ||
| } | ||
| if (expectation.height) { | ||
| expect( | ||
| Math.ceil(images[i].getBoundingClientRect().height) | ||
| ).to.be.equal(expectation.height) | ||
| } | ||
| if (expectedNaturalWidth) { | ||
| expect(Math.ceil(images[i].naturalWidth)).to.be.equal( | ||
| expectedNaturalWidth | ||
| ) | ||
| } | ||
| if (expectedNaturalHeight) { | ||
| expect(Math.ceil(images[i].naturalHeight)).to.be.equal( | ||
| expectedNaturalHeight | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| it(`should render correct dimensions`, () => { | ||
| if (config.fileCDN) { | ||
| cy.get('[data-testid="public"]').then(async $urls => { | ||
| const urls = Array.from( | ||
| $urls.map((_, $url) => $url.getAttribute("href")) | ||
| ) | ||
|
|
||
| for (const url of urls) { | ||
| // using OSS implementation for publicURL for now | ||
| expect(url).to.match(new RegExp(`^${PATH_PREFIX}/_gatsby/file`)) | ||
| const res = await fetch(url, { | ||
| method: "HEAD", | ||
| }) | ||
| expect(res.ok).to.be.true | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| cy.get(".resize").then({ timeout: 60000 }, async $imgs => { | ||
| await testImages(Array.from($imgs), [ | ||
| { | ||
| width: 100, | ||
| height: 133, | ||
| }, | ||
| { | ||
| width: 100, | ||
| height: 160, | ||
| }, | ||
| { | ||
| width: 100, | ||
| height: 67, | ||
| }, | ||
| ]) | ||
| }) | ||
|
|
||
| cy.get(".fixed img:not([aria-hidden=true])").then( | ||
| { timeout: 60000 }, | ||
| async $imgs => { | ||
| await testImages(Array.from($imgs), [ | ||
| { | ||
| width: 100, | ||
| height: 133, | ||
| }, | ||
| { | ||
| width: 100, | ||
| height: 160, | ||
| }, | ||
| { | ||
| width: 100, | ||
| height: 67, | ||
| }, | ||
| ]) | ||
| } | ||
| ) | ||
|
|
||
| cy.get(".constrained img:not([aria-hidden=true])").then( | ||
| { timeout: 60000 }, | ||
| async $imgs => { | ||
| await testImages(Array.from($imgs), [ | ||
| { | ||
| width: 300, | ||
| height: 400, | ||
| }, | ||
| { | ||
| width: 300, | ||
| height: 481, | ||
| }, | ||
| { | ||
| width: 300, | ||
| height: 200, | ||
| }, | ||
| ]) | ||
| } | ||
| ) | ||
|
|
||
| cy.get(".full img:not([aria-hidden=true])").then( | ||
| { timeout: 60000 }, | ||
| async $imgs => { | ||
| await testImages(Array.from($imgs), [ | ||
| { | ||
| naturalHeight: 1333, | ||
| }, | ||
| { | ||
| naturalHeight: 1603, | ||
| }, | ||
| { | ||
| naturalHeight: 666, | ||
| }, | ||
| ]) | ||
| } | ||
| ) | ||
| }) | ||
|
|
||
| it(`should render a placeholder`, () => { | ||
| if (config.placeholders) { | ||
| cy.get(".fixed [data-placeholder-image]") | ||
| .first() | ||
| .should("have.css", "background-color", "rgb(232, 184, 8)") | ||
| cy.get(".constrained [data-placeholder-image]") | ||
| .first() | ||
| .should($el => { | ||
| expect($el.prop("tagName")).to.be.equal("IMG") | ||
| expect($el.prop("src")).to.contain("data:image/jpg;base64") | ||
| }) | ||
| cy.get(".constrained_traced [data-placeholder-image]") | ||
| .first() | ||
| .should($el => { | ||
| // traced falls back to DOMINANT_COLOR | ||
| expect($el.prop("tagName")).to.be.equal("DIV") | ||
| expect($el).to.be.empty | ||
| }) | ||
| } | ||
| cy.get(".full [data-placeholder-image]") | ||
| .first() | ||
| .should($el => { | ||
| expect($el.prop("tagName")).to.be.equal("DIV") | ||
| expect($el).to.be.empty | ||
| }) | ||
| }) | ||
| } | ||
| ) | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,3 +1,9 @@ | ||
| [build] | ||
| command = "npm run build" | ||
| publish = "public/" | ||
| publish = "public/" | ||
|
|
||
| [build.environment] | ||
| NETLIFY_IMAGE_CDN = "true" | ||
|
|
||
| [images] | ||
| remote_images = ["https://images.unsplash.com/*"] | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.