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

fix(index): update to allow requesting failed async css files #292

Merged
merged 4 commits into from Nov 21, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 1 addition & 16 deletions src/index.js
Expand Up @@ -334,22 +334,6 @@ class MiniCssExtractPlugin {
Template.indent([
`var href = ${linkHrefPath};`,
`var fullhref = ${mainTemplate.requireFn}.p + href;`,
'var existingLinkTags = document.getElementsByTagName("link");',
'for(var i = 0; i < existingLinkTags.length; i++) {',
Template.indent([
'var tag = existingLinkTags[i];',
'var dataHref = tag.getAttribute("data-href") || tag.getAttribute("href");',
'if(tag.rel === "stylesheet" && (dataHref === href || dataHref === fullhref)) return resolve();',
]),
'}',
'var existingStyleTags = document.getElementsByTagName("style");',
'for(var i = 0; i < existingStyleTags.length; i++) {',
Template.indent([
'var tag = existingStyleTags[i];',
'var dataHref = tag.getAttribute("data-href");',
'if(dataHref === href || dataHref === fullhref) return resolve();',
]),
cwalten marked this conversation as resolved.
Show resolved Hide resolved
'}',
'var linkTag = document.createElement("link");',
'linkTag.rel = "stylesheet";',
'linkTag.type = "text/css";',
Expand All @@ -359,6 +343,7 @@ class MiniCssExtractPlugin {
'var request = event && event.target && event.target.src || fullhref;',
'var err = new Error("Loading CSS chunk " + chunkId + " failed.\\n(" + request + ")");',
'err.request = request;',
'delete installedCssChunks[chunkId]',
'reject(err);',
cwalten marked this conversation as resolved.
Show resolved Hide resolved
]),
'};',
Expand Down
5 changes: 5 additions & 0 deletions test/manual/index.html
Expand Up @@ -34,6 +34,11 @@
<p>But turn orange, when <button class="lazy-button2">pressing this button</button>. Additional clicks have no effect.</p>
<p>Refresh and press buttons in reverse order: This should turn green instead.</p>
</div>
<div class="test lazy-failure-css">
<p>Lazy CSS: Turn off the network and <button class="lazy-failure-button">press this button</button>.</p>
<p>An error should have appeared.</p>
<p>Now if you turn the network back on and click it again, it should turn aqua.</p>
</div>
<div class="test preloaded-css1">
<p>Preloaded CSS: Must be green.</p>
<p><button class="preloaded-button1">Pressing this button</button> displays an alert and should turn red.</p>
Expand Down
8 changes: 6 additions & 2 deletions test/manual/src/index.js
Expand Up @@ -5,10 +5,12 @@ const handleError = (err) => {
console.error(err);
}

const makeButton = (className, fn) => {
const makeButton = (className, fn, shouldDisable = true) => {
const button = document.querySelector(className);
button.addEventListener("click", () => {
button.disabled = true;
if(shouldDisable) {
button.disabled = true;
}
fn().then(() => {
button.disabled = false;
}).catch(handleError);
Expand All @@ -20,3 +22,5 @@ makeButton(".lazy-button2", () => import('./lazy2.css'));

makeButton(".preloaded-button1", () => import(/* webpackChunkName: "preloaded1" */ './preloaded1'));
makeButton(".preloaded-button2", () => import(/* webpackChunkName: "preloaded2" */ './preloaded2'));

makeButton(".lazy-failure-button", () => import('./lazy-failure.js'), false);
3 changes: 3 additions & 0 deletions test/manual/src/lazy-failure.css
@@ -0,0 +1,3 @@
.lazy-failure-css {
background: aqua;
}
1 change: 1 addition & 0 deletions test/manual/src/lazy-failure.js
@@ -0,0 +1 @@
import './lazy-failure.css';