Skip to content

Commit 5703f22

Browse files
authored
Merge pull request #2 from vivekmunde/documentation
documentation
2 parents 80ed58c + abce49a commit 5703f22

File tree

6 files changed

+186
-5
lines changed

6 files changed

+186
-5
lines changed

README.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,48 @@
11
# ember-m-css-loader
22

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.
16+
17+
mCssLoader: Ember.inject.service('m-css-loader'),
18+
beforeModel() {
19+
this.get('mCssLoader').load({
20+
href: 'http://cdn-assets/maps.css',
21+
integrity: 'sha384-shfssiufhnof7348f738f7bw8g+Pmsjshdinwe98',
22+
crossorigin: 'anonymous'
23+
});
24+
}
25+
26+
#### Promise Based Load
27+
28+
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.
446

547
## Installation
648

@@ -11,7 +53,7 @@ This README outlines the details of collaborating on this Ember addon.
1153
## Running
1254

1355
* `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.
1557

1658
## Running Tests
1759

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Ember from 'ember';
2+
3+
export default Ember.Route.extend({
4+
mCssLoader: Ember.inject.service('m-css-loader'),
5+
beforeModel() {
6+
return Ember.RSVP.hash({
7+
bootstrap: this.get('mCssLoader').load({
8+
href: 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css',
9+
integrity: 'sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u',
10+
crossorigin: 'anonymous'
11+
}),
12+
bootstrapTheme: this.get('mCssLoader').load({
13+
href: 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css',
14+
integrity: 'sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp',
15+
crossorigin: 'anonymous'
16+
})
17+
});
18+
}
19+
});

tests/dummy/app/routes/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import Ember from 'ember';
2+
3+
export default Ember.Route.extend({
4+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div style="padding:100px 50px;text-align:center;font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;font-size:16px;">
2+
loading bootstrap css ...
3+
</div>
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
<h2 id="title">Welcome to Ember</h2>
2-
3-
{{outlet}}
1+
<h3 class="text-center">{{#link-to "index"}}ember-m-css-loader{{/link-to}}</h3>
2+
<hr>
3+
<div class="container">
4+
{{outlet}}
5+
</div>
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<h2 class="text-center">Read Me</h2>
2+
<br>
3+
<article class="markdown-body entry-content" itemprop="text">
4+
<p>This <a href="https://emberjs.com/">Ember.js</a> addon helps load the css file(s) on demand, i.e. lazy loading, inside
5+
the <code>&lt;link&gt;</code> tag in the <code>document</code> <code>&lt;head&gt;</code> using the service <code>m-css-loader</code> .</p>
6+
<h3>
7+
<a id="user-content-lazy-loading-of-css" class="anchor" href="#lazy-loading-of-css" aria-hidden="true">
8+
<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16">
9+
<path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path>
10+
</svg>
11+
</a>Lazy Loading of CSS</h3>
12+
<p><a href="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 <a href="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>
19+
<h2>
20+
<a id="user-content-loadattr" class="anchor" href="#loadattr" aria-hidden="true">
21+
<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16">
22+
<path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path>
23+
</svg>
24+
</a><code>.load(attr)</code></h2>
25+
<p>The method to load a CSS file on demand.</p>
26+
<p><code>attr</code> is a JSON object which holds the attribute values for the <code>&lt;link&gt;</code> tag to load CSS.
27+
It should at least have <code>href</code> property set to the source of the CSS.</p>
28+
<pre><code>mCssLoader: Ember.inject.service('m-css-loader'),
29+
beforeModel() {
30+
this.get('mCssLoader').load({
31+
href: 'http://cdn-assets/maps.css',
32+
integrity: 'sha384-shfssiufhnof7348f738f7bw8g+Pmsjshdinwe98',
33+
crossorigin: 'anonymous'
34+
});
35+
}
36+
</code></pre>
37+
<h4>
38+
<a id="user-content-promise-based-load" class="anchor" href="#promise-based-load" aria-hidden="true">
39+
<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16">
40+
<path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path>
41+
</svg>
42+
</a>Promise Based Load</h4>
43+
<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>&lt;link&gt;</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>
46+
<pre><code>mCssLoader: Ember.inject.service('m-css-loader'),
47+
beforeModel() {
48+
return this.get('mCssLoader').load({
49+
href: '/assets/maps.css'
50+
});
51+
}
52+
</code></pre>
53+
<h4>
54+
<a id="user-content-caching" class="anchor" href="#caching" aria-hidden="true">
55+
<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16">
56+
<path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path>
57+
</svg>
58+
</a>Caching</h4>
59+
<p>The service <code>m-css-loader</code> caches the <code>href</code>s loaded to avoid injecting the same CSS more than
60+
once.</p>
61+
<h4>
62+
<a id="user-content-cors" class="anchor" href="#cors" aria-hidden="true">
63+
<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16">
64+
<path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path>
65+
</svg>
66+
</a>CORS</h4>
67+
<p>The service inserts a <code>&lt;link&gt;</code> tag which as good as having it hardcoded at the time of html load. So
68+
no <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS">CORS</a> issue.</p>
69+
<h2>
70+
<a id="user-content-installation" class="anchor" href="#installation" aria-hidden="true">
71+
<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16">
72+
<path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path>
73+
</svg>
74+
</a>Installation</h2>
75+
<ul>
76+
<li><code>git clone &lt;repository-url&gt;</code> this repository</li>
77+
<li><code>cd ember-m-css-loader</code></li>
78+
<li><code>npm install</code></li>
79+
</ul>
80+
<h2>
81+
<a id="user-content-running" class="anchor" href="#running" aria-hidden="true">
82+
<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16">
83+
<path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path>
84+
</svg>
85+
</a>Running</h2>
86+
<ul>
87+
<li><code>ember serve</code></li>
88+
<li>Visit your app at <a href="http://localhost:4200">http://localhost:4200</a>, which loads the <a href="https://getbootstrap.com">bootstrap</a> CSS lazily.</li>
89+
</ul>
90+
<h2>
91+
<a id="user-content-running-tests" class="anchor" href="#running-tests" aria-hidden="true">
92+
<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16">
93+
<path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path>
94+
</svg>
95+
</a>Running Tests</h2>
96+
<ul>
97+
<li><code>npm test</code> (Runs <code>ember try:each</code> to test your addon against multiple Ember versions)</li>
98+
<li><code>ember test</code></li>
99+
<li><code>ember test --server</code></li>
100+
</ul>
101+
<h2>
102+
<a id="user-content-building" class="anchor" href="#building" aria-hidden="true">
103+
<svg aria-hidden="true" class="octicon octicon-link" height="16" version="1.1" viewBox="0 0 16 16" width="16">
104+
<path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path>
105+
</svg>
106+
</a>Building</h2>
107+
<ul>
108+
<li><code>ember build</code></li>
109+
</ul>
110+
<p>For more information on using ember-cli, visit <a href="https://ember-cli.com/">https://ember-cli.com/</a>.</p>
111+
</article>

0 commit comments

Comments
 (0)