Skip to content

Backfill the assets removed from minified WordPress bundles#1604

Merged
bgrgicak merged 48 commits into
trunkfrom
add/wp-static-asset-zip
Jul 16, 2024
Merged

Backfill the assets removed from minified WordPress bundles#1604
bgrgicak merged 48 commits into
trunkfrom
add/wp-static-asset-zip

Conversation

@bgrgicak
Copy link
Copy Markdown
Collaborator

Motivation for the change, related issues

To achieve offline support we need to download all WordPress files into the Playground filesystem. Today Playground downloads WordPress partially on boot and downloads assets as they are needed using fetch requests.

This PR downloads all assets into the Playground filesystem without blocking the boot process.

Implementation details

Zip files are generated using the WordPress build script and are accessible in the /wp-VERSION/ folder like any other assets.

The download is triggered after WordPress is installed. The fetch is async and won't block the boot process.

In case some of the assets are needed immediately on boot they will still be downloaded using fetch to ensure they are available to WordPress.

Once the assets are downloaded they are stored in the Playground filesystem and will be served from there.

Testing Instructions (or ideally a Blueprint)

  • Run npx nx bundle-wordpress:nightly playground-wordpress-builds
  • Confirm that a packages/playground/wordpress-builds/public/wp-nightly/wordpress-static.zip file was created
  • Load Playground using this blueprint
  • In network tools see that the wordpress-static.zip file was downloaded
  • Open /wp-admin/
  • In network tools confirm that static assets like thickbox.js were loaded from the service worker instead of a fetch

}

/**
* If the browser is online we can download WordPress assets asynchronously to speed up the boot process.
Copy link
Copy Markdown
Collaborator

@adamziel adamziel Jul 12, 2024

Choose a reason for hiding this comment

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

I got a bit confused why that's optional – this should clarify it:

Suggested change
* If the browser is online we can download WordPress assets asynchronously to speed up the boot process.
* If the browser is online we download the WordPress assets asynchronously to speed up the boot process.

Also, let's clarify this comment talks about the minified WordPress bundles and that we're not doing either of these when a full production build is used (since it ships all the static assets already).

Copy link
Copy Markdown
Collaborator

@adamziel adamziel Jul 15, 2024

Choose a reason for hiding this comment

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

I'm reopening this conversation because this isn't addressed yet:

Also, let's clarify this comment talks about the minified WordPress bundles and that we're not doing either of these when a full production build is used (since it ships all the static assets already).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done 2ef0bcd. I kept that part short because it's described in detail in the function docblock.

* If the browser is online we can download WordPress assets asynchronously to speed up the boot process.
* Missing assets will be fetched on demand from the Playground server until they are downloaded.
*
* If the browser is offline, we download WordPress assets synchronously to ensure the Playground is fully functional on load.
Copy link
Copy Markdown
Collaborator

@adamziel adamziel Jul 12, 2024

Choose a reason for hiding this comment

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

How are we downloading assets if the browser is offline?

Also, nitpick: Synchronous means blocking, meaning the event loop would be stopped until the download is complete. In here, however, we're not pausing the entire worker but just awaiting the download before continuing the boot process.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The comment was misleading, this is what it needs to say: _ If the browser is offline, we backfill WordPress assets synchronously from the cache to ensure Playground is fully functional before boot finishes._

Copy link
Copy Markdown
Collaborator

@adamziel adamziel Jul 15, 2024

Choose a reason for hiding this comment

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

I'm reopening this conversation because this isn't addressed yet:

Synchronous means blocking, meaning the event loop would be stopped until the download is complete. In here, however, we're not pausing the entire worker but just awaiting the download before continuing the boot process.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I get it now. In my mind, the comment sounded the same, but await and synchronous definitely aren't the same. 3e4cd07

Comment thread packages/playground/remote/src/lib/worker-thread.ts Outdated
Comment thread packages/playground/remote/src/lib/worker-thread.ts Outdated
Comment thread packages/playground/remote/src/lib/worker-thread.ts Outdated
Comment thread packages/playground/remote/src/lib/worker-thread.ts Outdated
@adamziel
Copy link
Copy Markdown
Collaborator

adamziel commented Jul 12, 2024

Most of my notes are about comments and code style. The only technical blocker I see is this:

If the browser is offline, we download WordPress assets synchronously to ensure the Playground is fully functional on load.

How would it download the static assets when the browser is offline? Would it use the zip file cached in the service worker, reject the promise, and display a reasonable error message if that file not cached yet? If yes, that sounds fine.

Comment thread packages/playground/remote/src/lib/boot-playground-remote.ts Outdated
Co-authored-by: Adam Zieliński <adam@adamziel.com>
@bgrgicak
Copy link
Copy Markdown
Collaborator Author

How would it download the static assets when the browser is offline? Would it use the zip file cached in the service worker, reject the promise, and display a reasonable error message if that file not cached yet? If yes, that sounds fine.

The comment was misleading. The difference is that in offline mode boot waits for the backfill to finish, and when offline it doesn't.

For offline support to work, the wordpress-static.zip needs to be cached, so we expect it to be available.

@bgrgicak bgrgicak requested a review from adamziel July 15, 2024 08:58
Comment thread packages/playground/remote/src/lib/boot-playground-remote.ts
Comment thread packages/playground/remote/src/lib/worker-thread.ts Outdated
@adamziel adamziel changed the title Download all WordPress assets on boot Backfill the assets removed from minified WordPress bundles Jul 15, 2024
Copy link
Copy Markdown
Collaborator

@adamziel adamziel left a comment

Choose a reason for hiding this comment

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

The implementation seems great so I'm approving. Thank you @bgrgicak! Let's still address the notes I left about the documentation and inline comments before merging.

@bgrgicak
Copy link
Copy Markdown
Collaborator Author

@adamziel I addressed your comments and will merge it now to unblock development.
Let me know if the comments require more changes and I can push them to another PR.

@bgrgicak bgrgicak merged commit c8b1752 into trunk Jul 16, 2024
@bgrgicak bgrgicak deleted the add/wp-static-asset-zip branch July 16, 2024 06:07
bgrgicak added a commit that referenced this pull request Jul 16, 2024
…1614)

## Motivation for the change, related issues

#1604 accidentally
introduced recursive calls to
`webApi.backfillStaticFilesRemovedFromMinifiedBuild`.

## Implementation details

Update `webApi.backfillStaticFilesRemovedFromMinifiedBuild` to call
`phpApi.backfillStaticFilesRemovedFromMinifiedBuild`.

## Testing Instructions (or ideally a Blueprint)

- checkout this branch
- confirm that there are no errors related to calls to
`backfillStaticFilesRemovedFromMinifiedBuild` in the browser console
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

No open projects
Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants