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
Conversation
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
|
||
# 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, | ||
) |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call - added
There was a problem hiding this 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
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.Some of the sprockets-related changes in this commit are not ideal, but we hope to remove sprockets in the not-too-distant future.