Title: Add ability to set URL for ngsw.json at runtime · Issue #51457 · angular/angular · GitHub
Open Graph Title: Add ability to set URL for ngsw.json at runtime · Issue #51457 · angular/angular
X Title: Add ability to set URL for ngsw.json at runtime · Issue #51457 · angular/angular
Description: Which @angular/* package(s) are relevant/related to the feature request? service-worker Description TL;DR: We cannot serve ngsw-worker.js and ngsw.json from the same origin, so we'd like a way to configure where ngsw.json is fetched from...
Open Graph Description: Which @angular/* package(s) are relevant/related to the feature request? service-worker Description TL;DR: We cannot serve ngsw-worker.js and ngsw.json from the same origin, so we'd like a way to c...
X Description: Which @angular/* package(s) are relevant/related to the feature request? service-worker Description TL;DR: We cannot serve ngsw-worker.js and ngsw.json from the same origin, so we'd like a way ...
Opengraph URL: https://github.com/angular/angular/issues/51457
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add ability to set URL for ngsw.json at runtime","articleBody":"### Which @angular/* package(s) are relevant/related to the feature request?\r\n\r\nservice-worker\r\n\r\n### Description\r\n\r\nTL;DR: We cannot serve `ngsw-worker.js` and `ngsw.json` from the same origin, so we'd like a way to configure where `ngsw.json` is fetched from.\r\n\r\nOur setup requires us to serve the application (i.e files produced by `ng build`) from an external CDN. The CDN URL and path cannot be known at build-time, and is set by the server when the index file is rendered (e.g with [Thymeleaf](https://www.thymeleaf.org)). Think:\r\n\r\n```html\r\n// index.html\r\n\u003cscript>\r\n window.APP_CONFIG = {\r\n cdnUrl: [[${CDN_URL}]]\r\n };\r\n\u003c/script>\r\n\r\n\u003cscript th:src=\"@{${CDN_URL}+'polyfills.js'}\">\u003c/script>\r\n\u003cscript th:src=\"@{${CDN_URL}+'runtime.js'}\">\u003c/script>\r\n\u003cscript th:src=\"@{${CDN_URL}+'main.js'}\">\u003c/script>\r\n```\r\n\r\nWhen trying to add `@angular/service-worker` for offline support into the mix, we run into the issue that the `ngsw-worker.js` script cannot be served from the CDN like other files, because of browser restrictions.\r\n\r\nWe *can* work around this by having the `ngsw-worker.js` be served from the page origin. However, we'd still like for the `ngsw.json` manifest to be served together with the rest of the app from the CDN. And this is the issue. There is no way to tell the service worker to fetch the manifest from a different path/url.\r\n\r\n### Proposed solution\r\n\r\nAdd a *runtime* config property that allows to override the default `ngsw.json` path. For example: \r\n\r\n```typescript\r\nServiceWorkerModule.register(\"ngsw-worker.js\", {\r\n\t// ...\r\n\tmanifestUrl: `${window.APP_CONFIG.CDN_URL}/ngsw.json`,\r\n}),\r\n```\r\n\r\nNote that for our setup, the CDN URL is not known at build-time, so it cannot be a part of `ngsw-config.json`.\r\n\r\nEDIT:\r\nAnother possible solution would be something like:\r\n```typescript\r\nServiceWorkerModule.register(\"ngsw-worker.js\", {\r\n\t// ...\r\n\tngswOrigin: \"sw-scope\" | \"import-meta\",\r\n}),\r\n```\r\n...where `\"sw-scope\"` would be default (and the way it works right now), and `\"import-meta\"` would become a URL that is based on where the `main.js` script is loaded from. Possibly based on `import.meta.url`.\r\n\r\nEDIT2:\r\nI forgot to add: If the `ngsw.json` is served from an external URL, then there must also be a way to tell the service worker that the URL:s inside of it are relative. Like so:\r\n\r\n```json\r\n// https://the.cdn/path/to/ngsw.json\r\n{\r\n \"assetGroups\": [\r\n {\r\n \"urls\": [\r\n \"main.js\" // Service Worker should interpret this as https://the.cdn/path/to/main.js\r\n ],\r\n }\r\n ],\r\n}\r\n```\r\n\r\n### Alternatives considered\r\n\r\nOne alternative we tried was to serve `ngsw.json` from the page origin (i.e the same as `ngsw-worker.json`).\r\n\r\nHowever this is not ideal, as the CDN files are deployed more or less independently from the index page.","author":{"url":"https://github.com/anhallbe","@type":"Person","name":"anhallbe"},"datePublished":"2023-08-22T07:06:08Z","url":"https://github.com/angular/angular/issues/51457"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:ca797ba5-0875-44d3-074c-2a23bce0911b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D65C:1CEA6F:FF8604:1525756:6B010864 |
| html-safe-nonce | 6671e0bcd6660ed9d56fed745c046fa80ebe421a2600eded9f774ca6d5525d6e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENjVDOjFDRUE2RjpGRjg2MDQ6MTUyNTc1Njo2QjAxMDg2NCIsInZpc2l0b3JfaWQiOiI3ODU4NTY1ODczMDQ3NTA0OTk2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 4955aada6d1cf410b3446cf3d3c51de96f9ce6f5063fc60524ff9be1896b4626 |
| hovercard-subject-tag | issue:1860752854 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/51457/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c258373adfcf22b6066d6fd08b9ec04fff4cff440f3bb99d1a0480ca2938644a/angular/angular/issues/51457 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c258373adfcf22b6066d6fd08b9ec04fff4cff440f3bb99d1a0480ca2938644a/angular/angular/issues/51457 |
| og:image:alt | Which @angular/* package(s) are relevant/related to the feature request? service-worker Description TL;DR: We cannot serve ngsw-worker.js and ngsw.json from the same origin, so we'd like a way to c... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | anhallbe |
| hostname | github.com |
| expected-hostname | github.com |
| None | cb7fa109eca7d4156c889f474306b0e91336055e9adf8fd4684b61e49094d124 |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 2b59d31a4e44f08b7af67a4b67f8fd0af3befe2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width