-
-
Notifications
You must be signed in to change notification settings - Fork 372
fix: Promise pollyfil loaded before webpack bundle #127
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
Conversation
| minify: true, | ||
| stripPrefix: config.cwd, | ||
| staticFileGlobsIgnorePatterns: [ | ||
| /polyfills\.js$/, |
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.
Did you ignore this because all serviceWorker-compatible browsers don't need a fetch or Promise polyfill?
(^ I'm not certain that that's 100% true)
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.
From what I understand serviceWorker implementation requires fetch implementation which in turn requires Promise implementation. Based on FetchEvent. I could be wrong tough.
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.
Ah, right. I believe that's the case too 👍 You upgraded my 90% certainty to 95% 😉
src/resources/template.html
Outdated
| <%= htmlWebpackPlugin.options.ssr({ | ||
| url: '/' | ||
| }) %> | ||
| <script>"undefined"==typeof fetch&&document.write('<script src="<%= htmlWebpackPlugin.files.publicPath + "polyfills.js" %>"><\/script>')</script> |
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.
How about (window.fetch===void 0) && ...?
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.
Sounds great! I'll change it to window.fetch==void 0&&document.write(...) (5 less characters & html webpack plugin doesn't minify inline script 😢 )
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.
Lol okay. I was just going for perf since !== void 0 is faster than typeof === 'undefined'
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.
if we're changing, maybe just go with window.fetch || document.write(..).
developit
left a comment
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.
This is awesome - I was trying to do the exact same thing locally before I ended up going with the alias+ensure() setup but I couldn't get it working. It pains me ever so slightly to have a blocking document.write of a script in our HTML though - any thoughts on working around that?
src/resources/template.html
Outdated
| <%= htmlWebpackPlugin.options.ssr({ | ||
| url: '/' | ||
| }) %> | ||
| <script>"undefined"==typeof fetch&&document.write('<script src="<%= htmlWebpackPlugin.files.publicPath + "polyfills.js" %>"><\/script>')</script> |
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.
if we're changing, maybe just go with window.fetch || document.write(..).
|
Randomly commented in my review - do you guys think we could do something like this to work around using a blocking <body>
<script src="/bundle.js" defer></script>
<script>window.fetch || document.body.appendChild(document.createElement('script')).src='/polyfills.js';</script>
</body> |
|
@developit Why not split out <script>window.Promise || document.body.appendChild(....</script>Then the rest of the polyfills could just be loaded as normal from webpack. |
|
@ethanroday currently, preact-cli only includes Promise and fetch. And you need promise supports for fetch anyway. So no splitting needed |
|
Doesn't it have to be blocking in order to ensure it loads before |
|
@lukeed I wasn't sure if the async script (via createElement) would defer the |
|
@developit so I've done my research.
I've changed |
|
@rkostrzewski Makes sense, that's what I had "remembered" too. However, I'm worried that changing from |
|
@lukeed perf should be pretty much the same for tl;dr |
|
Haha okay 😆 Thanks! |
| <% for (var chunk of webpack.chunks) { %> | ||
| <% if (chunk.names.length === 1 && chunk.names[0] === 'polyfills') continue; %> | ||
| <% for (var file of chunk.files) { %> | ||
| <% if (htmlWebpackPlugin.options.preload && file.match(/\.(js|css)$/)) { %> |
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.
BTW htmlWebpackPlugin.options.preload comes from argv.preload which is a hidden switch that can't be used. Gonna check whole codebase for hidden switches sometime soon.
# Conflicts: # package.json
lukeed
left a comment
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.
Didn't check out locally but looks good to me!
|
@lukeed gonna fix regex coz I just noticed it is wrong ( |
|
@rkostrzewski you are using npm5.x? I thought it wasn't that stable and it has many issues. |
|
@reznord works good enough for me. Hadn't had any issues yet. |
|
I was had issues with it when I was working with react-native, so thought of upgrading later. |
|
any issues which I can take over? |
|
@reznord if you want feel free take anything with |
Closes #101.
Tested in Chrome on desktop & mobile and IE 11. Chrome doesn't complain about document.write (ofc I've used
typeof fetch === "function"). Rendering is not blocked in IE. 😄