Skip to content

Commit 85b1319

Browse files
Andrey PetrovAndreiPiatrou
Andrey Petrov
andauthoredSep 8, 2022
fix(gatsby-link): Correct handling of trailingSlash & pathPrefix (#36542)
Co-authored-by: AndreiPiatrou <19graff91@gmail.com>
1 parent ba43263 commit 85b1319

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed
 

‎packages/gatsby-link/src/__tests__/rewrite-link-path.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import { rewriteLinkPath } from "../rewrite-link-path"
22

33
beforeEach(() => {
44
global.__TRAILING_SLASH__ = ``
5+
global.__PATH_PREFIX__ = undefined
56
})
67

7-
const getRewriteLinkPath = (option = `legacy`) => {
8+
const getRewriteLinkPath = (option = `legacy`, pathPrefix = undefined) => {
89
global.__TRAILING_SLASH__ = option
10+
global.__PATH_PREFIX__ = pathPrefix
911
return rewriteLinkPath
1012
}
1113

@@ -20,6 +22,7 @@ describe(`rewriteLinkPath`, () => {
2022
expect(getRewriteLinkPath()(`/path?query_param=hello#anchor`, `/`)).toBe(
2123
`/path?query_param=hello#anchor`
2224
)
25+
expect(getRewriteLinkPath(`legacy`, `/prefix`)(`/`, `/`)).toBe(`/prefix/`)
2326
})
2427
it(`handles always option`, () => {
2528
expect(getRewriteLinkPath(`always`)(`/path`, `/`)).toBe(`/path/`)
@@ -31,6 +34,7 @@ describe(`rewriteLinkPath`, () => {
3134
expect(
3235
getRewriteLinkPath(`always`)(`/path?query_param=hello#anchor`, `/`)
3336
).toBe(`/path/?query_param=hello#anchor`)
37+
expect(getRewriteLinkPath(`always`, `/prefix`)(`/`, `/`)).toBe(`/prefix/`)
3438
})
3539
it(`handles never option`, () => {
3640
expect(getRewriteLinkPath(`never`)(`/path`, `/`)).toBe(`/path`)
@@ -42,6 +46,7 @@ describe(`rewriteLinkPath`, () => {
4246
expect(
4347
getRewriteLinkPath(`never`)(`/path/?query_param=hello#anchor`, `/`)
4448
).toBe(`/path?query_param=hello#anchor`)
49+
expect(getRewriteLinkPath(`never`, `/prefix`)(`/`, `/`)).toBe(`/prefix`)
4550
})
4651
it(`handles ignore option`, () => {
4752
expect(getRewriteLinkPath(`ignore`)(`/path`, `/`)).toBe(`/path`)
@@ -53,5 +58,6 @@ describe(`rewriteLinkPath`, () => {
5358
expect(
5459
getRewriteLinkPath(`ignore`)(`/path?query_param=hello#anchor`, `/`)
5560
).toBe(`/path?query_param=hello#anchor`)
61+
expect(getRewriteLinkPath(`ignore`, `/prefix`)(`/`, `/`)).toBe(`/prefix/`)
5662
})
5763
})

‎packages/gatsby-link/src/rewrite-link-path.js

+15-12
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ function absolutify(path, current) {
2626
return absolutePath
2727
}
2828

29+
function applyPrefix(path) {
30+
const prefixed = withPrefix(path)
31+
const option = getGlobalTrailingSlash()
32+
33+
if (option === `always` || option === `never`) {
34+
const { pathname, search, hash } = parsePath(prefixed)
35+
const output = applyTrailingSlashOption(pathname, option)
36+
37+
return `${output}${search}${hash}`
38+
}
39+
40+
return prefixed
41+
}
42+
2943
export const rewriteLinkPath = (path, relativeTo) => {
3044
if (typeof path === `number`) {
3145
return path
@@ -34,16 +48,5 @@ export const rewriteLinkPath = (path, relativeTo) => {
3448
return path
3549
}
3650

37-
const { pathname, search, hash } = parsePath(path)
38-
const option = getGlobalTrailingSlash()
39-
let adjustedPath = path
40-
41-
if (option === `always` || option === `never`) {
42-
const output = applyTrailingSlashOption(pathname, option)
43-
adjustedPath = `${output}${search}${hash}`
44-
}
45-
46-
return isAbsolutePath(adjustedPath)
47-
? withPrefix(adjustedPath)
48-
: absolutify(adjustedPath, relativeTo)
51+
return isAbsolutePath(path) ? applyPrefix(path) : absolutify(path, relativeTo)
4952
}

0 commit comments

Comments
 (0)
Please sign in to comment.