.localhost subdomains (e.g., myapp.localhost) to provide stable, named URLs for your development servers. While most browsers handle these automatically, Safari relies on the system DNS resolver, which may not resolve .localhost subdomains to 127.0.0.1.
The Problem
When you try to accesshttp://myapp.localhost:1355 in Safari, you may see:
- “Safari can’t find the server”
- “Can’t connect to the server”
- DNS resolution errors
- Chrome, Firefox, and Edge have built-in handling for
.localhostdomains - Safari uses the system DNS resolver (mDNSResponder on macOS)
- The system resolver may not be configured to resolve
.localhostsubdomains
The base
.localhost domain (e.g., localhost:1355) typically works fine. The issue is specifically with subdomains like myapp.localhost or api.myapp.localhost.Solution 1: Manual Hosts File Sync (Recommended)
The most reliable solution is to add entries to/etc/hosts for your Portless routes.
Sync Current Routes
Run this command to add all active routes to/etc/hosts:
Re-sync After Adding Routes
Whenever you start a new app with Portless, re-run the sync command:Clean Up
When you’re done developing, remove the Portless entries:/etc/hosts entries intact.
Solution 2: Automatic Hosts File Syncing
Portless can automatically update/etc/hosts whenever routes change, eliminating the need to manually run hosts sync.
Enable Auto-Sync
-
Set the
PORTLESS_SYNC_HOSTSenvironment variable:Add this to your shell configuration (.zshrc,.bashrc, etc.) to make it permanent. -
Start the proxy with
sudo(required for/etc/hostswrite access): -
Routes will now be automatically synced to
/etc/hostswhenever they change.
How It Works
WhenPORTLESS_SYNC_HOSTS=1:
- The proxy watches
routes.jsonfor changes - On startup, it syncs all existing routes to
/etc/hosts - When you start a new app, the route is immediately added to
/etc/hosts - When you stop an app, the route is removed from
/etc/hosts - On proxy shutdown, all Portless entries are cleaned up
Caveats
If you prefer not to run the proxy as root, use Solution 1 (manual syncing) instead.Solution 3: Use Different Browsers
If you don’t need to test in Safari specifically, use Chrome, Firefox, or Edge. These browsers have native support for.localhost subdomain resolution and don’t require any hosts file configuration.
Verification
To verify that DNS resolution is working:Checking Current Hosts Entries
To see what Portless has added to/etc/hosts:
Troubleshooting
”Permission denied” when running hosts sync
You need to run the command withsudo:
Auto-sync not working
Ensure:PORTLESS_SYNC_HOSTS=1is exported in your environment- The proxy was started with
sudo portless proxy start - The proxy is actually running (
portless list)
Safari still not connecting after sync
Try:- Clear Safari’s DNS cache by restarting Safari
- Flush the system DNS cache:
- Verify the route exists:
portless list - Test if the proxy is responding:
Entries not removed after stopping an app
If using auto-sync, entries should be removed automatically. If they persist:- Restart the proxy:
- Manually clean up:
Alternative: Use Port-Only URLs
If modifying/etc/hosts is not an option, you can access your apps via http://localhost:PORT directly, bypassing the proxy: