@@ -9,9 +9,9 @@ const HTMLSerializer = new SimpleDOM.HTMLSerializer(SimpleDOM.voidMap);
9
9
* method.
10
10
*/
11
11
class Result {
12
- constructor ( options ) {
13
- this . instanceBooted = false ;
14
- this . instanceDestroyed = false ;
12
+ constructor ( options ) {
13
+ this . instanceBooted = false ;
14
+ this . instanceDestroyed = false ;
15
15
this . _doc = options . doc ;
16
16
this . _html = options . html ;
17
17
this . _fastbootInfo = options . fastbootInfo ;
@@ -43,7 +43,7 @@ class Result {
43
43
}
44
44
}
45
45
46
- return Promise . resolve ( insertIntoIndexHTML ( this . _html , this . _head , this . _body ) ) ;
46
+ return insertIntoIndexHTML ( this . _html , this . _head , this . _body ) ;
47
47
}
48
48
49
49
/**
@@ -84,10 +84,10 @@ class Result {
84
84
return this ;
85
85
}
86
86
87
- _finalizeMetadata ( instance ) {
88
- if ( this . instanceBooted ) {
89
- this . url = instance . getURL ( ) ;
90
- }
87
+ _finalizeMetadata ( instance ) {
88
+ if ( this . instanceBooted ) {
89
+ this . url = instance . getURL ( ) ;
90
+ }
91
91
92
92
let response = this . _fastbootInfo . response ;
93
93
@@ -112,22 +112,38 @@ class Result {
112
112
}
113
113
}
114
114
115
- /**
116
- * `String.replace()` converts '$$' to '$', so we must escape each '$' as '$$';
117
- * but because we’re using `String.replace()` to do it, we must use '$$$'!
118
- */
119
- function escapeForStringReplace ( string ) {
120
- return string . replace ( / \$ / g, '$$$' ) ;
115
+ function missingTag ( tag ) {
116
+ return Promise . reject ( new Error ( `Fastboot was not able to find ${ tag } in base HTML. It could not replace the contents.` ) ) ;
121
117
}
122
118
123
119
function insertIntoIndexHTML ( html , head , body ) {
124
- html = html . replace ( "<!-- EMBER_CLI_FASTBOOT_BODY -->" , escapeForStringReplace ( body ) ) ;
120
+ if ( ! html ) { return Promise . resolve ( html ) ; }
121
+
122
+ if ( body ) {
123
+ let isBodyReplaced = false ;
124
+ html = html . replace ( "<!-- EMBER_CLI_FASTBOOT_BODY -->" , function ( ) {
125
+ isBodyReplaced = true ;
126
+ return body ;
127
+ } ) ;
128
+
129
+ if ( ! isBodyReplaced ) {
130
+ return missingTag ( '<!--EMBER_CLI_FASTBOTT_BODY-->' ) ;
131
+ }
132
+ }
125
133
126
134
if ( head ) {
127
- html = html . replace ( "<!-- EMBER_CLI_FASTBOOT_HEAD -->" , escapeForStringReplace ( head ) ) ;
135
+ let isHeadReplaced = false ;
136
+ html = html . replace ( "<!-- EMBER_CLI_FASTBOOT_HEAD -->" , function ( ) {
137
+ isHeadReplaced = true ;
138
+ return head ;
139
+ } ) ;
140
+
141
+ if ( ! isHeadReplaced ) {
142
+ return missingTag ( '<!--EMBER_CLI_FASTBOTT_HEAD-->' ) ;
143
+ }
128
144
}
129
145
130
- return html ;
146
+ return Promise . resolve ( html ) ;
131
147
}
132
148
133
149
module . exports = Result ;
0 commit comments