-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Framework: Move localStorage polyfill to separate bundle #7232
Conversation
@@ -5,43 +5,41 @@ import { assert } from 'chai'; | |||
|
|||
describe( 'localStorage', function() { | |||
describe( 'when window.localStorage does not exist', function() { | |||
const window = {}; | |||
delete global.localStorage; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels like we should stub, or restore this value when the suite is done.
This line is causing the failed tests: https://github.com/Automattic/wp-calypso/blob/master/client/lib/react-test-env-setup/index.js#L31 |
cc @mkaz for the desktop question |
Indeed, though interestingly different failures when updating this line. We might instead need to change |
2e90c58
to
d402cf5
Compare
Rebased and introduced a separate |
Whoops lost track of this one. I can test again after a rebase |
d402cf5
to
9036fff
Compare
Thanks @gwwar , pushed a rebase to resolve merge conflicts. I noticed that the conflicts were caused by the introduction of the ES6 function wrappers in #6117, which feels kinda similar in purpose. Do you think it would make sense to pull that into the "polyfill" bundle here? To be fair, those are more development mode utilities than polyfills. I dunno, maybe best to leave for a separate pull request in any case. |
Let's leave that for another PR |
With a completely empty |
👍 I confirmed that browsing with private Safari browser does not lead to errors in Calypso on this branch. The polyfill chunk size seems unreasonably large though, so we may want to investigate if an alternative webpack configuration can give us a lighter result. |
@gwwar hmmm.. if it's an entry chunk, it'll include the loader bits from webpack, but 385 seems... crazy. What all is in there? |
When building as production, it appears to be much smaller - Around 30kb without any minification or gzip (~5.5kb after). Inspecting generated bundle, most of this size is CoreJS polyfills for ES6 e.g. |
30kb sounds much more reasonable. I'd be comfortable with merging this. |
Checking in on this one, was there anything else that needed additional testing? |
No, just haven't had a chance to circle back around to this. Will need to rebase and retest to account for changes in #8161. |
9036fff
to
2a2df95
Compare
Rebased to account for #8161. @blowery : Do you think this should be included in the vendor bundle? Currently it's separate. Also, I'm not sure why it was needed previously, but I did not include the |
client/lib/polyfill/index.js
Outdated
import localStoragePolyfill from 'lib/local-storage'; | ||
|
||
export default ( () => { | ||
if ( 'undefined' === typeof window ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this a little confusing. This is an IIFE that's exporting nothing. Could it just be an IIFE in the body of the module and explicitly export null or undefined instead? like
( () => { /*do the thing */ } )();
export default null;
or maybe even don't export anything?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hrm, just realized I may not understand how modules evaluate as well as I thought. Does code in the body of the module run before the module is require
d? Or just once on the first require
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're right that we don't need or want the export default
. The only other reason to keep it an IIFE is to allow the early return, since there can't be top-level returns in a module. Otherwise we could just put all the browser-specific polyfills inside the if
block.
Or just once on the first
require
?
This is the intended behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed IIFE export in ec93319.
re
Could you walk through that a bit more? I'm curious why the dev bundle would change in size at all. |
@blowery To be honest, I don't recall why I felt the need to include it in |
@aduth Want to keep this one active? We plan close it soon as it is an older pull with no recent activity. |
@lancewillett Yes, I'd still like to merge this in. It's reviewable in its current state as far as I can recall. |
I'd be fine with 🚢'ing this after a rebase and another spot check. |
Meh, static file has a number of downsides. Haven't decided whether they outweigh the need to create a separate config for such a small file though. |
Yeah. I'd consider just inlining it honestly. |
That's a good thought. In which case it acts more as an extension to what we have already for BrowseHappy? |
That sounds cool. |
My reluctance at this point stems from wariness to add more unminified JavaScript to the base
Can you explain what you mean by this being affected @blowery ? |
Just that it'll have to load after the manifest if its part of our normal webpack build. That's probably ok? |
My question is why we don't migrate existing code to use localForage instead of localStorage? |
We should, but it's non-trivial since the leap from synchronous to asynchronous is not always well-defined. Since it's not obvious, also noting that most usage won't be uncovered from a search of |
I did some research and it looks like
They mention callback and promises also here. |
I did even more research and it looks like we would need sync wrapper for I think your PR makes much more sense in this context. |
@aduth This PR needs a rebase |
yeah, there's a few places using it:
|
1b731a7
to
e0dcb62
Compare
@aduth I rebased it with master. @fabianapsimoes Can you test this branch with the scenario described in #6555 (comment)? |
@klimeryk Sounds alright to me! We can always revisit if needed. |
As discovered in #7038 and #6558 before it, our local storage polyfill loading behavior is very fragile, and I suspect we're doomed to repeat it in its current state. As such, this pull request seeks to separate the polyfill into a separate bundle to be included in the page prior to the main application bundle.
Testing instructions:
Verify that your primary browser and Safari Private Mode load Calypso without any errors in the console.
Open questions:
public/polyfill.js
since it’s a static asset?/cc @gwwar (fixed previous breakage) @blowery (tinkerer of Webpack)
Test live: https://calypso.live/?branch=add/polyfill-bundle