Skip to content

Commit

Permalink
Amp install sw custom scope (ampproject#19915)
Browse files Browse the repository at this point in the history
* fixing method call for esm build

* custom scope for amp-install-sw
  • Loading branch information
prateekbh authored and Noran Azmy committed Mar 22, 2019
1 parent 657cd7d commit cc2a321
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
Expand Up @@ -297,7 +297,10 @@ class UrlRewriter_ {
* @return {!Promise<!ServiceWorkerRegistration|undefined>}
*/
function install(win, src, element) {
return win.navigator.serviceWorker.register(src).then(function(registration) {
const scope = element.getAttribute('data-scope') || '/';
return win.navigator.serviceWorker.register(src, {
scope,
}).then(function(registration) {
if (getMode().development) {
user().info(TAG, 'ServiceWorker registration successful with scope: ',
registration.scope);
Expand Down
Expand Up @@ -84,8 +84,51 @@ describes.realWin('amp-install-serviceworker', {
},
navigator: {
serviceWorker: {
register: src => {
register: (src, options) => {
expect(calledSrc).to.be.undefined;
expect(options.scope).to.be.equal('/');
calledSrc = src;
return p;
},
},
},
};
whenVisible = Promise.resolve();
registerServiceBuilder(implementation.win, 'viewer', function() {
return {
whenFirstVisible: () => whenVisible,
isVisible: () => true,
};
});
implementation.buildCallback();
expect(calledSrc).to.be.undefined;
return Promise.all([whenVisible, loadPromise(implementation.win)]).then(
() => {
expect(calledSrc).to.equal('https://example.com/sw.js');
// Should not be called before `register` resolves.
expect(maybeInstallUrlRewriteStub).to.not.be.called;
});
});

it('should install for custom scope', () => {
const install = doc.createElement('div');
container.appendChild(install);
install.getAmpDoc = () => ampdoc;
install.setAttribute('src', 'https://example.com/sw.js');
install.setAttribute('data-scope', '/profile');
const implementation = new AmpInstallServiceWorker(install);
let calledSrc;
const p = new Promise(() => {});
implementation.win = {
complete: true,
location: {
href: 'https://example.com/some/path',
},
navigator: {
serviceWorker: {
register: (src, options) => {
expect(calledSrc).to.be.undefined;
expect(options.scope).to.be.equal('/profile');
calledSrc = src;
return p;
},
Expand Down

0 comments on commit cc2a321

Please sign in to comment.