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

PERF: Improve workbox loading strategy #22019

Merged
merged 3 commits into from Jun 9, 2023
Merged

Conversation

davidtaylorhq
Copy link
Member

Previously workbox JS was vendored into our git repository, and would be loaded from the public/javascripts directory with a 1 day cache lifetime. The main aim of this commit is to add 'cachebuster' to the workbox URL so that the cache lifetime can be increased.

  • Remove vendored copies of workbox.
  • Use ember-cli/broccoli to collect workbox files from node_modules into assets/workbox-{digest}
  • Add assets to sprockets manifest so that they're collected from the ember-cli output directory (and uploaded to s3 when configured)

Some of the sprockets-related changes in this commit are not ideal, but we hope to remove sprockets in the not-too-distant future.

Previously workbox JS was vendored into our git repository, and would be loaded from the `public/javascripts` directory with a 1 day cache lifetime. The main aim of this commit is to add 'cachebuster' to the workbox URL so that the cache lifetime can be increased.

- Remove vendored copies of workbox.
- Use ember-cli/broccoli to collect workbox files from node_modules into assets/workbox-{digest}
- Add assets to sprockets manifest so that they're collected from the ember-cli output directory (and uploaded to s3 when configured)

Some of the sprockets-related changes in this commit are not ideal, but we hope to remove sprockets in the not-too-distant future.
@@ -1,9 +1,21 @@
'use strict';

importScripts("<%= "#{Discourse.asset_host}#{Discourse.base_path}/javascripts/workbox/workbox-sw.js" %>");
<%
base = if GlobalSetting.use_s3? && GlobalSetting.s3_cdn_url
Copy link
Member

Choose a reason for hiding this comment

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

We don't have a method that does this already? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

We kinda do - ApplicationHelper#script_asset_path. But, it doesn't work here for a few reasons

  • It only works with files, but here we need a directory path
  • It checks the manifest to see whether a given asset exists. But when we're building the service-worker, the manifest hasn't yet been written
  • It adds .gz and .br extensions based on request headers. We don't want that here - workbox doesn't support it, and also we're baking the URLs into the compiled service-worker, so it cannot vary based on request headers

I considered trying to refactor #script_asset_path to extract this logic, but in the end I decided it was too specific to the workbox situation to justify complicating ApplicationHelper any further.

Comment on lines +52 to +61

# Skip digest path for workbox assets. They are already in a folder with a digest in the name.
Sprockets::Asset.prepend(
Module.new do
def digest_path
return logical_path if logical_path.match?(%r{^workbox-.*/})
super
end
end,
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Wonder if we make a note of this in workbox-tree-builder.js ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good call - added 👍

Copy link
Contributor

@jjaffeux jjaffeux left a comment

Choose a reason for hiding this comment

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

I don't have experience with all the parts here, but the js part looks quite straightforward, and nothing has been standing out 👍

@davidtaylorhq davidtaylorhq merged commit 9c926ce into main Jun 9, 2023
12 of 13 checks passed
@davidtaylorhq davidtaylorhq deleted the workbox-s3-ember-cli branch June 9, 2023 10:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants