-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implementing forced nullification #25
Changes from 6 commits
bc2dd33
a2d3b05
5611ec0
ae563bc
3e2e8a5
942033d
ad85749
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ export async function buildSW( | |
documentCachingOptions: {}, | ||
}, | ||
importFrom: string = 'https://cdn.ampproject.org/amp-sw.js', | ||
forcedNullifcation: boolean = false, | ||
) { | ||
let code = ''; | ||
if (config.offlinePageOptions && config.offlinePageOptions.url) { | ||
|
@@ -32,6 +33,9 @@ export async function buildSW( | |
); | ||
} | ||
code = `importScripts('${importFrom}')\n`; | ||
code += `AMP_SW.init(${serializeObject(config || {})})`; | ||
return code; | ||
if (forcedNullifcation) { | ||
return code + 'AMP_SW.forcedNullifcation()'; | ||
} else { | ||
return code + `AMP_SW.init(${serializeObject(config || {})})`; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can just return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's move this all to testing directories, since it's purely for test cases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do this in a separate PR, issue opened #30 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const UNVERSIONED_CACHE_NAME = 'AMP-UNVERSIONED-CACHE'; | ||
export const VERSIONED_CACHE_NAME = 'AMP-VERSIONED-CACHE'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,10 +19,12 @@ import { ServiceWorkerConfiguration } from '../../configuration'; | |
import { AssetCachingOptions } from '../asset-caching'; | ||
import { LinkPrefetchOptions } from '../link-prefetch'; | ||
import { OfflinePageOptions } from '../offline-page'; | ||
|
||
declare global { | ||
interface WorkerGlobalScope { | ||
AMP_SW: { | ||
init: Function; | ||
forcedNullifcation: () => void; | ||
}; | ||
} | ||
} | ||
|
@@ -96,7 +98,16 @@ function init(config: ServiceWorkerConfiguration = {}) { | |
}); | ||
} | ||
|
||
function forcedNullifcation() { | ||
import(/* webpackChunkName: "service-worker-remover" */ '../service-worker-remover/index').then( | ||
async ({ ServiceWorkerRemover }) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. async ({ ServiceWorkerRemover }) => {
new ServiceWorkerRemover().installNoOpServiceWorker();
}, Can be rewritten as... async ({ ServiceWorkerRemover }) => new ServiceWorkerRemover().installNoOpServiceWorker(), There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are you aware of a prettier rule for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. manually fixing this for now |
||
new ServiceWorkerRemover().installNoOpServiceWorker(); | ||
}, | ||
); | ||
} | ||
|
||
// Initialize AMP_SW namespace | ||
self['AMP_SW'] = { | ||
init, | ||
forcedNullifcation, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const AMP_PREFETCHED_LINKS: string = 'AMP-PREFETCHED-LINKS'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* Copyright 2019 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { | ||
VERSIONED_CACHE_NAME, | ||
UNVERSIONED_CACHE_NAME, | ||
} from '../amp-caching/constants'; | ||
import { AMP_ASSET_CACHE } from '../asset-caching/constants'; | ||
import { AMP_PUBLISHER_CACHE } from '../document-caching/constants'; | ||
import { AMP_PREFETCHED_LINKS } from '../link-prefetch/constants'; | ||
|
||
export class ServiceWorkerRemover { | ||
async installNoOpServiceWorker() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's verify the first version of Safari supporting Service Workers, there is a bug in Safari 10.1 that would preclude an async member from working as expected. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 11.2 |
||
// Taking over the document | ||
self.addEventListener('install', function(e: ExtendableEvent) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should these be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This memory is freed once the service worker is terminated, and post that service worker execution is just event based so i guess this should matter. |
||
const { skipWaiting } = self as ServiceWorkerGlobalScope; | ||
e.waitUntil(skipWaiting()); | ||
}); | ||
|
||
self.addEventListener('activate', async (e: ExtendableEvent) => { | ||
const { clients } = self as ServiceWorkerGlobalScope; | ||
e.waitUntil( | ||
Promise.all([ | ||
this.cleanCacheStorage(), | ||
this.forceRefreshClients(clients), | ||
]), | ||
); | ||
}); | ||
} | ||
|
||
async cleanCacheStorage() { | ||
return Promise.all([ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we build a test that verifies that an exception thrown in these methods doesn't break the SW? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit tough to do in the current e2e test setup due to the following reasons
Instead I can test this with a unit test case instead. I'll setup a unit test setup and add a unit tests around |
||
caches.delete(VERSIONED_CACHE_NAME), | ||
caches.delete(UNVERSIONED_CACHE_NAME), | ||
caches.delete(AMP_ASSET_CACHE), | ||
caches.delete(AMP_PUBLISHER_CACHE), | ||
caches.delete(AMP_PREFETCHED_LINKS), | ||
]); | ||
} | ||
|
||
forceRefreshClients(clients: Clients) { | ||
return clients.claim().then(async () => { | ||
// Cache current document if its AMP. | ||
const windowClients = await clients.matchAll({ type: 'window' }); | ||
windowClients.forEach((client: WindowClient) => | ||
client.navigate(client.url), | ||
); | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"ampRuntimeVersion":"011905140117570","ampCssUrl":"https://cdn.ampproject.org/rtv/011905140117570/v0.css","canaryPercentage":"0.015","diversions":["001905222334000","031905222334000","021905140117570"]} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just curious, how is this more readable than what is there today?