├── .gitignore ├── .npmignore ├── LICENSE.md ├── README.md ├── index.js ├── lib └── url.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | bower_components 2 | node_modules 3 | *.log 4 | .DS_Store 5 | bundle.js 6 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | bower_components 2 | node_modules 3 | *.log 4 | .DS_Store 5 | bundle.js 6 | test 7 | test.js 8 | demo/ 9 | .npmignore 10 | LICENSE.md -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2017 Jam3 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 20 | OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # reload-css 2 | 3 | [![experimental](http://badges.github.io/stability-badges/dist/experimental.svg)](http://github.com/badges/stability-badges) 4 | 5 | This module reloads all style sheets associated with a specified `url`. This is primarily useful for LiveReload servers that wish to update style sheets without triggering a full page refresh. 6 | 7 | If you omit the `url` argument, all style sheets will be cache busted. 8 | 9 | ## Install 10 | 11 | ```sh 12 | npm install reload-css --save 13 | ``` 14 | 15 | ## Usage 16 | 17 | [![NPM](https://nodei.co/npm/reload-css.png)](https://www.npmjs.com/package/reload-css) 18 | 19 | #### `reloadCSS([url], [opt])` 20 | 21 | Cache-busts the URLs for all `` tags that match the specified `url`, as well as any other style sheets that `@import` the URL. 22 | 23 | By default, this will only look for local style sheets (i.e. `localhost`, `127.0.0.1`, or matching the document domain). You can pass `{ local: false }` as the options to cache bust all styles. 24 | 25 | In some cases, it will walk upwards to a more top-level style sheet (i.e. in a long chain of import dependencies) to ensure a consistent result across browsers. Import statements are updated in the `cssRules`, and `` tags are re-attached for a clean update (no flicker/flash). 26 | 27 | You can omit `url` or pass `null` as the first argument to reload all styles instead of just a target one. 28 | 29 | ## License 30 | 31 | MIT, see [LICENSE.md](http://github.com/Jam3/reload-css/blob/master/LICENSE.md) for details. 32 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var qs = require('query-string'); 2 | var URL = require('./lib/url'); 3 | var baseHosts = getBaseHosts(); 4 | 5 | module.exports = function (url, opt) { 6 | // by default, only reloads local style sheets 7 | var localOnly = true; 8 | if (opt && opt.local === false) { 9 | localOnly = false; 10 | } 11 | 12 | // determine base URL 13 | var baseUrl = document.location.pathname; 14 | var baseTag = document.querySelector('base'); 15 | if (baseTag) { 16 | baseUrl = baseTag.getAttribute('href'); 17 | var parsedBase = URL.parse(baseUrl); 18 | parsedBase.pathname = '/'; 19 | parsedBase.hash = null; 20 | parsedBase.query = null; 21 | parsedBase.search = null; 22 | baseUrl = URL.format(parsedBase); 23 | } 24 | 25 | // Find all and