Skip to content

Commit

Permalink
Merge pull request #592 from inertiajs/fix-url-generation
Browse files Browse the repository at this point in the history
Fix URL generation
  • Loading branch information
jessarcher authored Mar 8, 2024
2 parents 463b280 + 5139432 commit 778ead7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;
use GuzzleHttp\Promise\PromiseInterface;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Contracts\Support\Arrayable;
Expand Down Expand Up @@ -99,7 +100,7 @@ public function toResponse($request)
$page = [
'component' => $this->component,
'props' => $props,
'url' => $request->getBaseUrl().$request->getRequestUri(),
'url' => Str::start(Str::after($request->fullUrl(), $request->getSchemeAndHttpHost()), '/'),
'version' => $this->version,
];

Expand Down
39 changes: 39 additions & 0 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,43 @@ public function test_responsable_with_invalid_key(): void
$page['props']['resource']
);
}

public function test_the_page_url_is_prefixed_with_the_proxy_prefix(): void
{
if (version_compare(app()->version(), '7', '<')) {
$this->markTestSkipped('This test requires Laravel 7 or higher.');
}

Request::setTrustedProxies(['1.2.3.4'], Request::HEADER_X_FORWARDED_PREFIX);

$request = Request::create('/user/123', 'GET');
$request->server->set('REMOTE_ADDR', '1.2.3.4');
$request->headers->set('X_FORWARDED_PREFIX', '/sub/directory');

$user = ['name' => 'Jonathan'];
$response = new Response('User/Edit', ['user' => $user], 'app', '123');
$response = $response->toResponse($request);
$view = $response->getOriginalContent();
$page = $view->getData()['page'];

$this->assertInstanceOf(BaseResponse::class, $response);
$this->assertInstanceOf(View::class, $view);

$this->assertSame('/sub/directory/user/123', $page['url']);
}

public function test_the_page_url_doesnt_double_up(): void
{
$request = Request::create('/subpath/product/123', 'GET', [], [], [], [
'SCRIPT_FILENAME' => '/project/public/index.php',
'SCRIPT_NAME' => '/subpath/index.php',
]);
$request->headers->add(['X-Inertia' => 'true']);

$response = new Response('Product/Show', []);
$response = $response->toResponse($request);
$page = $response->getData();

$this->assertSame('/subpath/product/123', $page->url);
}
}

0 comments on commit 778ead7

Please sign in to comment.