Skip to content
This repository was archived by the owner on Sep 10, 2022. It is now read-only.

Commit c70d24c

Browse files
author
Matt Gaunt
committed
Making it so that styles are added to the head of the document
1 parent b620c19 commit c70d24c

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

gulp-tasks/service-worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var swPrecache = require('sw-precache');
2424
var packageName = JSON.parse(fs.readFileSync('./package.json', 'utf8')).name;
2525

2626
gulp.task('service-worker:watch', function(cb) {
27-
gulp.watch(GLOBAL.config.src + '/**/*.*', ['service-worker']);
27+
gulp.watch(GLOBAL.config.dest + '/**/*.*', ['service-worker']);
2828
gulp.watch(GLOBAL.config.src + '/../server/views/**/*.*',
2929
['service-worker']);
3030
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "app-shell",
3-
"version": "0.1.175",
3+
"version": "0.1.177",
44
"private": true,
55
"license": "Apache Version 2.0",
66
"engines": {
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
<style type="text/css">{{{inlineStyles}}}</style>
1+
<style class="js-partial-styles" type="text/css">{{{inlineStyles}}}</style>
22

3+
<div class="js-partial-content">
34
{{{ body }}}
45

56
{{#each remoteScripts}}
67
<script src="{{this}}" async></script>
78
{{~/each}}
9+
</div>

src/scripts/controller/ActivityController.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,50 @@ export default class ActivityController {
2727
})
2828
.then((responseText) => {
2929
if (responseText !== null) {
30-
this.mainContainer.innerHTML = responseText;
30+
/**
31+
* NOTE: We could move the script tags in the partial to the
32+
* bottom of the body, but I don't think it'll change browser
33+
* behaviour so it should be fine to inline the script tag in
34+
* the main element.
35+
**/
36+
37+
// Parse the response string
38+
var parser = new DOMParser();
39+
var partialElement =
40+
parser.parseFromString(responseText, 'text/html');
41+
42+
// Add style element to the document head
43+
var styleElement = partialElement.querySelector('.js-partial-styles');
44+
document.head.appendChild(styleElement);
45+
46+
// Add content from partial to page
47+
var contentElement =
48+
partialElement.querySelector('.js-partial-content');
49+
this.mainContainer.innerHTML = contentElement.innerHTML;
3150
}
3251

3352
// Hide loading dialog
3453
this.loader.classList.add('is-hidden');
3554
})
3655
.catch((error) => {
56+
console.log(error);
3757
this.showError('There was a problem loading this page');
3858
});
3959
}
4060

4161
onFinish() {
42-
console.log('onFinish: ', this.path);
62+
console.log('onFinish');
63+
// Remove any existing styles
64+
var insertedStyles =
65+
document.querySelector('.js-partial-styles');
66+
if (insertedStyles) {
67+
document.head.removeChild(insertedStyles);
68+
}
69+
70+
// Remove the current content
71+
while (this.mainContainer.firstChild) {
72+
this.mainContainer.removeChild(this.mainContainer.firstChild);
73+
}
4374
}
4475

4576
show404() {

0 commit comments

Comments
 (0)