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

control non amp pages via amp-sw #28

Merged
merged 5 commits into from
Jul 2, 2019
Merged

control non amp pages via amp-sw #28

merged 5 commits into from
Jul 2, 2019

Conversation

prateekbh
Copy link
Member

@prateekbh prateekbh commented May 23, 2019

fixes #17

  • adds allowedNonAMPPages:Array<RegExp> config to documentCachingOptions.

@prateekbh prateekbh requested a review from kristoferbaxter May 23, 2019 01:29
Copy link
Contributor

@kristoferbaxter kristoferbaxter left a comment

Choose a reason for hiding this comment

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

Left a few pieces of feedback.

super(config);
private allowedPages_: Array<RegExp>;

constructor(config: {
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 the config an interface defined before the default class.

Also if it's by default required your can force all fields to be optional via TS syntax. Partial<Config>. (https://github.com/Microsoft/TypeScript/blob/ee25cdecbca49b2b5a290ecd65224f425b1d6a9c/lib/lib.es5.d.ts#L1354)

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, since this extends an interface from Workbox, let's make sure we're extending the type provided by Workbox.

If Workbox isn't providing the interface type, let's fix their definitions.

Copy link
Member Author

Choose a reason for hiding this comment

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

Comment 1 is fixed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Re Comment 2: The next version of Workbox is being written in TS, so that should solve the problem

}) {
const { allowedNonAMPPages, ...pluginConfig } = config;
super(pluginConfig);
if (allowedNonAMPPages && Array.isArray(allowedNonAMPPages)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Possible to make this early exit pattern instead of checking existance in successive condition checks?

if (allowedNonAMPPages) {
  if (!Array.isArray(allowedNonAMPPages)) {
    throw new TypeError('allowedNonAMPPages should be an array of RegExp');
  }
  this.allowedPages_ = allowedNonAMPPages;
}

Copy link
Member Author

Choose a reason for hiding this comment

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

done

if (currentTestUrl.test(clonedResponse.url)) {
return response;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Might also consider an Array.find.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed with Array.some as we were not using the returned object

@@ -30,6 +30,7 @@ export type DocumentCachingOptions = {
timeoutSeconds?: Number;
maxDocumentsInCache?: Number;
maxAgeSecondsforDocumentsInCache?: Number;
allowedNonAMPPages?: Array<RegExp>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Investigate extending the Plugin interfaces to create the DocumentCachingOptions interface.

Copy link
Member Author

Choose a reason for hiding this comment

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

The reason why we cannot do this is because our keys do not match with the Plugin's config keys.
e.g. MaxExtries makes sense for workbox but for us the same key is known as MaxDocumentsInCache so the extends wouldn't work here

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense, thanks for investigating. Might recommend a comment or similar.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

super(pluginConfig);
if (allowedNonAMPPages) {
if (!Array.isArray(allowedNonAMPPages)) {
throw new TypeError('allowedNonAMPPages should be an array of RegExp');
Copy link
Contributor

Choose a reason for hiding this comment

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

If the value is a single RegExp should this handle it?

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess just enforcing this to always be an array should make this intuitive enough for everyone, plus lesser switch cases around typeof would keep the code straight forward

@@ -30,6 +30,7 @@ export type DocumentCachingOptions = {
timeoutSeconds?: Number;
maxDocumentsInCache?: Number;
maxAgeSecondsforDocumentsInCache?: Number;
allowedNonAMPPages?: Array<RegExp>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense, thanks for investigating. Might recommend a comment or similar.

@prateekbh prateekbh merged commit 041625a into master Jul 2, 2019
@prateekbh prateekbh deleted the non-amp-pages branch July 2, 2019 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow user to optin Non amp document to be cached by AMP service worker
2 participants