Skip to content

Commit 38db5c6

Browse files
msutkowskikettanaito
authored andcommitted
Make keepAlive an option, add a log when quiet:false
1 parent 2b3ba63 commit 38db5c6

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
lines changed

src/mockServiceWorker.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ self.addEventListener('message', async function (event) {
2727
const allClientIds = allClients.map((client) => client.id)
2828

2929
switch (event.data) {
30-
case 'PING': {
30+
case 'KEEPALIVE_REQUEST': {
3131
sendToClient(client, {
32-
type: 'PONG',
32+
type: 'KEEPALIVE_RESPONSE',
3333
payload: new Date(),
3434
})
3535
break

src/setupWorker/glossary.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface SetupWorkerInternalContext {
1111
worker: ServiceWorker | null
1212
registration: ServiceWorkerRegistration | null
1313
requestHandlers: RequestHandler<any, any>[]
14-
keepAliveInterval?: ReturnType<typeof setInterval> | number
14+
keepAliveInterval?: number
1515
events: {
1616
/**
1717
* Adds an event listener on the given target.
@@ -66,6 +66,12 @@ export type StartOptions = SharedOptions & {
6666
* of all registered Service Workers on the page.
6767
*/
6868
findWorker?: FindWorker
69+
70+
/**
71+
* Disable the keep alive or use a custom interval.
72+
* Defaults to 10000 (ms)
73+
*/
74+
keepAlive?: false | number
6975
}
7076

7177
export type RequestHandlersList = RequestHandler<any, any>[]

src/setupWorker/start/createStart.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const DEFAULT_START_OPTIONS: DeepRequired<StartOptions> = {
1717
options: null as any,
1818
},
1919
quiet: false,
20+
keepAlive: 10000,
2021
waitUntilReady: true,
2122
onUnhandledRequest: 'bypass',
2223
findWorker: (scriptURL, mockServiceWorkerUrl) =>
@@ -94,6 +95,9 @@ Please consider using a custom "serviceWorker.url" option to point to the actual
9495
// the Service Worker when there are no open clients.
9596
worker.postMessage('CLIENT_CLOSED')
9697
}
98+
// Make sure we're always clearing the interval - there are reports that not doing this can
99+
// cause memory leaks in headless browser environments.
100+
window.clearInterval(context.keepAliveInterval)
97101
})
98102

99103
// Check if the active Service Worker is the latest published one
@@ -124,10 +128,12 @@ If this message still persists after updating, please report an issue: https://g
124128
return null
125129
}
126130

127-
context.keepAliveInterval = setInterval(
128-
() => worker.postMessage('PING'),
129-
5000,
130-
)
131+
if (resolvedOptions.keepAlive) {
132+
context.keepAliveInterval = window.setInterval(
133+
() => worker.postMessage('KEEPALIVE_REQUEST'),
134+
resolvedOptions.keepAlive,
135+
)
136+
}
131137

132138
return registration
133139
}

src/setupWorker/stop/createStop.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ export const createStop = (context: SetupWorkerInternalContext) => {
99
return function stop() {
1010
context.worker?.postMessage('MOCK_DEACTIVATE')
1111
context.events.removeAllListeners()
12-
clearInterval(context.keepAliveInterval as any)
12+
window.clearInterval(context.keepAliveInterval)
1313
}
1414
}

src/utils/handleRequestWith.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ export const handleRequestWith = (
5757

5858
// Ignore irrelevant worker message types
5959
if (type !== 'REQUEST') {
60+
if (type === 'KEEPALIVE_RESPONSE' && !options.quiet) {
61+
console.info(
62+
`[MSW] Received Keep Alive response from the mockServiceWorker`,
63+
)
64+
}
65+
6066
return null
6167
}
6268

0 commit comments

Comments
 (0)