Open patrickdemers6 opened 4 months ago
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.
It seems like the source of the bug is in gatsby-link
, where several code paths don't call withPrefix
. I cloned the gatsby repo, and modified the gatsby-link
package to add the appropriate lines. (Confusingly, this file seems to have an isAbsolutePath
function which returns true for relative paths).
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)
The 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 patches/gatsby-link+5.10.0.patch
in your repository.
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