-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Trailing slash redirect broken when using path prefix #39043
Comments
I was running into this too. I noticed that Gatsby seems to be dead and while in the long run, I expect to move away from Gatsby, but in the short-term, I needed a fix. IssueIt seems like the source of the bug is in diff --git a/packages/gatsby-link/src/rewrite-link-path.js b/packages/gatsby-link/src/rewrite-link-path.js
index e1b2b49671..6668b76c9b 100644
--- a/packages/gatsby-link/src/rewrite-link-path.js
+++ b/packages/gatsby-link/src/rewrite-link-path.js
@@ -13,25 +13,26 @@ const getGlobalTrailingSlash = () =>
function applyTrailingSlashOptionOnPathnameOnly(path, option) {
const { pathname, search, hash } = parsePath(path)
const output = applyTrailingSlashOption(pathname, option)
return `${output}${search}${hash}`
}
function absolutify(path, current) {
// If it's already absolute, return as-is
if (isAbsolutePath(path)) {
- return path
+ return withPrefix(path)
}
+ const prefixed = withPrefix(path)
const option = getGlobalTrailingSlash()
- const absolutePath = resolve(path, current)
+ const absolutePath = resolve(prefixed, current)
if (option === `always` || option === `never`) {
return applyTrailingSlashOptionOnPathnameOnly(absolutePath, option)
}
return absolutePath
}
function applyPrefix(path) {
const prefixed = withPrefix(path)
Fixing ItThe site I'm working on uses patch-package to apply the above change to Gatsby link. To generate this yourself, you can clone the repo, apply the above change, build, and then generate a patch from that. If you'd rather re-use my work, you can copy the patch file in this gist to I haven't tested this too thoroughly, but it seems to handle all the cases I've thrown at it so far. |
Thank you for sharing your solution with the community @rosszurowski! Though we aren't actively investing in new features or low-priority bug fixes, Gatsby is still maintained. If you open a PR with your bug fix, we'll take a look and get it merged so everyone can benefit! Thanks 🙂 |
Hi @serhalp — good to know! I didn't see an official response in that thread, so assumed it was accurate. I'll gladly open a PR with the fix. |
Preliminary Checks
Description
A project using
pathPrefix
andtrailingSlash
does not redirect properly.When a page is loaded and requires a redirect due to the slash, the redirect does not take
pathPrefix
into account.Reproduction Link
https://github.com/patrickdemers6/gatsby-slash-redirect-path-prefix-bug
Steps to Reproduce
pathPrefix
to anything.trailingSlash
tonever
.gatsby build --prefix-paths
andgatsby serve --prefix-paths
Example:
pathPrefix = /prefix
Expected Result
The redirect removes just the trailing slash.
Expected
/prefix/hello/
=>/prefix/hello
Actual Result
The redirect removes the slash and the pathPrefix.
Actual
/prefix/hello/
=>/hello
Environment
Config Flags
No response
The text was updated successfully, but these errors were encountered: