Skip to content

Commit ba90289

Browse files
authored
fix(server): fix url join (#166)
1 parent 6b5ba21 commit ba90289

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

packages/server/src/util.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ export const smartRequire = modulePath => {
99
return require(modulePath)
1010
}
1111

12-
export const joinURLPath = (...paths) =>
13-
paths.join('/').replace(/(?<!http(s)?:)(?<=.)\/\//g, '/')
12+
export const joinURLPath = (...paths) => {
13+
const cleanPaths = paths.map(path => path.replace(/\/$/, ''))
14+
return cleanPaths.join('/')
15+
}

packages/server/src/util.test.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,27 @@ describe('util', () => {
66
expect(joinURLPath('public', 'style.css')).toBe('public/style.css')
77
expect(joinURLPath('public/', 'style.css')).toBe('public/style.css')
88
})
9+
10+
it('should join paths starting with "/"', () => {
11+
expect(joinURLPath('/foo', 'style.css')).toBe('/foo/style.css')
12+
expect(joinURLPath('/', 'style.css')).toBe('/style.css')
13+
})
14+
915
it('should join paths with absolute public path', () => {
1016
const publicPath = 'http://localhost:3001/public'
1117

1218
expect(joinURLPath(publicPath, 'style.css')).toBe(
13-
`${publicPath}/style.css`,
19+
`http://localhost:3001/public/style.css`,
1420
)
1521
expect(joinURLPath(`${publicPath}/`, 'style.css')).toBe(
16-
`${publicPath}/style.css`,
22+
`http://localhost:3001/public/style.css`,
1723
)
1824
})
25+
1926
it('should join paths with protocol free public path', () => {
2027
const publicPath = '//127.0.0.1/public'
2128
expect(joinURLPath(publicPath, 'style.css')).toBe(
22-
`${publicPath}/style.css`,
29+
`//127.0.0.1/public/style.css`,
2330
)
2431
})
2532
})

0 commit comments

Comments
 (0)