Skip to content

Conversation

pascalbaljet
Copy link
Member

@pascalbaljet pascalbaljet commented Jun 5, 2025

Credit goes to @RobertBoes for bringing this idea to the table. I'll add you as an author when this is merged.

With this callback, you can customize the URL that gets sent to the frontend. There are many edge cases and preferences to have one unified way to do this (trailing slashes, encoded query params, etc.). This way, developers can customize the URL to their needs and setup.

Inertia::resolveUrlUsing(function (Request $request) {
    // Return a custom relative URL based on the current request.
    return Str::start(Str::after($request->fullUrl(), $request->getSchemeAndHttpHost()), '/');
});

Here are some of the issues/PRs about it:

@rojtjo
Copy link
Contributor

rojtjo commented Jun 5, 2025

In my original PR I also allowed overriding the URL resolver from the middleware. Could we have that added as well?

use Inertia\Middleware;

class HandleInertiaRequests extends Middleware
{
    public function urlResolver(Request $request): ?callable
    {
        return fn (Request $request) => 'https://inertiajs.com/'.$request->getRequestUri();
    }
}

@pascalbaljet
Copy link
Member Author

Yes, I'll work on that! And I'll add you as co-author as well :)

@pascalbaljet pascalbaljet changed the title Support for a custom URL resolver [2.x] Support for a custom URL resolver Jun 6, 2025
@pascalbaljet pascalbaljet merged commit 4dfc71e into 2.x Jun 17, 2025
37 checks passed
@pascalbaljet pascalbaljet deleted the custom-url-resolver branch June 17, 2025 07:29
@chack1172
Copy link

chack1172 commented Jun 19, 2025

@pascalbaljet there is actually an issue when setting a custom resolver. If the url does not match exactly the url in a , anchor tags will not work.

For example in the react playground, if you force leading slash:

Str::before($request->fullUrl(), '?') . '/'

Than go to the home and click article link, it will not redirect to the bottom, unless you change the link adding the trailing slash

@pascalbaljet
Copy link
Member Author

@pascalbaljet there is actually an issue when setting a custom resolver. If the url does not match exactly the url in a , anchor tags will not work.

The custom resolver was not introduced to add query params to URL (or similar changes). Its main goal is to prevent query params from being encoded, like brackets. I'm still playing around with some additional comparisons in the frontend library to improve on this further. Here's an example from the original PR that uses rawurldecode on the URL:

Inertia::resolveUrlUsing(function (Request $request) {
    return Str::start(Str::after(rawurldecode($request->fullUrl()), $request->getSchemeAndHttpHost()), '/');
});

@chack1172
Copy link

@pascalbaljet the code I posted was not correct, I wanted to only add a leading slash not adding a query param, I updated it.

@mrleblanc101
Copy link

@pascalbaljet Why was nothing added to the documentation about this ?

@pascalbaljet
Copy link
Member Author

@pascalbaljet Why was nothing added to the documentation about this ?

It's been released less than an hour ago 😅 I'm working on documenting this and some other new features and doc improvements in general. I'll probably open a PR for that next week.

@mrleblanc101
Copy link

@pascalbaljet Yeah, I just noticed the doc was a seperate repo.

@thaqebon
Copy link

It looks like the js core package changes the format of usePage().url after the initial page load.
What I'm trying to do:

// In HandleInertiaRequests.php
public function urlResolver()
{
    return fn (Request $request) => $request->fullUrl();
}

Expected Behavior:
I expect usePage().url to return the full URL consistently, including the scheme and domain — for example:
https://app.test/some/page

Actual Behavior:

  • On the first page load, usePage().url correctly returns:
    https://app.test/some/page
  • On subsequent client-side navigations, it only returns the path:
    /some/page

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