Skip to content

Commit 742122d

Browse files
alan-agius4AndrewKushnir
authored andcommitted
docs: use markdown code fences in service worker docs
Replaces the <docs-code> component with standard markdown code fences in the service worker documentation. This improves the readability and maintainability of the documentation. (cherry picked from commit d42f9ce)
1 parent 2ad6b72 commit 742122d

File tree

2 files changed

+78
-91
lines changed

2 files changed

+78
-91
lines changed

adev/src/content/ecosystem/service-workers/custom-service-worker-scripts.md

Lines changed: 47 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,91 +8,83 @@ To create a custom service worker that extends Angular's functionality:
88

99
1. Create a custom service worker file (e.g., `custom-sw.js`) in your `src` directory:
1010

11-
<docs-code language="javascript">
12-
11+
```js
1312
// Import the Angular service worker
1413
importScripts('./ngsw-worker.js');
1514

1615
(function () {
17-
'use strict';
16+
'use strict';
1817

19-
// Add custom notification click handler
20-
self.addEventListener('notificationclick', (event) => {
21-
console.log('Custom notification click handler');
22-
console.log('Notification details:', event.notification);
18+
// Add custom notification click handler
19+
self.addEventListener('notificationclick', (event) => {
20+
console.log('Custom notification click handler');
21+
console.log('Notification details:', event.notification);
2322

2423
// Handle notification click - open URL if provided
2524
if (clients.openWindow && event.notification.data.url) {
2625
event.waitUntil(clients.openWindow(event.notification.data.url));
2726
console.log('Opening URL:', event.notification.data.url);
2827
}
28+
});
2929

30-
});
31-
32-
// Add custom background sync handler
33-
self.addEventListener('sync', (event) => {
34-
console.log('Custom background sync handler');
30+
// Add custom background sync handler
31+
self.addEventListener('sync', (event) => {
32+
console.log('Custom background sync handler');
3533

3634
if (event.tag === 'background-sync') {
3735
event.waitUntil(doBackgroundSync());
3836
}
39-
40-
});
41-
42-
function doBackgroundSync() {
43-
// Implement your background sync logic here
44-
return fetch('https://example.com/api/sync')
45-
.then(response => response.json())
46-
.then(data => console.log('Background sync completed:', data))
47-
.catch(error => console.error('Background sync failed:', error));
48-
}
37+
});
38+
39+
function doBackgroundSync() {
40+
// Implement your background sync logic here
41+
return fetch('https://example.com/api/sync')
42+
.then((response) => response.json())
43+
.then((data) => console.log('Background sync completed:', data))
44+
.catch((error) => console.error('Background sync failed:', error));
45+
}
4946
})();
50-
51-
</docs-code>
47+
```
5248

5349
2. Update your `angular.json` file to use the custom service worker:
5450

