diff --git a/e2e-tests/path-prefix/cypress/integration/path-prefix.js b/e2e-tests/path-prefix/cypress/integration/path-prefix.js index 1faca07f93631..b0ae7ef7f11ac 100644 --- a/e2e-tests/path-prefix/cypress/integration/path-prefix.js +++ b/e2e-tests/path-prefix/cypress/integration/path-prefix.js @@ -11,6 +11,26 @@ describe(`Production pathPrefix`, () => { cy.location(`pathname`).should(`eq`, withTrailingSlash(pathPrefix)) }) + it(`renders static image`, () => { + cy.getTestElement(`static-image`) + .should(`have.attr`, `srcset`) + .and(srcset => { + srcset.split(/\s*,\s*/).forEach(part => { + expect(part).to.contain(`/blog`) + }) + }) + }) + + it(`renders dynamic image`, () => { + cy.getTestElement(`gatsby-image`) + .should(`have.attr`, `srcset`) + .and(srcset => { + srcset.split(/\s*,\s*/).forEach(part => { + expect(part).to.contain(`/blog`) + }) + }) + }) + describe(`navigation`, () => { it(`prefixes link with /blog`, () => { cy.getTestElement(`page-2-link`) diff --git a/e2e-tests/path-prefix/gatsby-config.js b/e2e-tests/path-prefix/gatsby-config.js index 2c39c2ed24e78..9ac33e0ecaab7 100644 --- a/e2e-tests/path-prefix/gatsby-config.js +++ b/e2e-tests/path-prefix/gatsby-config.js @@ -21,8 +21,15 @@ module.exports = { icon: `src/images/gatsby-icon.png`, // This path is relative to the root of the site. }, }, + { + resolve: `gatsby-source-filesystem`, + options: { + path: `${__dirname}/src/images/`, + }, + }, `gatsby-plugin-sitemap`, `gatsby-plugin-sharp`, + `gatsby-transformer-sharp`, `gatsby-plugin-image`, { resolve: `gatsby-plugin-feed`, diff --git a/e2e-tests/path-prefix/package.json b/e2e-tests/path-prefix/package.json index 2e2460c057bbd..7887a81f907ef 100644 --- a/e2e-tests/path-prefix/package.json +++ b/e2e-tests/path-prefix/package.json @@ -7,12 +7,14 @@ "cypress": "^3.1.0", "gatsby": "^2.3.34", "gatsby-plugin-feed": "^2.1.2", - "gatsby-plugin-image": "~1.0.0", + "gatsby-plugin-image": "^0.4.0", "gatsby-plugin-manifest": "^2.0.17", "gatsby-plugin-offline": "^2.0.23", "gatsby-plugin-react-helmet": "^3.0.6", "gatsby-plugin-sharp": "^2.0.37", "gatsby-plugin-sitemap": "^2.0.12", + "gatsby-source-filesystem": "^2.8.1", + "gatsby-transformer-sharp": "^2.9.0", "react": "^16.8.0", "react-dom": "^16.8.0", "react-helmet": "^5.2.0" diff --git a/e2e-tests/path-prefix/src/images/citrus-fruits.jpg b/e2e-tests/path-prefix/src/images/citrus-fruits.jpg new file mode 100644 index 0000000000000..8a1a95b1e4555 Binary files /dev/null and b/e2e-tests/path-prefix/src/images/citrus-fruits.jpg differ diff --git a/e2e-tests/path-prefix/src/pages/index.js b/e2e-tests/path-prefix/src/pages/index.js index 70252db3327cc..f5bb4b74b3565 100644 --- a/e2e-tests/path-prefix/src/pages/index.js +++ b/e2e-tests/path-prefix/src/pages/index.js @@ -1,38 +1,57 @@ import * as React from "react" -import { Link, navigate } from "gatsby" -import { StaticImage } from "gatsby-plugin-image" +import { Link, navigate, graphql } from "gatsby" +import { StaticImage, GatsbyImage, getImage } from "gatsby-plugin-image" import Layout from "../components/layout" -const IndexPage = () => ( - -

Hi people

- -

Welcome to your new Gatsby site.

-

Now go build something great.

- - Go to page 2 - - - Go to blogtest - - - - Go to not existing page - - - Go to subdirectory - -
-) +const IndexPage = ({ data }) => { + const image = getImage(data.file) + return ( + +

Hi people

+ + +

Welcome to your new Gatsby site.

+

Now go build something great.

+ + Go to page 2 + + + Go to blogtest + + + + Go to not existing page + + + Go to subdirectory + +
+ ) +} + +export const query = graphql` + query { + file(relativePath: { eq: "citrus-fruits.jpg" }) { + childImageSharp { + gatsbyImageData + } + } + } +` export default IndexPage diff --git a/packages/gatsby-plugin-sharp/src/image-data.ts b/packages/gatsby-plugin-sharp/src/image-data.ts index 7cd95ce80f87b..6a5e3f4e39aa4 100644 --- a/packages/gatsby-plugin-sharp/src/image-data.ts +++ b/packages/gatsby-plugin-sharp/src/image-data.ts @@ -200,7 +200,9 @@ export async function generateImageData({ toFormat: primaryFormat, }) - if (pathPrefix) transform.pathPrefix = pathPrefix + if (pathPrefix) { + transform.pathPrefix = pathPrefix + } return transform }) @@ -256,6 +258,9 @@ export async function generateImageData({ height: Math.round(width / imageSizes.aspectRatio), toFormat: `avif`, }) + if (pathPrefix) { + transform.pathPrefix = pathPrefix + } return transform }) @@ -285,6 +290,9 @@ export async function generateImageData({ height: Math.round(width / imageSizes.aspectRatio), toFormat: `webp`, }) + if (pathPrefix) { + transform.pathPrefix = pathPrefix + } return transform }) diff --git a/packages/gatsby-transformer-sharp/src/customize-schema.js b/packages/gatsby-transformer-sharp/src/customize-schema.js index 583d6bdbd8cab..e4abfc65ff1c4 100644 --- a/packages/gatsby-transformer-sharp/src/customize-schema.js +++ b/packages/gatsby-transformer-sharp/src/customize-schema.js @@ -510,7 +510,6 @@ const imageNodeType = ({ }, resolve: async (image, fieldArgs, context) => { const file = getNodeAndSavePathDependency(image.parent, context.path) - const args = { ...fieldArgs, pathPrefix } if (!generateImageData) { reporter.warn(`Please upgrade gatsby-plugin-sharp`) @@ -525,7 +524,8 @@ const imageNodeType = ({ } const imageData = await generateImageData({ file, - args, + args: fieldArgs, + pathPrefix, reporter, cache, })