Skip to content
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

Fix page.url doesn't contain url prefix #333

Merged
merged 1 commit into from
Jan 6, 2022

Conversation

ppodds
Copy link

@ppodds ppodds commented Dec 16, 2021

Laravel PR 40014

Same issue. This PR fixes the URL prefix problem in src/Response.php. Making us deploy Laravel + Inertia project in a subfolder of domain possible.

@ppodds ppodds marked this pull request as ready for review December 16, 2021 14:15
@claudiodekker
Copy link
Member

Hey 👋,

Thanks for your pull request to Inertia!

Could you please provide additional information that includes an explanation of the exact problem, how to reproduce said problem, and how this change fixes that problem? It is difficult for us to consider pull requests without this, as it otherwise may introduce breaking changes to the library.

Thanks!

@ppodds
Copy link
Author

ppodds commented Dec 30, 2021

Problem Introduce

Inertia generates the wrong url when the Laravel project deploys under the subfolder of the domain.

How To Reproduce

This problem only occurs when you deploy your Laravel project in a subfolder of the domain like this.

https://my.domain/to/project

NGINX setting (reverse proxy) maybe like this

server {
  listen 80;
  listen [::]:80;

  server_name _;
  location /to/project/ {
    proxy_set_header X-Forwarded-Prefix /to/project;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
    proxy_set_header X-Forwarded-Host my.domain;
    proxy_set_header X-Forwarded-Port 443;
    # laravel backend at localhost:8080
    proxy_pass http://localhost:8080/;
}

In the said condition below, inertia will set url = `` when user requests https://my.domain/to/project, cause user's browser URL to be rewritten as `https://my.domain/`.

Fix

toResponse($request) (in src/Response.php) doesn't consider base URL in $page.url. This PR just add base URL in front of request URI to fix it.

<?php
...
class Response implements Responsable
{
    use Macroable;
    ...

    public function toResponse($request)
    {
        ...
        $page = [
            'component' => $this->component,
            'props' => $props,
            'url' => $request->getBaseUrl().$request->getRequestUri(),
            'version' => $this->version,
        ];
        ...
        return ResponseFactory::view($this->rootView, $this->viewData + ['page' => $page]);
    }
}

@claudiodekker
Copy link
Member

Thanks!

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

Successfully merging this pull request may close these issues.

2 participants