Skip to content

Commit

Permalink
Set web worker startup options with messages instead of query strings (
Browse files Browse the repository at this point in the history
…#1574)

## Motivation for the change, related issues

When the web worker is loaded from browser storage it loses the query
string context we use to send startup options like WP and PHP version.

To enable offline support, this PR changes how the worker obtains
startup options to ensure it works when cached.

## Implementation details

Instead of fetching startup options from the query string,
`spawnPHPWorkerThread` will post startup options as a message to the
worker.

When `worker-utils` loads it will await the `startup-options` message
and continue once it receives the data.

## Testing Instructions (or ideally a Blueprint)

- Ensure all tests pass

~~@brandonpayton @adamziel I'm not sure about this solution. It works,
but I'm not happy with it. How does it look to you?~~
  • Loading branch information
bgrgicak authored Jul 9, 2024
1 parent 966f7bf commit 97d95f3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export async function spawnPHPWorkerThread(
(error as any).filename = e.filename;
reject(error);
};
worker.postMessage({
type: 'startup-options',
startupOptions,
});
// There is no way to know when the worker script has started
// executing, so we use a message to signal that.
function onStartup(event: { data: string }) {
Expand Down
30 changes: 18 additions & 12 deletions packages/playground/remote/src/lib/worker-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,24 @@ export type ParsedStartupOptions = {
siteSlug: string;
};

export const receivedParams: ReceivedStartupOptions = {};
const url = self?.location?.href;
if (typeof url !== 'undefined') {
const params = new URL(self.location.href).searchParams;
receivedParams.wpVersion = params.get('wpVersion') || undefined;
receivedParams.phpVersion = params.get('phpVersion') || undefined;
receivedParams.storage = params.get('storage') || undefined;
// Default to CLI to support the WP-CLI Blueprint step
receivedParams.sapiName = params.get('sapiName') || 'cli';
receivedParams.phpExtensions = params.getAll('php-extension');
receivedParams.siteSlug = params.get('site-slug') || undefined;
}
const getReceivedParams = async () => {
return new Promise<ReceivedStartupOptions>((resolve) => {
self.addEventListener('message', async (event) => {
if (event.data.type === 'startup-options') {
resolve({
wpVersion: event.data.startupOptions.wpVersion,
phpVersion: event.data.startupOptions.phpVersion,
storage: event.data.startupOptions.storage,
sapiName: event.data.startupOptions.sapiName,
phpExtensions: event.data.startupOptions['php-extension'],
siteSlug: event.data.startupOptions['site-slug'],
});
}
});
});
};

const receivedParams: ReceivedStartupOptions = await getReceivedParams();

export const requestedWPVersion = receivedParams.wpVersion || '';
export const startupOptions = {
Expand Down

0 comments on commit 97d95f3

Please sign in to comment.