Skip to content

Commit c762050

Browse files
GatsbyJS BotLB
GatsbyJS Bot
and
LB
authored
fix(gatsby-plugin-sharp): PathPrefix isn't being passed/set for GatsbyImage (#28845) (#28991)
* fix for path-prefix in GatsbyImage * add e2e test for path prefix cases * update tests * forgot to save * refactor * add curlys (cherry picked from commit 5874414) Co-authored-by: LB <[email protected]>
1 parent 1617c6c commit c762050

File tree

7 files changed

+92
-36
lines changed

7 files changed

+92
-36
lines changed

e2e-tests/path-prefix/cypress/integration/path-prefix.js

+20
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,26 @@ describe(`Production pathPrefix`, () => {
1111
cy.location(`pathname`).should(`eq`, withTrailingSlash(pathPrefix))
1212
})
1313

14+
it(`renders static image`, () => {
15+
cy.getTestElement(`static-image`)
16+
.should(`have.attr`, `srcset`)
17+
.and(srcset => {
18+
srcset.split(/\s*,\s*/).forEach(part => {
19+
expect(part).to.contain(`/blog`)
20+
})
21+
})
22+
})
23+
24+
it(`renders dynamic image`, () => {
25+
cy.getTestElement(`gatsby-image`)
26+
.should(`have.attr`, `srcset`)
27+
.and(srcset => {
28+
srcset.split(/\s*,\s*/).forEach(part => {
29+
expect(part).to.contain(`/blog`)
30+
})
31+
})
32+
})
33+
1434
describe(`navigation`, () => {
1535
it(`prefixes link with /blog`, () => {
1636
cy.getTestElement(`page-2-link`)

e2e-tests/path-prefix/gatsby-config.js

+7
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,15 @@ module.exports = {
2121
icon: `src/images/gatsby-icon.png`, // This path is relative to the root of the site.
2222
},
2323
},
24+
{
25+
resolve: `gatsby-source-filesystem`,
26+
options: {
27+
path: `${__dirname}/src/images/`,
28+
},
29+
},
2430
`gatsby-plugin-sitemap`,
2531
`gatsby-plugin-sharp`,
32+
`gatsby-transformer-sharp`,
2633
`gatsby-plugin-image`,
2734
{
2835
resolve: `gatsby-plugin-feed`,

e2e-tests/path-prefix/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
"cypress": "^3.1.0",
88
"gatsby": "^2.3.34",
99
"gatsby-plugin-feed": "^2.1.2",
10-
"gatsby-plugin-image": "~1.0.0",
10+
"gatsby-plugin-image": "^0.4.0",
1111
"gatsby-plugin-manifest": "^2.0.17",
1212
"gatsby-plugin-offline": "^2.0.23",
1313
"gatsby-plugin-react-helmet": "^3.0.6",
1414
"gatsby-plugin-sharp": "^2.0.37",
1515
"gatsby-plugin-sitemap": "^2.0.12",
16+
"gatsby-source-filesystem": "^2.8.1",
17+
"gatsby-transformer-sharp": "^2.9.0",
1618
"react": "^16.8.0",
1719
"react-dom": "^16.8.0",
1820
"react-helmet": "^5.2.0"
Loading
+51-32
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,57 @@
11
import * as React from "react"
2-
import { Link, navigate } from "gatsby"
3-
import { StaticImage } from "gatsby-plugin-image"
2+
import { Link, navigate, graphql } from "gatsby"
3+
import { StaticImage, GatsbyImage, getImage } from "gatsby-plugin-image"
44

55
import Layout from "../components/layout"
66

7-
const IndexPage = () => (
8-
<Layout>
9-
<h1>Hi people</h1>
10-
<StaticImage
11-
src="../images/gatsby-icon.png"
12-
alt="Gatsby icon"
13-
layout="fixed"
14-
/>
15-
<p>Welcome to your new Gatsby site.</p>
16-
<p>Now go build something great.</p>
17-
<Link data-testid="page-2-link" to="/page-2/">
18-
Go to page 2
19-
</Link>
20-
<Link data-testid="page-blogtest-link" to="/blogtest/">
21-
Go to blogtest
22-
</Link>
23-
<button
24-
data-testid="page-2-button-link"
25-
onClick={() => navigate(`/page-2/`)}
26-
>
27-
Go to page 2 with navigate()
28-
</button>
29-
<Link data-testid="404-link" to="/not-existing-page">
30-
Go to not existing page
31-
</Link>
32-
<Link data-testid="subdir-link" to="subdirectory/page-1">
33-
Go to subdirectory
34-
</Link>
35-
</Layout>
36-
)
7+
const IndexPage = ({ data }) => {
8+
const image = getImage(data.file)
9+
return (
10+
<Layout>
11+
<h1>Hi people</h1>
12+
<StaticImage
13+
src="../images/gatsby-icon.png"
14+
alt="Gatsby icon"
15+
layout="fixed"
16+
data-testid="static-image"
17+
/>
18+
<GatsbyImage
19+
image={image}
20+
alt="Citrus Fruits"
21+
data-testid="gatsby-image"
22+
/>
23+
<p>Welcome to your new Gatsby site.</p>
24+
<p>Now go build something great.</p>
25+
<Link data-testid="page-2-link" to="/page-2/">
26+
Go to page 2
27+
</Link>
28+
<Link data-testid="page-blogtest-link" to="/blogtest/">
29+
Go to blogtest
30+
</Link>
31+
<button
32+
data-testid="page-2-button-link"
33+
onClick={() => navigate(`/page-2/`)}
34+
>
35+
Go to page 2 with navigate()
36+
</button>
37+
<Link data-testid="404-link" to="/not-existing-page">
38+
Go to not existing page
39+
</Link>
40+
<Link data-testid="subdir-link" to="subdirectory/page-1">
41+
Go to subdirectory
42+
</Link>
43+
</Layout>
44+
)
45+
}
46+
47+
export const query = graphql`
48+
query {
49+
file(relativePath: { eq: "citrus-fruits.jpg" }) {
50+
childImageSharp {
51+
gatsbyImageData
52+
}
53+
}
54+
}
55+
`
3756

3857
export default IndexPage

packages/gatsby-plugin-sharp/src/image-data.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ export async function generateImageData({
200200
toFormat: primaryFormat,
201201
})
202202

203-
if (pathPrefix) transform.pathPrefix = pathPrefix
203+
if (pathPrefix) {
204+
transform.pathPrefix = pathPrefix
205+
}
204206
return transform
205207
})
206208

@@ -256,6 +258,9 @@ export async function generateImageData({
256258
height: Math.round(width / imageSizes.aspectRatio),
257259
toFormat: `avif`,
258260
})
261+
if (pathPrefix) {
262+
transform.pathPrefix = pathPrefix
263+
}
259264
return transform
260265
})
261266

@@ -285,6 +290,9 @@ export async function generateImageData({
285290
height: Math.round(width / imageSizes.aspectRatio),
286291
toFormat: `webp`,
287292
})
293+
if (pathPrefix) {
294+
transform.pathPrefix = pathPrefix
295+
}
288296
return transform
289297
})
290298

packages/gatsby-transformer-sharp/src/customize-schema.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,6 @@ const imageNodeType = ({
510510
},
511511
resolve: async (image, fieldArgs, context) => {
512512
const file = getNodeAndSavePathDependency(image.parent, context.path)
513-
const args = { ...fieldArgs, pathPrefix }
514513

515514
if (!generateImageData) {
516515
reporter.warn(`Please upgrade gatsby-plugin-sharp`)
@@ -525,7 +524,8 @@ const imageNodeType = ({
525524
}
526525
const imageData = await generateImageData({
527526
file,
528-
args,
527+
args: fieldArgs,
528+
pathPrefix,
529529
reporter,
530530
cache,
531531
})

0 commit comments

Comments
 (0)