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

STU-22: Add custom local domain support for WordPress sites #994

Open
wants to merge 21 commits into
base: trunk
Choose a base branch
from

Conversation

youknowriad
Copy link

@youknowriad youknowriad commented Mar 2, 2025

Related issues

Summary …

  • Implemented custom domain support for WordPress sites using a proxy server
  • Added UI for setting custom domains when creating sites
  • Integrated with system hosts file for local domain resolution
  • The user chooses a custom domain on site creation (editing an existing site is harder because it would require updates to the WP database).

Implementation details

  • Created a proxy server that listens on port 80 and forwards requests to the appropriate local site. This means that if 80 is already used, custom domains won't work (there's no way around this)
  • Added domain management in hosts file to resolve custom domains to localhost
  • Updated site details to store and display custom domain information
  • Modified URL display in settings to show custom domain when enabled
  • Added validation for custom domain format

Test plan

  • Create a new site with custom domain enabled
  • Verify the domain is added to hosts file (requires admin permission)
  • Visit the site using the custom domain (without port)
  • Verify admin and frontend URLs work properly
  • Delete the site and verify domain is removed from hosts file

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors?

Important note This PR has been created for a huge part using AI. My main purpose with this PR was to learn both Studio's code base and learn AI while implementing a useful feature.

Screenshot 2025-03-02 at 2 20 18 PM

@youknowriad youknowriad self-assigned this Mar 2, 2025
@youknowriad youknowriad force-pushed the add/custom-domain-support branch 2 times, most recently from 4f49875 to 57a013b Compare March 2, 2025 13:23
try {
console.log( 'Attempting to start proxy server with elevated privileges' );

if ( currentPlatform === 'win32' ) {
Copy link
Author

@youknowriad youknowriad Mar 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently in Windows, we need to run the proxy with admin rights. The AI proposed using netsh to forward to a custom port. I need someone to help me test Windows.

@youknowriad youknowriad requested review from wojtekn and sejas March 3, 2025 08:56
@fredrikekelund fredrikekelund requested review from a team and removed request for wojtekn, sejas, fredrikekelund, nightnei and katinthehatsite March 3, 2025 11:47
Copy link
Contributor

@fredrikekelund fredrikekelund left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to work relatively well in my light testing, which is great 👍 Still, this is a big feature, and we will need to test it thoroughly before landing.

A couple of initial high-level thoughts:

  • There are several visual issues in the Add site modal right now. We should get some designer input from @matt-west or someone else on how to implement this.
  • Users should be able to edit the domain after a site is added. This is already a big PR, so we don't necessarily need to implement that here, but it should go into the same release as this feature.

Comment on lines +229 to +220
useCustomDomain,
setUseCustomDomain,
customDomain,
setCustomDomain,
customDomainError,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the fault of this PR, but we should really move this state inside SiteForm and pass it back to the parent in the onSubmit handler. Too much prop drilling now with no clear benefit.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Entirely agree, I noticed it too :)

port,
phpVersion,
customDomain,
useCustomDomain,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to store a boolean for useCustomDomain in the user config? Is it not enough to store customDomain?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm hesitant, I believe that we can get away with just "customDomain" but a boolean gives a sense of clarity. We absolutely need both as state values in the form.

Comment on lines 73 to 75
if ( domainRegex.test( hostsContent ) ) {
return; // Domain already exists
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do we do when this happens..? Display some kind of error in the app?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, I was thinking of cases when the domain is pointing to a different IP. Edge case, perhaps, but it ties into other error handling questions like #994 (comment)

Copy link
Contributor

@wojtekn wojtekn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The basic happy path works fine on Mac. I left some comments related to small issues and different cases.

const tempPath = '/tmp/wp-studio-hosts';
await writeFile( tempPath, content );
// @ts-expect-error promisify doesn't seem typed properly.
await sudoExec( `cat ${ tempPath } > ${ hostsPath }`, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It asks for permission each time I add a new site. Is there a way to save it somehow, at least for the current Studio session?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm reading this properly https://www.npmjs.com/package/sudo-prompt#concurrency it's probably not possible.

That said, I don't think it's a problem, I think it was more visible when we had the bug where the hosts file was modified on each "start". But right now, it's only when you start the server for the first time for a given site. So a real user is very unlikely to face the issue on a regular basis.

@richtabor richtabor changed the title Add custom local domain support for WordPress sites STU-22: Add custom local domain support for WordPress sites Mar 5, 2025
@mcsf
Copy link
Member

mcsf commented Mar 6, 2025

Pushed some unit tests for the hosts-file module. 9bf1b56 and b3c4c7b show different possible approaches. Read the their commit messages for details. Feel free to push a revert for the latter if the former is preferred.

@mcsf
Copy link
Member

mcsf commented Mar 6, 2025

I pushed a revert, so we keep the first approach. Decisions, not options. :)

@youknowriad
Copy link
Author

Thanks @mcsf

I tried adding e2e test myself for the feature, there's a good and a bad news:

  • The good news is that the infrastructure in place allows writing e2e tests pretty easily. Thanks @wojtekn and friends.
  • The bad news is that the current PR can't be e2e tested because it relies on a system prompt (sudo)

Overall, I think the PR is a good state, I would love more testing specifically. cc @wojtekn @fredrikekelund

@youknowriad youknowriad force-pushed the add/custom-domain-support branch from dfd8a89 to 12ce9e9 Compare March 7, 2025 04:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Router mode request (localhost and site domain)
8 participants