Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(index): dynamic css loading support for older browsers#134

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
gaterking wants to merge3 commits intowebpack-contrib:master
base:master
Choose a base branch
Loading
fromgaterking:master

Conversation

gaterking
Copy link

This PR contains a:

  • bugfix
  • newfeature
  • code refactor
  • test update
  • typo fix
  • metadata update

Motivation / Use-Case

Breaking Changes

Additional Info

In old browser, such as android 4.3, link tag not support 4.3. Although these devices is few, the onload callback resolve function will not be called. This PR ignore onload for these devices and resolve immediately.

@jsf-clabot
Copy link

jsf-clabot commentedMay 8, 2018
edited
Loading

CLA assistant check
All committers have signed the CLA.

@alexander-akait
Copy link
Member

@gaterking can you provide minimum reproducible test repo and browser version to ensure it is does not works for all, thanks!

@gaterking
Copy link
Author

@evilebottnawihttp://pie.gd/test/script-link-events/
From this test page, we can see many old browsers not support link tag onload event.

alexander-akait reacted with thumbs up emoji

@alexander-akait
Copy link
Member

@gaterking also please accept CLA

@alexander-akait
Copy link
Member

@gaterking can you faced this problem ?

src/index.js Outdated
@@ -338,8 +338,10 @@ class MiniCssExtractPlugin {
]),
'}',
'var linkTag = document.createElement("link");',
'var isOldWebKit = Number(navigator.userAgent.replace(/.*AppleWebKit\\/(\\d+)..*/, "$1")) < 536;',

Choose a reason for hiding this comment

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

Maybe we can find better solution for check this, can you investigate how this do examplejquery or other frontend libraries?

Copy link
Author

Choose a reason for hiding this comment

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

At present, old browser is less. I only use regex to match AppleWebKit 536, bescause I think it will cover most of old browser. There is another solution to check version:
https://github.com/filamentgroup/loadCSS/blob/master/src/onloadCSS.js
As bellow is another solution to check if css is loaded:
http://www.backalleycoder.com/2011/03/20/link-tag-css-stylesheet-load-event/
https://www.zachleat.com/web/load-css-dynamically/
Another question: Why we need to wait resolve for css chunks is loaded? CSS chunkses are appended in sequence, they can't impact each other. The waiting will increase js render time inversely. There is so less case we need to caculate element style when the the self css is loaded but next css is not loaded.

Choose a reason for hiding this comment

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

@gaterkinghttp://www.backalleycoder.com/2011/03/20/link-tag-css-stylesheet-load-event/ looks universal, but create new dom node looks overload.

Give me time on thinking about best solution, ping be tomorrow and we find better.

Copy link
Author

Choose a reason for hiding this comment

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

https://github.com/guybedford/require-css/blob/master/css.js
A CSS Loader plugin for RequireJS which can be refered to.

Choose a reason for hiding this comment

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

@gaterking looks solid, maybe we can port solution here?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, it's solid. We can remove some inspections such as ie or low version firefox. Webpack 4 don't support ie6 to 8.

Choose a reason for hiding this comment

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

@gaterking we can avoid part of code for oldie

@gaterking
Copy link
Author

@evilebottnawi Yes, I faced it in my vue project. Android 4.3 browser will fail, bescause some lazy load modules with css chunk not resolve.

@alexander-akait
Copy link
Member

@gaterking looks good solution, but i think we should search better solution for detecting this

@Jessidhia
Copy link
Contributor

This also happened to be a blocker for a smartphone-specific project at work. I'll link this to the person involved so he can check if this works 🤔

@Jessidhia
Copy link
Contributor

Note that it's important to know when a dynamically inserted style is loaded. Not knowing this leads to FOUC, which can be disastrous if you're doing things likegetBoundingClientRect on component mount if it happens before the load is done.

@gaterking
Copy link
Author

index.js
@evilebottnawi Can you review my new code? If OK, I will create a new PR.
Refer toRequieJS plugin, it use regex to check browser, but it don't check webkit version.
In new solution, I refer toDojo plugin andxstyle. I use setInterval to check css file is loaded in this case.
I am sure we need to support webkit < 536 only, Becase of Webpack 4.

@gaterking
Copy link
Author

what the next step for this PR?

@@ -349,7 +362,25 @@ class MiniCssExtractPlugin {
'reject(err);',
]),
'};',
'linkTag.href = fullhref;',
'} else {',
'var errorTimeout = 60000;',

