Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trailingSlash option isn't applied on client-side links when a relative path is specified #35419

Closed
2 tasks done
pragmaticpat opened this issue Apr 13, 2022 · 5 comments · Fixed by #35444
Closed
2 tasks done
Labels
status: confirmed Issue with steps to reproduce the bug that’s been verified by at least one reviewer. topic: core Relates to Gatsby's core (e.g. page loading, reporter, state machine) type: bug An issue or pull request relating to a bug in Gatsby

Comments

@pragmaticpat
Copy link
Contributor

Preliminary Checks

Description

See : #34205 (comment)

Reproduction Link

https://github.com/wad-im/wadim-on-the-internet

Steps to Reproduce

  1. Clone the linked repository
  2. Run gatsby develop
  3. Hover the first blog post => notice no trailing slash
  4. Click the first blog post -> notice no trailing slash in the URL
  5. Refresh the page -> notice trailing slash has been applied

Expected Result

The hover and clicked link should both have trailing slashes, as requested in the configuration option trailingSlash: "always"

Actual Result

The hover and clicked link should both lack trailing slashes, even though trailing slash was requested in the configuration option trailingSlash: "always"

Environment

- n/a -

Config Flags

trailingSlash: "always"

@pragmaticpat pragmaticpat added the type: bug An issue or pull request relating to a bug in Gatsby label Apr 13, 2022
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Apr 13, 2022
@LekoArts LekoArts added status: confirmed Issue with steps to reproduce the bug that’s been verified by at least one reviewer. topic: core Relates to Gatsby's core (e.g. page loading, reporter, state machine) and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Apr 13, 2022
@LekoArts
Copy link
Contributor

A way to "fix" this is to add leading slashes to the slug in the frontmatter.

https://github.com/wad-im/wadim-on-the-internet/blob/c4bea721c3972abbe02fbe0fc3454e9395c01e2f/blog/first-post/index.mdx

Because that's why it's not working. So instead of first-blogpost/ it would be /first-blogpost.

Inside gatsby-link we absolutify this path since the check for this is falsy:

return isAbsolutePath(adjustedPath)
? withPrefix(adjustedPath)
: absolutify(adjustedPath, relativeTo)

So after we successfully apple the trailing slash, with absolutify it's gone afterwards.

Our <Link> in the e2e tests all use links with leading slash: https://github.com/gatsbyjs/gatsby/blob/master/e2e-tests/trailing-slash/src/pages/index.js
We should use (for testing purposes) to without a leading slash.

@wad-im
Copy link

wad-im commented Apr 13, 2022

I tried this quickly and changed one of the slugs from first-blogpost/to /first-blogpost in development only, and now I get a 404 page when I click on the respective link.

Removing any slash from the slug in the frontmatter makes no difference to behaviour described above

@LekoArts
Copy link
Contributor

Because the page you created now has two leading slashes:
https://github.com/wad-im/wadim-on-the-internet/blob/c4bea721c3972abbe02fbe0fc3454e9395c01e2f/gatsby-node.ts#L24

Removing it there and rerunning a build will work. (with path: node.frontmatter.slug)

@wad-im
Copy link

wad-im commented Apr 13, 2022

Thank you, Lennart. You are of course right and it works like a charm and as expected now.

@LekoArts
Copy link
Contributor

We still wanna fix the version you previously had though, thanks for surfacing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: confirmed Issue with steps to reproduce the bug that’s been verified by at least one reviewer. topic: core Relates to Gatsby's core (e.g. page loading, reporter, state machine) type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants