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 using preloading over prefetching. #2388

Merged
merged 1 commit into from
Mar 5, 2016
Merged

Conversation

mkhatib
Copy link
Contributor

@mkhatib mkhatib commented Mar 2, 2016

Would something like this work? Or should we have two separate methods for prefetch and preload? The similarities between both kinda begs for one method.

Addresses #2372

@@ -135,6 +145,20 @@ class Preconnect {
return false;
}

/** @private */
isPreloadSupported_() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Memoizing would really help here, avoiding a bunch of useless <link>s from being created.

Copy link
Member

Choose a reason for hiding this comment

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

+1. Memoizing is a must

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

Choose a reason for hiding this comment

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

So this is still only memoized per instance of Preconnect. Can we move it up into a script variable instead?

Copy link
Member

Choose a reason for hiding this comment

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

There is only one instance (per window).

@cramforce
Copy link
Member

Looks great!

@mkhatib
Copy link
Contributor Author

mkhatib commented Mar 4, 2016

I've addressed the comments. I am going to work on refactoring the usage to pass the as attribute where possible and add some tests.

@@ -135,6 +144,20 @@ class Preconnect {
return false;
}

/** @private */
isPreloadSupported_() {
if (this.preloadSupported_ !== undefined) {
Copy link
Member

Choose a reason for hiding this comment

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

If you cache the result in an instance var, then don't do this, just call it once and use the instance var after.

@mkhatib
Copy link
Contributor Author

mkhatib commented Mar 4, 2016

PTAL. Added tests and updated usage to pass the as attribute for all prefetches.

@mkhatib mkhatib changed the title WIP: Allow using preloading over prefetching. Allow using preloading over prefetching. Mar 4, 2016
expect(iframe.doc.querySelector('link[rel=preconnect]').href)
.to.equal('https://a.prefetch.com/');
// Actual prefetch
const fetches = iframe.doc.querySelectorAll(
Copy link
Member

Choose a reason for hiding this comment

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

This is a bit unfortunate in that this test will fail when travis upgrades to a Chrome that supports preload.

I think it is better to remove this test and instead think about writing a test for isPreloadSupported_ that mocks stubs document.createElement('link')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had that in mind and I was thinking of always mocking the perloadSupported=false except when I am testing for preloading. What do you think? So basically this will change the preloadSupported=undefined to false in the beforeEach block?

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good. One think I typically do is have 1 test that does not mock the feature test, but succeeds independent of whether the tested for thing is present or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done! The way I did this is basically for the existence of either preload or prefetch links after disabling the mock preloadSupported=undefined.

@cramforce
Copy link
Member

Looks great. Some test feedback

@cramforce
Copy link
Member

LGTM

mkhatib added a commit that referenced this pull request Mar 5, 2016
Allow using preloading over prefetching.
@mkhatib mkhatib merged commit c54f8a7 into ampproject:master Mar 5, 2016
@mkhatib mkhatib deleted the preload branch March 5, 2016 01:21
@henel677 henel677 mentioned this pull request Sep 7, 2022
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.

3 participants