Sharing Proxied Valet Domains

I ran into an issue recently when I wanted to share a Histoire book in a Laravel project which uses Valet. By default, Histoire runs on port 6006 of the valet domain (e.g. https://my-project.test:6006) rather than localhost, but valet share will only share ports 443 and 80 out of the box, and only works from the directory you run it from. I couldn’t use tunnelmole as that only works with apps running on localhost.

I couldn’t find an easy way to configure either valet, histoire or tunnelmole to handle this use case, so what I wound up doing was to proxy port 6006 to a new test domain using valet:

valet proxy histoire-proxy https://my-project.test:6006 --secure

At this point I can use valet link histoire-proxy then valet share from within the project directory. This works great, however means that the primary domain my-project can’t be shared with Valet simultaneously.

If this matters, then the solution is to create a dummy directory for the proxy domain to run valet share from instead.

mkdir histoire-proxy
cd histoire-proxy
composer require laravel/valet
valet share

The above should work for any port on a Valet proxied domain. If a service or project was running on localhost then I would use tunnelmole instead of valet share.