55-
<docs-code language="json">
56-
57-
{
58-
"projects": {
59-
"your-app": {
60-
"architect": {
61-
"build": {
62-
"options": {
63-
"assets": [
51+
```json
6452
{
65-
"glob": "**/*",
66-
"input": "public"
67-
},
68-
"app/src/custom-sw.js"
69-
]
70-
},
71-
}
72-
}
73-
}
74-
}
53+
"projects": {
54+
"your-app": {
55+
"architect": {
56+
"build": {
57+
"options": {
58+
"assets": [
59+
{
60+
"glob": "**/*",
61+
"input": "public"
62+
},
63+
"app/src/custom-sw.js"
64+
]
65+
}
66+
}
67+
}
68+
}
69+
}
7570
}
76-
77-
</docs-code>
71+
```
7872

7973
3. Configure the service worker registration to use your custom script:
8074

81-
<docs-code language="typescript">
82-
75+
```ts
8376
import { ApplicationConfig, isDevMode } from '@angular/core';
8477
import { provideServiceWorker } from '@angular/service-worker';
8578

8679
export const appConfig: ApplicationConfig = {
87-
providers: [
88-
provideServiceWorker('custom-sw.js', {
89-
enabled: !isDevMode(),
90-
registrationStrategy: 'registerWhenStable:30000'
91-
}),
92-
],
80+
providers: [
81+
provideServiceWorker('custom-sw.js', {
82+
enabled: !isDevMode(),
83+
registrationStrategy: 'registerWhenStable:30000',
84+
}),
85+
],
9386
};
94-
95-
</docs-code>
87+
```
9688

9789
### Best practices for custom service workers
9890

adev/src/content/ecosystem/service-workers/push-notifications.md

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,18 @@ The default behavior for the `notificationclick` event is to close the notificat
2323
You can specify an additional operation to be executed on `notificationclick` by adding an `onActionClick` property to the `data` object, and providing a `default` entry.
2424
This is especially useful for when there are no open clients when a notification is clicked.
2525

26-
<docs-code language="json">
27-
26+
```json
2827
{
29-
"notification": {
30-
"title": "New Notification!",
31-
"data": {
32-
"onActionClick": {
33-
"default": {"operation": "openWindow", "url": "foo"}
34-
}
35-
}
28+
"notification": {
29+
"title": "New Notification!",
30+
"data": {
31+
"onActionClick": {
32+
"default": {"operation": "openWindow", "url": "foo"}
33+
}
34+
}
35+
}
3636
}
37-
}
38-
39-
</docs-code>
37+
```
4038

4139
### Operations
4240

@@ -60,31 +58,29 @@ Each action is represented as an action button that the user can click to intera
6058

6159
In addition, using the `onActionClick` property on the `data` object, you can tie each action to an operation to be performed when the corresponding action button is clicked:
6260

63-
<docs-code language="typescript">
64-
61+
```json
6562
{
66-
"notification": {
67-
"title": "New Notification!",
68-
"actions": [
69-
{"action": "foo", "title": "Open new tab"},
70-
{"action": "bar", "title": "Focus last"},
71-
{"action": "baz", "title": "Navigate last"},
72-
{"action": "qux", "title": "Send request in the background"},
73-
{"action": "other", "title": "Just notify existing clients"}
74-
],
75-
"data": {
76-
"onActionClick": {
77-
"default": {"operation": "openWindow"},
78-
"foo": {"operation": "openWindow", "url": "/absolute/path"},
79-
"bar": {"operation": "focusLastFocusedOrOpen", "url": "relative/path"},
80-
"baz": {"operation": "navigateLastFocusedOrOpen", "url": "https://other.domain.com/"},
81-
"qux": {"operation": "sendRequest", "url": "https://yet.another.domain.com/"}
82-
}
83-
}
84-
}
63+
"notification": {
64+
"title": "New Notification!",
65+
"actions": [
66+
{"action": "foo", "title": "Open new tab"},
67+
{"action": "bar", "title": "Focus last"},
68+
{"action": "baz", "title": "Navigate last"},
69+
{"action": "qux", "title": "Send request in the background"},
70+
{"action": "other", "title": "Just notify existing clients"}
71+
],
72+
"data": {
73+
"onActionClick": {
74+
"default": {"operation": "openWindow"},
75+
"foo": {"operation": "openWindow", "url": "/absolute/path"},
76+
"bar": {"operation": "focusLastFocusedOrOpen", "url": "relative/path"},
77+
"baz": {"operation": "navigateLastFocusedOrOpen", "url": "https://other.domain.com/"},
78+
"qux": {"operation": "sendRequest", "url": "https://yet.another.domain.com/"}
79+
}
80+
}
81+
}
8582
}
86-
87-
</docs-code>
83+
```
8884

8985
IMPORTANT: If an action does not have a corresponding `onActionClick` entry, then the notification is closed and `SwPush.notificationClicks` is notified on existing clients.
9086

@@ -93,7 +89,6 @@ IMPORTANT: If an action does not have a corresponding `onActionClick` entry, the
9389
You might also be interested in the following:
9490

9591
<docs-pill-row>
96-
9792
<docs-pill href="ecosystem/service-workers/communications" title="Communicating with the Service Worker"/>
9893
<docs-pill href="ecosystem/service-workers/devops" title="Service Worker devops"/>
9994
</docs-pill-row>

0 commit comments

Comments
 (0)