Skip to content

Commit

Permalink
fix(gatsby-link): modify relative links based on trailing slash option (
Browse files Browse the repository at this point in the history
#35444)

* fix ending slash issue

* change implementation

* remove extra lines

* correct relative path

* Update packages/gatsby-link/src/rewrite-link-path.js

Co-authored-by: Lennart <[email protected]>

Co-authored-by: Lennart <[email protected]>
  • Loading branch information
marvinjude and LekoArts authored Apr 19, 2022
1 parent 1b6a35b commit e30be3e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions e2e-tests/trailing-slash/cypress/integration/always.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ describe(`always`, () => {
cy.waitForRouteChange().assertRoute(`/client-only-splat/with/with/`)
cy.getTestElement(`title`).should(`have.text`, `with/with`)
})
it("relative path with trailing slash", () => {
cy.getTestElement(`relative-path-with-trailing-slash`).click()
cy.waitForRouteChange().assertRoute(`/page-2/`)
})
})

describe(`always (direct visits)`, () => {
Expand Down
5 changes: 5 additions & 0 deletions e2e-tests/trailing-slash/src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ const IndexPage = ({ data }) => {
</Link>
</li>
))}
<li>
<Link to="page-2" data-testid="relative-path-with-trailing-slash">
Relative path with trailing slash
</Link>
</li>
</ul>
</main>
)
Expand Down
10 changes: 9 additions & 1 deletion packages/gatsby-link/src/rewrite-link-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ function absolutify(path, current) {
if (isAbsolutePath(path)) {
return path
}
return resolve(path, current)

const option = getGlobalTrailingSlash()
const absolutePath = resolve(path, current)

if (option === `always` || option === `never`) {
return applyTrailingSlashOption(absolutePath, option)
}

return absolutePath
}

export const rewriteLinkPath = (path, relativeTo) => {
Expand Down

0 comments on commit e30be3e

Please sign in to comment.