Enable service worker for legacy build
Proposed change
Create separate service workers for the modern and legacy builds. The legacy SW targets browsers that also support them to avoid some large unnecessary transpiling and polyfills. Adjusts the user-facing __BUILD__ global to the more user-friendly "modern" and "legacy" terms.
Workbox is patched with https://github.com/GoogleChrome/workbox/pull/3234 in order to use the injection method which properly adjusts the source maps.
404 errors will occur without https://github.com/home-assistant/core/pull/120488.
Type of change
- [ ] Dependency upgrade
- [ ] Bugfix (non-breaking change which fixes an issue)
- [x] New feature (thank you!)
- [ ] Breaking change (fix/feature causing existing functionality to break)
- [ ] Code quality improvements to existing code or addition of tests
Example configuration
Additional information
- This PR fixes or closes issue: fixes #
- This PR is related to issue or discussion:
- Link to documentation pull request:
Checklist
- [x] The code change is tested and works locally.
- [x] There is no commented out code in this PR.
- [ ] Tests have been added to verify that the new code works.
If user exposed functionality or configuration variables are added/changed:
- [ ] Documentation added/updated for www.home-assistant.io
Summary by CodeRabbit
-
New Features
- Added support for service workers in the legacy build profile.
- Enhanced service worker registration with dynamic script paths based on build type.
-
Refactor
- Updated build configurations to distinguish between "modern" and "legacy" builds.
- Refined service worker generation logic to handle modern and legacy builds separately.
- Improved file handling for service worker scripts.
-
Bug Fixes
- Patched
workbox-buildto address specific issues.
- Patched
-
Style
- Updated displayed JavaScript type from "latest" to "modern" in the UI.
-
Chores
- Removed unused
source-map-urldependency. - Adjusted browser compatibility configurations.
- Removed unused
Walkthrough
Walkthrough
The changes introduce a new legacy-sw configuration profile, enable modern and legacy builds for improved support and optimize service worker registration and generation processes. The modifications include updates to build scripts for defining build types, adjustments to Babel and webpack configurations, and enhanced handling of service worker files using workbox-build.
Changes
| Files | Change Summary |
|---|---|
.browserslistrc |
Added [legacy-sw] profile for service workers. |
build-scripts/bundle.cjs |
Updated build type definitions and entry points for service workers. |
build-scripts/gulp/service-worker.js |
Refactored service worker generation using node:fs/promises and workbox-build. |
build-scripts/webpack.cjs |
Modified webpack configuration to support experimental features. |
package.json |
Removed "source-map-url" and added a patch for "workbox-build". |
src/entrypoints/custom-panel.ts |
Updated conditional checks for __BUILD__ variable. |
src/entrypoints/service-worker.ts |
Refined routing logic and precacheAndRoute call for service workers. |
src/panels/config/info/ha-config-info.ts |
Displayed JavaScript type as "modern" instead of "latest". |
src/types.ts |
Updated __BUILD__ options from `"latest" |
src/util/custom-panel/load-custom-panel.ts |
Updated build conditional checks from "latest" to "modern". |
src/util/register-service-worker.ts |
Registered dynamic service worker script based on build type. |
Recent review details
Configuration used: CodeRabbit UI Review profile: ASSERTIVE
Commits
Files that changed from the base of the PR and between d9f1b06199e29db460f00fa282ecedf84c37c578 and f285c409c7bb718b4ebc059442d3cef1c50ce45c.
Files ignored due to path filters (2)
.yarn/patches/workbox-build-npm-7.1.1-a854f3faae.patchis excluded by!**/.yarn/**yarn.lockis excluded by!**/yarn.lock,!**/*.lock
Files selected for processing (11)
- .browserslistrc (1 hunks)
- build-scripts/bundle.cjs (3 hunks)
- build-scripts/gulp/service-worker.js (1 hunks)
- build-scripts/webpack.cjs (2 hunks)
- package.json (2 hunks)
- src/entrypoints/custom-panel.ts (1 hunks)
- src/entrypoints/service-worker.ts (2 hunks)
- src/panels/config/info/ha-config-info.ts (1 hunks)
- src/types.ts (1 hunks)
- src/util/custom-panel/load-custom-panel.ts (1 hunks)
- src/util/register-service-worker.ts (1 hunks)
Files not reviewed due to errors (1)
- src/util/custom-panel/load-custom-panel.ts (no review received)
Additional context used
Path-based instructions (3)
src/types.ts (4)
Pattern
*/**(html|markdown|md): - For instructional content in documentation, use a direct and authoritative tone. Avoid expressions of politeness such as 'may' or 'please', and ensure the goal of the instruction is fronted.
- Apply the Microsoft Style Guide to ensure documentation maintains clarity and conciseness.
- In step-by-step instructions, front the location phrase in the instructional sentence.
- In step-by-step instructions, front the 'goal' in the instructional sentence.
- In step-by-step instructions, if in doubt what to front, front the 'goal' before the location phrase in the instructional sentence.
- do not hyphenate terms like 'top-right' or 'bottom-left' with 'corner'
Pattern
*/**(html|markdown|md): - Use bold to mark UI strings.
- If "" are used to mark UI strings, replace them by bold.
Pattern
*/**(html|markdown|md): - Be brief in your replies and don't add fluff like "thank you for..." and "Please let me know if"
Pattern
*/**(html|markdown|md): - Use sentence-style capitalization also in headings.build-scripts/webpack.cjs (4)
Pattern
*/**(html|markdown|md): - For instructional content in documentation, use a direct and authoritative tone. Avoid expressions of politeness such as 'may' or 'please', and ensure the goal of the instruction is fronted.
- Apply the Microsoft Style Guide to ensure documentation maintains clarity and conciseness.
- In step-by-step instructions, front the location phrase in the instructional sentence.
- In step-by-step instructions, front the 'goal' in the instructional sentence.
- In step-by-step instructions, if in doubt what to front, front the 'goal' before the location phrase in the instructional sentence.
- do not hyphenate terms like 'top-right' or 'bottom-left' with 'corner'
Pattern
*/**(html|markdown|md): - Use bold to mark UI strings.
- If "" are used to mark UI strings, replace them by bold.
Pattern
*/**(html|markdown|md): - Be brief in your replies and don't add fluff like "thank you for..." and "Please let me know if"
Pattern
*/**(html|markdown|md): - Use sentence-style capitalization also in headings.build-scripts/bundle.cjs (4)
Pattern
*/**(html|markdown|md): - For instructional content in documentation, use a direct and authoritative tone. Avoid expressions of politeness such as 'may' or 'please', and ensure the goal of the instruction is fronted.
- Apply the Microsoft Style Guide to ensure documentation maintains clarity and conciseness.
- In step-by-step instructions, front the location phrase in the instructional sentence.
- In step-by-step instructions, front the 'goal' in the instructional sentence.
- In step-by-step instructions, if in doubt what to front, front the 'goal' before the location phrase in the instructional sentence.
- do not hyphenate terms like 'top-right' or 'bottom-left' with 'corner'
Pattern
*/**(html|markdown|md): - Use bold to mark UI strings.
- If "" are used to mark UI strings, replace them by bold.
Pattern
*/**(html|markdown|md): - Be brief in your replies and don't add fluff like "thank you for..." and "Please let me know if"
Pattern
*/**(html|markdown|md): - Use sentence-style capitalization also in headings.
Biome
src/util/custom-panel/load-custom-panel.ts
[error] 41-41: Forbidden non-null assertion.
(lint/style/noNonNullAssertion)
build-scripts/gulp/service-worker.js
[error] 16-24: Template literals are preferred over string concatenation.
Unsafe fix: Use a template literal.
(lint/style/useTemplate)
src/entrypoints/custom-panel.ts
[error] 94-94: Forbidden non-null assertion.
Unsafe fix: Replace with optional chain operator ?. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator
(lint/style/noNonNullAssertion)
[error] 95-95: Unexpected any. Specify a different type.
any disables many type checking rules. Its use should be avoided.
(lint/suspicious/noExplicitAny)
[error] 104-104: Forbidden non-null assertion.
(lint/style/noNonNullAssertion)
[error] 109-109: This variable implicitly has the any type.
Variable declarations without type annotation and initialization implicitly have the any type. Declare a type or initialize the variable with some value.
(lint/suspicious/noImplicitAnyLet)
[error] 114-114: Unexpected any. Specify a different type.
any disables many type checking rules. Its use should be avoided.
(lint/suspicious/noExplicitAny)
[error] 117-117: Unexpected any. Specify a different type.
any disables many type checking rules. Its use should be avoided.
(lint/suspicious/noExplicitAny)
[error] 140-140: Forbidden non-null assertion.
Unsafe fix: Replace with optional chain operator ?. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator
(lint/style/noNonNullAssertion)
src/entrypoints/service-worker.ts
[error] 88-88: Avoid the delete operator which can impact performance.
Unsafe fix: Use an undefined assignment instead.
(lint/performance/noDelete)
[error] 94-94: Avoid the delete operator which can impact performance.
Unsafe fix: Use an undefined assignment instead.
(lint/performance/noDelete)
[error] 101-101: Template literals are preferred over string concatenation.
Unsafe fix: Use a template literal.
(lint/style/useTemplate)
[error] 120-120: This variable implicitly has the any type.
Variable declarations without type annotation and initialization implicitly have the any type. Declare a type or initialize the variable with some value.
(lint/suspicious/noImplicitAnyLet)
[error] 171-171: This variable implicitly has the any type.
Variable declarations without type annotation and initialization implicitly have the any type. Declare a type or initialize the variable with some value.
(lint/suspicious/noImplicitAnyLet)
[error] 172-172: This variable implicitly has the any type.
Variable declarations without type annotation and initialization implicitly have the any type. Declare a type or initialize the variable with some value.
(lint/suspicious/noImplicitAnyLet)
src/types.ts
[error] 71-71: Unexpected any. Specify a different type.
any disables many type checking rules. Its use should be avoided.
(lint/suspicious/noExplicitAny)
[error] 71-71: Unexpected any. Specify a different type.
any disables many type checking rules. Its use should be avoided.
(lint/suspicious/noExplicitAny)
[error] 117-117: Unexpected any. Specify a different type.
any disables many type checking rules. Its use should be avoided.
(lint/suspicious/noExplicitAny)
[error] 195-195: Unexpected any. Specify a different type.
any disables many type checking rules. Its use should be avoided.
(lint/suspicious/noExplicitAny)
[error] 201-201: Unexpected any. Specify a different type.
any disables many type checking rules. Its use should be avoided.
(lint/suspicious/noExplicitAny)
[error] 253-253: Unexpected any. Specify a different type.
any disables many type checking rules. Its use should be avoided.
(lint/suspicious/noExplicitAny)
[error] 256-256: Unexpected any. Specify a different type.
any disables many type checking rules. Its use should be avoided.
(lint/suspicious/noExplicitAny)
[error] 269-269: Unexpected any. Specify a different type.
any disables many type checking rules. Its use should be avoided.
(lint/suspicious/noExplicitAny)
[error] 292-292: Unexpected any. Specify a different type.
any disables many type checking rules. Its use should be avoided.
(lint/suspicious/noExplicitAny)
[error] 292-292: Unexpected any. Specify a different type.
any disables many type checking rules. Its use should be avoided.
(lint/suspicious/noExplicitAny)
[error] 293-293: Unexpected any. Specify a different type.
any disables many type checking rules. Its use should be avoided.
(lint/suspicious/noExplicitAny)
[error] 296-296: Unexpected any. Specify a different type.
any disables many type checking rules. Its use should be avoided.
(lint/suspicious/noExplicitAny)
src/panels/config/info/ha-config-info.ts
[error] 106-106: Unexpected any. Specify a different type.
any disables many type checking rules. Its use should be avoided.
(lint/suspicious/noExplicitAny)
[error] 215-215: Unexpected any. Specify a different type.
any disables many type checking rules. Its use should be avoided.
(lint/suspicious/noExplicitAny)
[error] 217-217: Unexpected any. Specify a different type.
any disables many type checking rules. Its use should be avoided.
(lint/suspicious/noExplicitAny)
build-scripts/webpack.cjs
[error] 1-1: A Node.js builtin module should be imported with the node: protocol.
Using the node: protocol is more explicit and signals that the imported module belongs to Node.js. Unsafe fix: Add the node: protocol.
(lint/style/useNodejsImportProtocol)
[error] 2-2: A Node.js builtin module should be imported with the node: protocol.
Using the node: protocol is more explicit and signals that the imported module belongs to Node.js. Unsafe fix: Add the node: protocol.
(lint/style/useNodejsImportProtocol)
[error] 43-43: Excessive complexity detected.
Please refactor this function to reduce its complexity score from 19 to the max allowed complexity 15.
(lint/complexity/noExcessiveCognitiveComplexity)
build-scripts/bundle.cjs
[error] 1-1: A Node.js builtin module should be imported with the node: protocol.
Using the node: protocol is more explicit and signals that the imported module belongs to Node.js. Unsafe fix: Add the node: protocol.
(lint/style/useNodejsImportProtocol)
[error] 221-221: Template literals are preferred over string concatenation.
Unsafe fix: Use a template literal.
(lint/style/useTemplate)
[error] 248-248: Template literals are preferred over string concatenation.
Unsafe fix: Use a template literal.
(lint/style/useTemplate)
[error] 278-278: Template literals are preferred over string concatenation.
Unsafe fix: Use a template literal.
(lint/style/useTemplate)
[error] 292-292: Template literals are preferred over string concatenation.
Unsafe fix: Use a template literal.
(lint/style/useTemplate)
[error] 312-312: Template literals are preferred over string concatenation.
Unsafe fix: Use a template literal.
(lint/style/useTemplate)
Additional comments not posted (13)
src/util/register-service-worker.ts (1)
20-20: Approved dynamic service worker registration path.The dynamic generation of the service worker path based on the
__BUILD__variable is correctly implemented, ensuring that the appropriate service worker is registered for each build type..browserslistrc (1)
41-45: Approved addition of[legacy-sw]configuration.The addition of the
[legacy-sw]configuration to support service workers in legacy builds aligns with the PR objectives and enhances compatibility.src/entrypoints/custom-panel.ts (1)
76-76: Approved conditional loading for legacy build.The conditional loading of the ES5 adapter for the legacy build is correctly implemented, aligning with the PR objectives to handle different builds effectively.
src/entrypoints/service-worker.ts (3)
16-16: Updated manifest declaration to use TypeScript type annotations.This change improves type safety by explicitly declaring the type of
__WB_MANIFEST__, which is used in theprecacheAndRoutefunction.
22-25: Ensure correct handling of URL parameters in caching strategy.The configuration to ignore all URL parameters during caching is a good practice for service workers, as it prevents cache bloating and ensures that resources are fetched efficiently.
57-58: Network-only strategy for manifest and onboarding pages.Using a
NetworkOnlystrategy for critical resources like the manifest and onboarding pages ensures they are always served with the most up-to-date version, which is crucial for proper app functioning.src/types.ts (1)
24-24: Updated global variable declarations to reflect new build configurations.The updated global variable
__BUILD__to use "modern" and "legacy" improves clarity and aligns with the project's new terminology.src/panels/config/info/ha-config-info.ts (1)
160-160: Updated JavaScript version display logic to include build type.This change ensures that the UI correctly reflects whether the "modern" or "legacy" build is being used, which is important for transparency and troubleshooting.
build-scripts/webpack.cjs (1)
66-78: Refactored babel loader configuration to handle service workers.This change allows for specific babel configurations based on whether the module is a service worker, enhancing flexibility and maintainability of the build process.
package.json (1)
246-246: Approval of the patchedworkbox-builddependency.The addition of the patched version of
workbox-buildis crucial for addressing the issue with source maps, as highlighted in the PR summary. This ensures that service workers function correctly across different build environments.build-scripts/bundle.cjs (3)
223-229: Approved: Conditional configuration for service worker entry.This setup ensures that the correct service worker script is used based on the build environment and tooling. It's a crucial part of supporting separate service workers for modern and legacy builds.
95-95: Approved: Dynamic setting ofbrowserslistEnv.This dynamic approach allows for more targeted builds depending on the environment and presence of a service worker. Verify that the
browserslistEnvsettings are correctly utilized in all relevant build scripts and testing environments.Verification successful
Approved: Dynamic setting of
browserslistEnv.The dynamic approach to setting
browserslistEnvis correctly implemented and utilized in thelist-plugins-and-polyfills.jsfile. This ensures targeted builds based on the environment and presence of a service worker.
build-scripts/list-plugins-and-polyfills.js: Correct usage ofbrowserslistEnvconfirmed.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the correct utilization of `browserslistEnv`. # Test: Check for the usage of `browserslistEnv` in build scripts. Expect: Correct settings for each scenario. rg --type all "browserslistEnv"Length of output: 383
Script:
#!/bin/bash # Description: Extract surrounding lines to verify the usage of `browserslistEnv` in the relevant file. # Extract 5 lines before and after each occurrence of `browserslistEnv` for context. rg --context 5 "browserslistEnv" build-scripts/list-plugins-and-polyfills.jsLength of output: 1091
50-50: Approved: Update to global variable__BUILD__.The update aligns with the PR's objective to differentiate between modern and legacy builds. Ensure that all references to
__BUILD__throughout the codebase are updated accordingly.Verification successful
Approved: Update to global variable
__BUILD__.The update aligns with the PR's objective to differentiate between modern and legacy builds. The search results indicate that all instances of
__BUILD__in the codebase are consistent with the new logic.
src/types.ts: Definition of__BUILD__.src/entrypoints/custom-panel.ts: Conditional check for__BUILD__.src/state/logging-mixin.ts: Usage in a template literal.src/util/register-service-worker.ts: Usage in a service worker registration.src/panels/config/info/ha-config-info.ts: Assignment to a constant.src/util/custom-panel/load-custom-panel.ts: Conditional check for__BUILD__.build-scripts/bundle.cjs: The line under review.All instances of
__BUILD__have been updated accordingly.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify all instances of `__BUILD__` are updated. # Test: Search for old instances of `__BUILD__`. Expect: No occurrences of old values. rg --type python "__BUILD__ === 'es5'"Length of output: 77
Script:
#!/bin/bash # Description: Verify all instances of `__BUILD__` are updated. # Search for all instances of `__BUILD__` in JavaScript files rg "__BUILD__" --type jsLength of output: 24
Script:
#!/bin/bash # Description: Verify all instances of `__BUILD__` are updated. # Search for all instances of `__BUILD__` across all file types rg "__BUILD__"Length of output: 562
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?
Tips
Chat
There are 3 ways to chat with CodeRabbit:
- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
I pushed a fix in commit <commit_id>.Generate unit testing code for this file.Open a follow-up GitHub issue for this discussion.
- Files and specific lines of code (under the "Files changed" tab): Tag
@coderabbitaiin a new review comment at the desired location with your query. Examples:@coderabbitai generate unit testing code for this file.@coderabbitai modularize this function.
- PR comments: Tag
@coderabbitaiin a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:@coderabbitai generate interesting stats about this repository and render them as a table.@coderabbitai show all the console.log statements in this repository.@coderabbitai read src/utils.ts and generate unit testing code.@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.@coderabbitai help me debug CodeRabbit configuration file.
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.
CodeRabbit Commands (invoked as PR comments)
@coderabbitai pauseto pause the reviews on a PR.@coderabbitai resumeto resume the paused reviews.@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository.@coderabbitai full reviewto do a full review from scratch and review all the files again.@coderabbitai summaryto regenerate the summary of the PR.@coderabbitai resolveresolve all the CodeRabbit review comments.@coderabbitai configurationto show the current CodeRabbit configuration for the repository.@coderabbitai helpto get help.
Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
CodeRabbit Configration File (.coderabbit.yaml)
- You can programmatically configure CodeRabbit by adding a
.coderabbit.yamlfile to the root of your repository. - Please see the configuration documentation for more information.
- If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation:
# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
Documentation and Community
- Visit our Documentation for detailed information on how to use CodeRabbit.
- Join our Discord Community to get help, request features, and share feedback.
- Follow us on X/Twitter for updates and announcements.
If this is considered as a "breaking change", what is expected to be done by the user?
It's not breaking... I checked the wrong box.
Core PR has been approved.