You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+44-2Lines changed: 44 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,48 @@
1
1
# ember-m-css-loader
2
2
3
-
This README outlines the details of collaborating on this Ember addon.
3
+
This [Ember.js](https://emberjs.com/) addon helps load the css file(s) on demand, i.e. lazy loading, inside the `<link>` tag in the `document``<head>` using the service `m-css-loader` .
4
+
5
+
### Lazy Loading of CSS
6
+
7
+
[CSS are render blocking resources.](https://developers.google.com/web/fundamentals/performance/critical-rendering-path/render-blocking-css) The ambitious SPAs need more than one CSS resources (external libraries) and some of these CSS resources can be more functionality centric and may not be required to get loaded on the Home screen or may be required only for few of screens.
8
+
9
+
Suppose a web app uses Maps Library (like [leaflet](https://leafletjs.com)) for displaying maps, which comes with its own CSS. The maps are displayed only on couple of routes other than home screen. So the home screen is not needed to load the CSS for maps. The maps CSS should be loaded when the routes displaying maps are requested. In this scenario, its always preferable to load the CSS dynamically.
10
+
11
+
## `.load(attr)`
12
+
13
+
The method to load a CSS file on demand.
14
+
15
+
`attr` is a JSON object which holds the attribute values for the `<link>` tag to load CSS. It should at least have `href` property set to the source of the CSS.
The service `m-css-loader` returns a promise. The service listens to the events `onload` and `onerror` on the `<link>` tag in which the CSS is loaded. It resolves the promise inside `onload` event and rejects it if `onerror` event is raised.
29
+
30
+
Use of this promise is completely optional and upto the requirement of the app. If a route needs to wait until the CSS gets loaded then the service can be used inside the `beforeModel` hook.
31
+
32
+
mCssLoader: Ember.inject.service('m-css-loader'),
33
+
beforeModel() {
34
+
return this.get('mCssLoader').load({
35
+
href: '/assets/maps.css'
36
+
});
37
+
}
38
+
39
+
#### Caching
40
+
41
+
The service `m-css-loader` caches the `href`s loaded to avoid injecting the same CSS more than once.
42
+
43
+
#### CORS
44
+
45
+
The service inserts a `<link>` tag which as good as having it hardcoded at the time of html load. So no [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) issue.
4
46
5
47
## Installation
6
48
@@ -11,7 +53,7 @@ This README outlines the details of collaborating on this Ember addon.
11
53
## Running
12
54
13
55
*`ember serve`
14
-
* Visit your app at [http://localhost:4200](http://localhost:4200).
56
+
* Visit your app at [http://localhost:4200](http://localhost:4200), which loads the [bootstrap](https://getbootstrap.com) CSS lazily.
<p><ahref="https://developers.google.com/web/fundamentals/performance/critical-rendering-path/render-blocking-css">CSS are render blocking resources.</a> The ambitious SPAs need more than one CSS resources (external libraries) and some of these CSS resources can be more
13
+
functionality centric and may not be required to get loaded on the Home screen or may be required only for few of
14
+
screens.</p>
15
+
<p>Suppose a web app uses Maps Library (like <ahref="https://leafletjs.com">leaflet</a>) for displaying maps, which comes
16
+
with its own CSS. The maps are displayed only on couple of routes other than home screen. So the home screen is not
17
+
needed to load the CSS for maps. The maps CSS should be loaded when the routes displaying maps are requested. In
18
+
this scenario, its always preferable to load the CSS dynamically.</p>
<p>The service <code>m-css-loader</code> returns a promise. The service listens to the events <code>onload</code> and <code>onerror</code> on the <code><link></code> tag in which the CSS is loaded. It resolves the promise inside <code>onload</code> event and rejects it if <code>onerror</code> event is raised.</p>
44
+
<p>Use of this promise is completely optional and upto the requirement of the app. If a route needs to wait until the CSS
45
+
gets loaded then the service can be used inside the <code>beforeModel</code> hook.</p>
<li>Visit your app at <ahref="http://localhost:4200">http://localhost:4200</a>, which loads the <ahref="https://getbootstrap.com">bootstrap</a> CSS lazily.</li>
0 commit comments