-
Notifications
You must be signed in to change notification settings - Fork 257
Improve URL handling with support for trailing slashes #731
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
Conversation
…aracters This PR provides a more consistent approach to URL handling in Inertia responses by: - Adding support for preserving trailing slashes in URLs - Improving readability by decoding special characters in query parameters - Maintaining compatibility with proxy prefixes - Ensuring URL consistency for SEO and debugging Previously, characters like slashes (%2F) and ampersands (%26) were encoded in the URL, making debugging more difficult and causing inconsistencies between browser URLs and Inertia history state URLs. Comprehensive tests have been added for both trailing and non-trailing slash scenarios, as well as specific tests for slash and ampersand handling in query parameters. Resolves issues discussed in inertiajs#663
Thanks for this! I reverted the Str::start(Str::after($request->fullUrl(), $request->getSchemeAndHttpHost()), '/'); As I don't want to revert the changes made in #592. Your new tests still passed with these changes! |
@pascalbaljet Also commenting this here for visibility. I believe this introduces a significant breaking change. Laravel itself does basically everything without trailing slashes. For example, if you use a paginator, the links it'll generate are without a trailing slash. Redirects are done without a trailing slash. |
I don't think so. |
In your own blog post, you write:
So I don't really follow the argument for SEO. Also as @RobertBoes mentioned, this seems to battle how URL generation in Laravel works, for things like I appreciate the attempt to solve #663 though. |
Thanks @pascalbaljet I really appreciate the support. I was careful about not disturbing the changes in #592 with this code but your solution works as well, so no complaints. It appears that others are worried that this PR will make trailing slashes the standard. It will not - all this PR does is make it possible to use them if you need to, without that very ugly and expensive workaround mentioned in the description. If your URLs don't have trailing slashes, then they'll be returned without them. If your URLs do have trailing slashes, they will now no longer be stripped. Don't throw around claims of breaking changes without backing them up with unit tests, it's really not helpful. |
Trailing slashes in URL are legit: https://developers.google.com/search/blog/2010/04/to-slash-or-not-to-slash
However the workaround for this package is pretty ugly: https://medium.com/@elbijay/trailing-slashes-for-inertia-and-laravel-74718fae51e2
Not only that, but the above solution calling setContent will double the request rendering. Without this code, only one view and one http client is created for each request.

This PR improves URL handling to properly preserve trailing slashes in URLs, which is important for SEO and compatibility with certain routing configurations. It also ensures proper handling of proxy prefixes with trailing slashes (which is how this bug was introduced originally)
EDIT: Hopefully this doesn't decrease the chances that this will get merged, but I added a second commit to solve this PR. Since the logic for determining the URL is now in it's own function, it is much easier to write clean code for each of these situations.