Skip to content

[Blueprints] Use the local worker in Builder during development mode #2495

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

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

Conversation

mho22
Copy link
Collaborator

@mho22 mho22 commented Aug 12, 2025

Motivation for the change, related issues

In order to find a solution to issue #2466. I struggled to go through the worker-thread run on port 4400 as the one used in the builder is the playground.wordpress.net one when running npm run dev.

Implementation details

If import.meta.env.MODE === 'production then use the remote worker, else use the local one.

Additionally, due to complex caching, other steps should be taken into account to get a fresh worker during page refresh. Explained below.

I didn't modify the following files :

packages/playground/website/src/github/preview-pr/form.tsx
packages/playground/website/src/components/error-report-modal.tsx

And possible errors can occur in these tests :

packages/playground/website/playwright/e2e/blueprints.spec.ts
packages/playground/php-cors-proxy.tests/ProxyFunctionsTests.php

Testing Instructions

  1. Add a test log inside the boot method in worker-thread.ts.

  2. Run npm run dev

  3. Go to http://127.0.0.1:5400/website-server/builder/builder.html

  4. Unregister the http://127.0.0.1:4400 service worker.

  5. Delete the playground-cache- in CacheStorage data in the CacheStorage section in Application tab from the browser developer tools.

  6. Refresh page.

You should see the log.

  1. Modify the previous test log inside the boot method in worker-thread.ts and reproduce the steps 3 to 5.

You should see the modified log.


Extra steps if the service worker stops running unexpectedly :

  1. enable Bypass for network in the Service Worker section in the Application tab from the browser developer tools and unregister the service worker from http://127.0.0.1:4400.
  2. Do a hard Refresh the page.
  3. Disable Bypass for network in the Service Worker section in the Application tab from the browser developer tools and do a hard refresh the page again.

Copy link
Member

@brandonpayton brandonpayton left a comment

Choose a reason for hiding this comment

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

Hi @mho22, thank you for working on this. This issue has bothered me for a while, but I haven't taken the time to fix it.

I left some questions and suggestions below, but this looks like a good thing for us to do.

@@ -215,6 +215,8 @@ const validRemoteOrigins = [
'https://localhost',
'http://127.0.0.1',
'https://127.0.0.1',
'http://127.0.0.1:4400',
'http://localhost:4400',
Copy link
Member

Choose a reason for hiding this comment

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

Is there any downside to using
import { remoteDevServerHost, remoteDevServerPort } from '../../build-config';
in this module and defining these string using those imports?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No, you're right I should use these configs instead. On it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done.

target:
mode === 'production'
? 'https://playground.wordpress.net'
: `http://${remoteDevServerHost}:${remoteDevServerPort}`,
Copy link
Member

Choose a reason for hiding this comment

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

IIRC, the purpose of this bit is to forward requests to playground.wordpress.net/plugin-proxy.php when a plugin-proxy.php request is received by the the dev server. The plugin-proxy.php script requires PHP and a GH_TOKEN environment var, so I think maybe we want to keep forwarding such requests to playground.wordpress.net where there is both a PHP server and that secret defined.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done.

Comment on lines 305 to 307
import.meta.env.MODE === 'production'
? `https://playground.wordpress.net/plugin-proxy.php?${proxyParams}`
: `http://${remoteDevServerHost}:${remoteDevServerPort}/plugin-proxy.php?${proxyParams}`
Copy link
Member

Choose a reason for hiding this comment

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

Could we conditionally define the target origin once in this module and replace these ternaries with a single template str? For example:

Suggested change
import.meta.env.MODE === 'production'
? `https://playground.wordpress.net/plugin-proxy.php?${proxyParams}`
: `http://${remoteDevServerHost}:${remoteDevServerPort}/plugin-proxy.php?${proxyParams}`
`${playgroundServerOrigin}/plugin-proxy.php?${proxyParams}`

Copy link
Member

Choose a reason for hiding this comment

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

On a related note, what if we default to the current origin when in production so both of the following could work?

https://playground.wordpress.net/builder/builder.html
https://my.favorite.self.hosted.playground.domain/builder/builder.html

Copy link
Collaborator Author

@mho22 mho22 Aug 15, 2025

Choose a reason for hiding this comment

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

done. It is way more readable!

@mho22
Copy link
Collaborator Author

mho22 commented Aug 15, 2025

@brandonpayton Thank you for your review! I added the corrections you suggested.

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.

2 participants