Skip to content
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

Allow reloadExtension() to soft fail #29042

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/service/extensions-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
calculateExtensionScriptUrl,
parseExtensionUrl,
} from './extension-location';
import {dev, devAssert, rethrowAsync} from '../log';
import {dev, devAssert, rethrowAsync, user} from '../log';
import {getMode} from '../mode';
import {installStylesForDoc} from '../style-installer';
import {map} from '../utils/object';
Expand Down Expand Up @@ -239,19 +239,22 @@ export class Extensions {
/**
* Reloads the new version of the extension.
* @param {string} extensionId
* @return {!Promise<!ExtensionDef>}
*/
reloadExtension(extensionId) {
// Ignore inserted script elements to prevent recursion.
const els = this.getExtensionScripts_(
extensionId,
/* includeInserted */ false
);
devAssert(
els.length > 0,
'Cannot find script for extension: %s',
extensionId
);
if (!els.length) {
const TAG = 'reloadExtension';
user().warn(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this an error

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's already a different error for this: Error: amp-fit-text is already registered. The script tag for amp-fit-text is likely included twice in the page.

Happens in custom-element-registry.js when we try to register a CE twice.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That happens too late, though, since any side-effects in the code can already execute. I think we need an earlier check in runtime.js when we're processing the pushed ExtensionPayload.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right and it doesn't cover non-CE extensions. I guess we have a similar check for templates. #29050

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This got off topic from the request. Let's make this an error, not a warn.

We can solve the escape hatch in a follow up.

TAG,
'Extension script for "%s" is missing or was already reloaded.',
extensionId
);
return;
}
// The previously awaited extension loader must not have finished or
// failed.
const holder = this.extensions_[extensionId];
Expand All @@ -263,7 +266,7 @@ export class Extensions {
el.setAttribute('i-amphtml-loaded-new-version', extensionId)
);
const urlParts = parseExtensionUrl(els[0].src);
return this.preloadExtension(extensionId, urlParts.extensionVersion);
this.preloadExtension(extensionId, urlParts.extensionVersion);
}

/**
Expand Down
38 changes: 26 additions & 12 deletions test/unit/test-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {Extensions} from '../../src/service/extensions-impl';
import {Services} from '../../src/services';
import {getServiceForDoc} from '../../src/service';
import {installTimerService} from '../../src/service/timer-impl';
import {user} from '../../src/log';

class AmpTest extends BaseElement {}
class AmpTestSub extends BaseElement {}
Expand Down Expand Up @@ -643,14 +644,18 @@ describes.sandboxed('Extensions', {}, () => {
env.sandbox.stub(Services, 'ampdocServiceFor').returns(null);
extensions = new Extensions(win);
env.sandbox.stub(extensions, 'preloadExtension');
env.sandbox.stub(user(), 'warn');
});

describe('regular scripts', () => {
it('should devAssert if script cannot be found', () => {
expect(() => {
allowConsoleError(() => extensions.reloadExtension('amp-list'));
}).to.throw('Cannot find script for extension: amp-list');
extensions.reloadExtension('amp-list');

expect(user().warn).to.be.calledWith(
'reloadExtension',
'Extension script for "%s" is missing or was already reloaded.',
'amp-list'
);
expect(extensions.preloadExtension).to.not.be.called;
});

Expand All @@ -664,10 +669,13 @@ describes.sandboxed('Extensions', {}, () => {
list.setAttribute('i-amphtml-inserted', '');
win.document.head.appendChild(list);

expect(() => {
allowConsoleError(() => extensions.reloadExtension('amp-list'));
}).to.throw('Cannot find script for extension: amp-list');
extensions.reloadExtension('amp-list');

expect(user().warn).to.be.calledWith(
'reloadExtension',
'Extension script for "%s" is missing or was already reloaded.',
'amp-list'
);
expect(list.hasAttribute('i-amphtml-loaded-new-version')).to.be.false;
expect(extensions.preloadExtension).to.not.be.called;
});
Expand Down Expand Up @@ -751,10 +759,13 @@ describes.sandboxed('Extensions', {}, () => {

describe('module/nomdule script pairs', () => {
it('should devAssert if script cannot be found', () => {
expect(() => {
allowConsoleError(() => extensions.reloadExtension('amp-list'));
}).to.throw('Cannot find script for extension: amp-list');
extensions.reloadExtension('amp-list');

expect(user().warn).to.be.calledWith(
'reloadExtension',
'Extension script for "%s" is missing or was already reloaded.',
'amp-list'
);
expect(extensions.preloadExtension).to.not.be.called;
});

Expand All @@ -779,10 +790,13 @@ describes.sandboxed('Extensions', {}, () => {
nomod.setAttribute('nomodule', '');
win.document.head.appendChild(nomod);

expect(() => {
allowConsoleError(() => extensions.reloadExtension('amp-list'));
}).to.throw('Cannot find script for extension: amp-list');
extensions.reloadExtension('amp-list');

expect(user().warn).to.be.calledWith(
'reloadExtension',
'Extension script for "%s" is missing or was already reloaded.',
'amp-list'
);
expect(mod.hasAttribute('i-amphtml-loaded-new-version')).to.be.false;
expect(nomod.hasAttribute('i-amphtml-loaded-new-version')).to.be.false;
expect(extensions.preloadExtension).to.not.be.called;
Expand Down