Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Address #27 - Dedupe in-flight requests and add 2 minute stylesheet caching #35

Closed
wants to merge 2 commits into from
Closed

Conversation

pl12133
Copy link

@pl12133 pl12133 commented Feb 24, 2017

To address the duplicated requests, I added deduplication and caching to loadExternal.

Using the awesome minimal test case from @Frxnz, I saw 3 XHR requests for each stylesheet. Here is a fixed test case showing only 1 request by loadExternal for each stylesheet.

I was unable to run your test suite because I could not find the replace package on Arch Linux, sorry. :)

@pl12133 pl12133 changed the title Dedupe in-flight XHR requests, cache responses from XHR requests for … Address #27 - Dedupe in-flight requests and add 2 minute stylesheet caching Feb 24, 2017
cq-prolyfill.js Outdated
CACHE[href] = response || '';
setTimeout(function() { delete CACHE[href]; }, CACHE_MAX_AGE);

IN_FLIGHT[href].forEach((cb) => cb(CACHE[href]));
Copy link
Contributor

Choose a reason for hiding this comment

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

No arrow functions here 🦀

@ausi ausi mentioned this pull request Feb 25, 2017
@ausi
Copy link
Owner

ausi commented Feb 25, 2017

Thank you for this pull request!

I was unable to run your test suite because I could not find the replace package on Arch Linux, sorry. :)

Nothing to be sorry about, I guess I have to update the devDependencies in the package.json sometime :)

I modified your code a little bit in PR #36, combined CACHE and IN_FLIGHT to requestCache and renamed CACHE_MAX_AGE to requestCacheTime. And I added a test case for the deduplication.

I’m not sure if the 2 minute cache timeout is actually necessary. Can you imagine a case where it is needed?

@developit
Copy link
Contributor

@ausi VS no expiry? I think it might be useful for something like dynamically updated stylesheets, but at least in the case I'm thinking of (webpack dev server), the URL actually changes.

Context: we are using cq-prolyfill on a fairly large website with a lot of dynamically injected links to stylesheets. Right now reevaluate() is re-fetching and parsing a pretty huge number of stylesheets, enough that we noticed it in network graphs haha.

@pl12133
Copy link
Author

pl12133 commented Feb 25, 2017

Thank you for reviewing and cleaning up the code, and for adding supporting tests. @developit covered the use case here - upon page loads we are seeing a single stylesheet being requested about 20~ times over the course of roughly 4 seconds.

The caching is something I usually include as standard for repeated AJAX requests in order to avoid network calls as much as possible. I just tried it out without caching, and I still saw repeated requests for the same stylesheet. 1 second after the first de-duplicated request had returned, a repeated request for the same stylesheet was made. So because of that, I think we need at least a short cache here to solve #27. Maybe the expire time could come down to 30 seconds?

@ausi
Copy link
Owner

ausi commented Feb 25, 2017

Maybe the expire time could come down to 30 seconds?

I think we could remove the expiration completely, meaning cache it for the whole lifetime of the page.

@developit
Copy link
Contributor

Agreed - at first I had though you were asking about reducing the cache time @ausi, but it makes sense to cache infinitely - it's in-memory, and any network call to the same URL in that same context is going to get the same stylesheet anyway. At least for our use-case, in-memory caching without expiry should cover all needs.

@pl12133
Copy link
Author

pl12133 commented Feb 25, 2017

Then I agree too there's no need for an expiration. I also thought @ausi was looking for a shorter cache time.

@ausi
Copy link
Owner

ausi commented Feb 25, 2017

Great! I updated #36.

After the BrowserStack API starts working again and the tests pass in all browsers, I will merge it.

@ausi ausi closed this Feb 25, 2017
@pl12133
Copy link
Author

pl12133 commented Feb 25, 2017

Thanks for the quick follow up and the cool project!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants