valet icon indicating copy to clipboard operation
valet copied to clipboard

Enable local network sharing

Open thinkverse opened this issue 2 years ago • 1 comments

This PR enables a rudimentary version of local network sharing with the fix mentioned in (https://github.com/laravel/valet/issues/440#issuecomment-579830736). This pull request was suggested in (https://github.com/laravel/docs/pull/8267) by @driesvints.

I've tested the fix locally and it does enable local network sharing, though it's rudimentary at best since assets don't load properly given the URL doesn't match.

For example, if I have http://laravel.test and access is via local network sharing http://192.168.1.111/laravel.test, the site does load. But the asset URLs will use http://laravel.test and thus not load. I'm not sure if that is due to the @vite directive or something else. The same thing happens with the asset helper, which might be because of the APP_URL.

I did manage to get the CSS, JavaScript, and HMR working with Vite by starting the Vite server using the local IP address passed in as the host.

npm run dev -- --host 192.168.1.111

Since Vite adds the IP address in the script and link files, the IP address used for the host must be an address that can be accessed by all devices over the network, The safest route is to use the same IP address of the device you're trying to expose.

Not sure how to go about fixing the issue of built assets not loading due to the URL not matching though. 🤷‍♂️

thinkverse avatar Sep 30 '22 14:09 thinkverse

Also, could anyone confirm or deny if this breaks Laravel Valets' wildcard DNS support? I don't use x|nip.io services so sadly cannot check that myself.

thinkverse avatar Sep 30 '22 14:09 thinkverse

I'm good with this modification, but I'll need someone who uses nip.io to test it out for me first.

Put out a request: https://twitter.com/stauffermatt/status/1598332500056621062

mattstauffer avatar Dec 01 '22 15:12 mattstauffer

I tested it on my setup and it worked, but there is a problem with Drivers. BasicValetDriver has a line

$_SERVER['SERVER_ADDR'] = '127.0.0.1';

that overrides server address, making a redirect to http://site-name.test. I don't know why there is a 127.0.0.1 override, but we can make fallback like this, where we can replace the ovveride condition with a proper one:

$serverAddr = $_SERVER['SERVER_ADDR'];

// condition when to override the server address if necessary
if (!$serverAddr || $serverAddr == 'localhost') {
    $_SERVER['SERVER_ADDR'] = '127.0.0.1';
}

same goes for every driver that do this. I can make a PR, but I would need to know when to enable this override 😄

F0rsaken avatar Dec 07 '22 16:12 F0rsaken

@F0rsaken Unfortunately I don't know yet whether your code there would or wouldn't be enough... I'm guessing it would be? I'll test it out and see if I can figure out.

mattstauffer avatar Jan 30 '23 01:01 mattstauffer

OK. I made a modification similar to yours @F0rsaken, but I didn't worry about the localhost check.

In theory that means this PR should work. I'll test it out now or shortly.

mattstauffer avatar Jan 30 '23 02:01 mattstauffer

Well.. this isn't perfect, but it at least is a start, with a bit of a potential fix, and it makes the docs at least a bit less inaccurate. lol. I just ran it and it worked, but with the same issues mentioned w/r/t the assets.

Thanks @thinkverse!

mattstauffer avatar Feb 01 '23 22:02 mattstauffer