Fix redirect logic to be able to handle dynamic parts in target paths #11184
+507
−52
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
There are a few related changes in this PR, but this was the use case I was trying to fix:
[category]/page/[page].astro
is the route handler formyCategory/page/1
.I'm trying to redirect from
/[category]
to/[category]/page/1
. Right now, since there's no RouteData with key/[category]/page/1
in the manifest, my redirect target falls back to the server-rendered path, which in my case for cloudflare, is the empty string '', causing the entry in_redirect
to be invalid. The line that gets generated is/:category 301
There is however, a RouteData in the manifest that's able to handle this path, so it should resolve that route at the key
/[category]/page/[page]
.Changes
underscore-redirect
to generate the right _redirect for targets with dynamic partsThis should allow this comment and hack to be removed
Questions
Are there edge cases that I'm missing? Its not clear to me if this is actually a backwards compatible change. It feels like my changes align with what the code is supposed to do, but this might break some folks?
Testing
Added test for each of the 3 cases above that did not exist before.
Docs
/cc @withastro/maintainers-docs Not sure if we need to add more docs around what is permissible in the redirect config, but this really threw me off guard that I couldn't do what I thought I was able to do.