Skip to content

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

Merged
merged 4 commits into from
Jun 4, 2025

Conversation

ljbw
Copy link
Contributor

@ljbw ljbw commented Apr 28, 2025

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.
image

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.

ljbw added 2 commits April 28, 2025 16:58
…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
@pascalbaljet pascalbaljet merged commit e96a821 into inertiajs:2.x Jun 4, 2025
18 checks passed
@pascalbaljet
Copy link
Contributor

Thanks for this! I reverted the urldecode part as discussed in #663; we need to find another solution there. I've also refactored back to using:

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!

@RobertBoes
Copy link
Contributor

RobertBoes commented Jun 5, 2025

@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. route('name') would generate a URL without trailing slash. Currently, Inertia will always use an URL without trailing slash, so this actually helps mitigate an incorrect webserver setup. This means Inertia follows how Laravel does URL generation. With this PR, more inconsistency is introduced.

@harryqt
Copy link

harryqt commented Jun 5, 2025

With this PR, more inconsistency is introduced.

I don't think so.

@jorqensen
Copy link

which is important for SEO

In your own blog post, you write:

For SEO purposes, it doesn’t really matter which way you go, but you should be consistent.

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 route(). Shouldn't the Laravel adapter follow Laravels conventions for consistency?

I appreciate the attempt to solve #663 though.

@ljbw
Copy link
Contributor Author

ljbw commented Jun 17, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants