Skip to content

Commit e2084f6

Browse files
gatsbybottyhopp
andauthored
fix(gatsby): Restore asset, path prefix for file-loader handled files (#37429) (#37430)
Co-authored-by: Ty Hopp <tyhopp@users.noreply.github.com>
1 parent c7b5e7c commit e2084f6

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,13 @@ describe(`assetPrefix`, () => {
7777
})
7878
})
7979
})
80+
81+
describe(`assetPrefix with assets handled by file-loader`, () => {
82+
beforeEach(() => {
83+
cy.visit(`/file-loader/`).waitForRouteChange()
84+
})
85+
86+
it(`prefixes an asset`, () => {
87+
assetPrefixMatcher(cy.getTestElement(`file-loader-image`), `src`)
88+
})
89+
})

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,15 @@ describe(`Production pathPrefix`, () => {
9898
cy.getTestElement(`server-data`).contains(`foo`)
9999
})
100100
})
101+
102+
describe(`pathPrefix with assets handled by file-loader`, () => {
103+
beforeEach(() => {
104+
cy.visit(`/file-loader/`).waitForRouteChange()
105+
})
106+
107+
it(`prefixes an asset`, () => {
108+
cy.getTestElement(`file-loader-image`)
109+
.invoke(`attr`, `src`)
110+
.should(`include`, withTrailingSlash(pathPrefix))
111+
})
112+
})

e2e-tests/path-prefix/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
],
2424
"license": "MIT",
2525
"scripts": {
26+
"clean": "gatsby clean",
2627
"prebuild": "del-cli -f assets && make-dir assets/blog",
2728
"build": "cross-env CYPRESS_SUPPORT=y gatsby build --prefix-paths",
2829
"postbuild": "cpy . '../assets/blog' --cwd=./public",
2930
"develop": "gatsby develop",
3031
"format": "prettier --write '**/*.js'",
3132
"test": "npm run build && npm run start-server-and-test",
3233
"start-server-and-test": "start-server-and-test serve \"http://localhost:9000/blog/|http://localhost:9001/blog/\" cy:run",
34+
"start-server-and-test:locally": "start-server-and-test serve \"http://localhost:9000/blog/|http://localhost:9001/blog/\" cy:open",
3335
"serve": "npm-run-all --parallel serve:*",
3436
"serve:site": "gatsby serve --prefix-paths",
3537
"serve:assets": "node scripts/serve.js",
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as React from "react"
2+
3+
// Test files that are handled by file-loader
4+
import logo from "../images/citrus-fruits.jpg"
5+
6+
export default function FileLoaderPage() {
7+
return <img src={logo} alt="Citrus fruits" data-testid="file-loader-image" />
8+
}

packages/gatsby/src/utils/webpack-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ export const createWebpackUtils = (
220220
ROUTES_DIRECTORY,
221221
`public`
222222
)
223-
fileLoaderCommonOptions.publicPath = `/`
223+
fileLoaderCommonOptions.publicPath = publicPath || `/`
224224
}
225225

226226
const loaders: ILoaderUtils = {

0 commit comments

Comments
 (0)