@@ -9,9 +9,9 @@ const HTMLSerializer = new SimpleDOM.HTMLSerializer(SimpleDOM.voidMap);
99 * method.
1010 */
1111class Result {
12- constructor ( options ) {
13- this . instanceBooted = false ;
14- this . instanceDestroyed = false ;
12+ constructor ( options ) {
13+ this . instanceBooted = false ;
14+ this . instanceDestroyed = false ;
1515 this . _doc = options . doc ;
1616 this . _html = options . html ;
1717 this . _fastbootInfo = options . fastbootInfo ;
@@ -43,7 +43,7 @@ class Result {
4343 }
4444 }
4545
46- return Promise . resolve ( insertIntoIndexHTML ( this . _html , this . _head , this . _body ) ) ;
46+ return insertIntoIndexHTML ( this . _html , this . _head , this . _body ) ;
4747 }
4848
4949 /**
@@ -84,10 +84,10 @@ class Result {
8484 return this ;
8585 }
8686
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+ }
9191
9292 let response = this . _fastbootInfo . response ;
9393
@@ -112,22 +112,38 @@ class Result {
112112 }
113113}
114114
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.` ) ) ;
121117}
122118
123119function 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+ }
125133
126134 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+ }
128144 }
129145
130- return html ;
146+ return Promise . resolve ( html ) ;
131147}
132148
133149module . exports = Result ;
0 commit comments