Skip to content

Commit 1d38d73

Browse files
committed
Fixed testcases
1 parent b68eddb commit 1d38d73

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

src/app/hooks/useServiceWorkerRegistration/index.test.tsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ describe('useServiceWorkerRegistration', () => {
3232
it('should register service worker with correct path when service is provided', () => {
3333
renderHook(() => useServiceWorkerRegistration('mundo'));
3434

35-
expect(mockRegister).toHaveBeenCalledWith('/mundo/sw.js');
35+
expect(mockRegister).toHaveBeenCalledWith('/mundo/sw.js', {
36+
scope: '/mundo',
37+
});
3638
});
3739

3840
it('should register service worker for different services', () => {
@@ -43,12 +45,16 @@ describe('useServiceWorkerRegistration', () => {
4345
},
4446
);
4547

46-
expect(mockRegister).toHaveBeenCalledWith('/news/sw.js');
48+
expect(mockRegister).toHaveBeenCalledWith('/news/sw.js', {
49+
scope: '/news',
50+
});
4751

4852
mockRegister.mockClear();
4953
rerender({ service: 'sport' });
5054

51-
expect(mockRegister).toHaveBeenCalledWith('/sport/sw.js');
55+
expect(mockRegister).toHaveBeenCalledWith('/sport/sw.js', {
56+
scope: '/sport',
57+
});
5258
});
5359

5460
it('should not register service worker when service is undefined', () => {
@@ -124,13 +130,17 @@ describe('useServiceWorkerRegistration', () => {
124130
);
125131

126132
expect(mockRegister).toHaveBeenCalledTimes(1);
127-
expect(mockRegister).toHaveBeenCalledWith('/mundo/sw.js');
133+
expect(mockRegister).toHaveBeenCalledWith('/mundo/sw.js', {
134+
scope: '/mundo',
135+
});
128136

129137
mockRegister.mockClear();
130138
rerender({ service: 'news' });
131139

132140
expect(mockRegister).toHaveBeenCalledTimes(1);
133-
expect(mockRegister).toHaveBeenCalledWith('/news/sw.js');
141+
expect(mockRegister).toHaveBeenCalledWith('/news/sw.js', {
142+
scope: '/news',
143+
});
134144
});
135145

136146
it('should not register again when service prop stays the same', () => {
@@ -159,7 +169,9 @@ describe('useServiceWorkerRegistration', () => {
159169

160170
renderHook(() => useServiceWorkerRegistration('mundo'));
161171

162-
expect(mockRegister).toHaveBeenCalledWith('/mundo/sw.js');
172+
expect(mockRegister).toHaveBeenCalledWith('/mundo/sw.js', {
173+
scope: '/mundo',
174+
});
163175

164176
await Promise.resolve();
165177

src/app/hooks/useServiceWorkerRegistration/index.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ const useServiceWorkerRegistration = (service?: string) => {
2020
onClient() && 'serviceWorker' in navigator;
2121

2222
if (shouldInstallServiceWorker) {
23-
// TODO: scope option to be used once Service-Worker-Allowed header is whitelisted
23+
// Scope option to ensure the service worker only controls pages under /{service}
2424
const result = sw.register(`/${service}/sw.js`, {
2525
scope: `/${service}`,
2626
});
2727

28-
// const result = sw.register(`/${service}/sw.js`);
29-
3028
Promise.resolve(result).catch(err => {
3129
// eslint-disable-next-line no-console
3230
console.error('Service worker registration failed:', err);

ws-nextjs-app/next.config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ module.exports = {
2424
},
2525
],
2626
},
27-
//* *TODO: */ Service worker headers — commented out, as this will be handled as part of future work in this ticket https://bbc.atlassian.net/browse/WS-2004.
2827

28+
// Service worker headers to allow scope to be set correctly */
2929
{
3030
source: '/:service/sw.js',
3131
headers: [
@@ -35,7 +35,8 @@ module.exports = {
3535
},
3636
{
3737
key: 'Cache-Control',
38-
value: 'public, max-age=0, must-revalidate',
38+
value:
39+
'public, stale-if-error=6000, stale-while-revalidate=600, max-age=300',
3940
},
4041
{ key: 'Content-Type', value: 'application/javascript' },
4142
],

0 commit comments

Comments
 (0)