Choose a reason for hiding this comment

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

What about is loading more than60000 ?

Copy link
Author

Choose a reason for hiding this comment

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

It's a timeout threshold for css loading. To make sure chunk is resolved, I think we need a stop time to clear setInterval when the css loaded fail.

@michael-ciniawskymichael-ciniawsky changed the titledynamic import css chunk support old browserfix(index): dynamic import css chunk support older browsersAug 6, 2018
@michael-ciniawskymichael-ciniawsky added this to the0.4.2 milestoneAug 6, 2018
@michael-ciniawskymichael-ciniawsky changed the titlefix(index): dynamic import css chunk support older browsersfix(index): dynamic css loading support for older browsersAug 6, 2018
@hxlniada
Copy link

may be this is a better solution:https://stackoverflow.com/a/5371426

@alexander-akait
Copy link
Member

/cc@gaterking

@gaterking
Copy link
Author

I think this PR is not necessary for most developer, maybe we can close it. If somebody need to support old browser, I suggest to clone own repo.

@lili21
Copy link

@evilebottnawi what do you think?

@lili21
Copy link

@hxlniada It's a neat solution, just need a little change.

...varimg=document.createElement('img');img.onerror=function(e){varrequest=event&&event.target&&event.target.src||fullhref;vari=document.styleSheets.length;while(i--){if(document.styleSheets[i].href===request){returnresolve();}}varerr=newError('Loading CSS chunk '+chunkId+' failed.\n('+request+')');err.request=request;reject(err);};img.src=fullhref;...

@gaterking
Copy link
Author

gaterking commentedNov 14, 2018
edited
Loading

#299 similar PR

'linkTag.href = fullhref;',
"// old webkit's would claim to have onload, but didn't really support it",
'// https://github.com/kriszyp/xstyle/blob/master/core/load-css.js',
'var webkitVersion = navigator.userAgent.match(/AppleWebKit\\/(\\d+\\.?\\d*)/);',
Copy link
Member

Choose a reason for hiding this comment

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

This extra check affects only 0.02% of the internet. I don't think it's worth including this workaround of everybody.

'// most browsers support this onload function now',
'linkTag.onload = function(){',
'// cleanup',
'linkTag.onload = null;',
Copy link
Member

Choose a reason for hiding this comment

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

indent

'var startTime = Date.now();',
'var interval = setInterval(function(){',
'if(linkTag.style){',
'clearInterval(interval);',
Copy link
Member

Choose a reason for hiding this comment

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

indent

'clearInterval(interval);',
'resolve();',
'}',
'if(!linkTag.style && (Date.now() - startTime) > errorTimeout){',
Copy link
Member

Choose a reason for hiding this comment

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

The support for onload and onerror differ. Make sure to handle them separately. Use a setTimeout for error handling and don't merge it into the setInterval for onload.

'var errorTimeout = 60000;',
'var startTime = Date.now();',
'var interval = setInterval(function(){',
'if(linkTag.style){',
Copy link
Member

Choose a reason for hiding this comment

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

Instead of polling this seem to be a better solution for this problem:https://github.com/webpack-contrib/mini-css-extract-plugin/pull/299/files

@codecov
Copy link

codecovbot commentedMay 3, 2020
edited
Loading

Codecov Report

Merging#134 intomaster willnot change coverage.
The diff coverage isn/a.

Impacted file tree graph

@@          Coverage Diff           @@##           master    #134   +/-   ##======================================  Coverage    0.00%   0.00%           ======================================  Files           3       3             Lines         241     241             Branches       49      49           ======================================  Misses        198     198             Partials       43      43
Impacted FilesCoverage Δ
src/index.js0.00% <ø> (ø)

Continue to review full report at Codecov.

Legend -Click here to learn more
Δ = absolute <relative> (impact),ø = not affected,? = missing data
Powered byCodecov. Last update41347f7...bd891a0. Read thecomment docs.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@sokrasokrasokra requested changes

@alexander-akaitalexander-akaitalexander-akait requested changes

Requested changes must be addressed to merge this pull request.

Assignees
No one assigned
Projects
None yet
Milestone
0.4.4
Development

Successfully merging this pull request may close these issues.

8 participants
@gaterking@jsf-clabot@alexander-akait@Jessidhia@hxlniada@lili21@sokra@michael-ciniawsky

[8]ページ先頭

©2009-2025 Movatter.jp