Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
Support reloading css files only.
Browse files Browse the repository at this point in the history
  • Loading branch information
T4rk1n committed Sep 17, 2018
1 parent 2097046 commit a9ecf20
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions src/components/core/Reloader.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Reloader extends React.Component {
disabled: true
}
}
this._head = document.querySelector('head');
}

componentDidUpdate() {
Expand All @@ -41,9 +42,49 @@ class Reloader extends React.Component {
|| !R.all(R.map(x => R.contains(x, this.state.packages),
reloadRequest.content.packages))
) {
// Assets file have changed
// or a component lib has been added/removed
window.top.location.reload();
// Look if it was a css file.
let was_css = false;
for (let a of reloadRequest.content.files) {
if (a.is_css) {
was_css = true;
const nodesToDisable = [];

// Search for the old file by xpath.
const it = document.evaluate(
`//link[contains(@href, "${a.url}")]`,
this._head
);
let node = it.iterateNext();

while (node) {
nodesToDisable.push(node);
node = it.iterateNext();
}
nodesToDisable.forEach(
n => n.setAttribute('disabled', 'disabled'));

const link = document.createElement('link');
link.href = `${a.url}?m=${a.modified}`;
link.type = 'text/css';
link.rel = 'stylesheet';
this._head.appendChild(link);
} else {
// If there's another kind of file here do a hard reload.
was_css = false;
break;
}
}
if (!was_css) {
// Assets file have changed
// or a component lib has been added/removed
window.top.location.reload();
} else {
// Since it's only a css reload,
// we just change the hash.
this.setState({
hash: reloadRequest.content.reloadHash
})
}
} else {
dispatch({'type': 'RELOAD'});
}
Expand Down

0 comments on commit a9ecf20

Please sign in to comment.