@@ -93,6 +93,11 @@ class Util {
93
93
script . innerHTML = source . innerHTML ;
94
94
( target || source ) . replaceWith ( script ) ;
95
95
}
96
+
97
+ static fullPageReload ( ) {
98
+ Util . log ( `Page reload initiated.` ) ;
99
+ window . location . reload ( ) ;
100
+ }
96
101
}
97
102
98
103
class EleventyReload {
@@ -111,93 +116,96 @@ class EleventyReload {
111
116
default : async ( files , build = { } ) => {
112
117
let morphed = false ;
113
118
119
+ if ( ( build . templates || [ ] ) . length === 0 ) {
120
+ return ;
121
+ }
122
+
123
+ let templates = ( build ?. templates || [ ] ) . filter ( ( { url, inputPath} ) => {
124
+ return url === document . location . pathname && ( files || [ ] ) . includes ( inputPath ) ;
125
+ } ) ;
126
+
127
+ if ( templates . length === 0 ) {
128
+ Util . fullPageReload ( ) ;
129
+ return ;
130
+ }
131
+
114
132
try {
115
- if ( ( build . templates || [ ] ) . length > 0 ) {
116
- // Important: using `./` in `./morphdom.js` allows the special `.11ty` folder to be changed upstream
117
- const { default : morphdom } = await import ( `./morphdom.js` ) ;
118
-
119
- // { url, inputPath, content }
120
- for ( let template of build ?. templates || [ ] ) {
121
- if ( template . url === document . location . pathname ) {
122
- // Importantly, if this does not match but is still relevant (layout/include/etc), a full reload happens below. This could be improved.
123
- if ( ( files || [ ] ) . includes ( template . inputPath ) ) {
124
- // Notable limitation: this won’t re-run script elements or JavaScript page lifecycle events (load/DOMContentLoaded)
125
- morphed = true ;
126
-
127
- morphdom ( document . documentElement , template . content , {
128
- childrenOnly : true ,
129
- onBeforeElUpdated : function ( fromEl , toEl ) {
130
- if ( fromEl . nodeName === "SCRIPT" && toEl . nodeName === "SCRIPT" ) {
131
- if ( toEl . innerHTML !== fromEl . innerHTML ) {
132
- Util . log ( `JavaScript modified, reload initiated.` ) ;
133
- window . location . reload ( ) ;
134
- }
135
-
136
- return false ;
137
- }
138
-
139
- // Speed-up trick from morphdom docs
140
- // https://dom.spec.whatwg.org/#concept-node-equals
141
- if ( fromEl . isEqualNode ( toEl ) ) {
142
- return false ;
143
- }
144
-
145
- if ( Util . isEleventyLinkNodeMatch ( fromEl , toEl ) ) {
146
- return false ;
147
- }
148
-
149
- return true ;
150
- } ,
151
- addChild : function ( parent , child ) {
152
- // Declarative Shadow DOM https://github.com/11ty/eleventy-dev-server/issues/90
153
- if ( child . nodeName === "TEMPLATE" && child . hasAttribute ( "shadowrootmode" ) ) {
154
- let root = parent . shadowRoot ;
155
- if ( root ) {
156
- // remove all shadow root children
157
- while ( root . firstChild ) {
158
- root . removeChild ( root . firstChild ) ;
159
- }
160
- }
161
- for ( let newChild of child . content . childNodes ) {
162
- root . appendChild ( newChild ) ;
163
- }
164
- } else {
165
- parent . appendChild ( child ) ;
166
- }
167
- } ,
168
- onNodeAdded : function ( node ) {
169
- if ( node . nodeName === 'SCRIPT' ) {
170
- Util . log ( `JavaScript added, reload initiated.` ) ;
171
- window . location . reload ( ) ;
172
- }
173
- } ,
174
- onElUpdated : function ( node ) {
175
- // Re-attach custom elements
176
- if ( customElements . get ( node . tagName . toLowerCase ( ) ) ) {
177
- let placeholder = document . createElement ( "div" ) ;
178
- node . replaceWith ( placeholder ) ;
179
- requestAnimationFrame ( ( ) => {
180
- placeholder . replaceWith ( node ) ;
181
- placeholder = undefined ;
182
- } ) ;
183
- }
133
+ // Important: using `./` allows the `.11ty` folder name to be changed
134
+ const { default : morphdom } = await import ( `./morphdom.js` ) ;
135
+
136
+ for ( let { url, inputPath, content} of templates ) {
137
+ // Notable limitation: this won’t re-run script elements or JavaScript page lifecycle events (load/DOMContentLoaded)
138
+ morphed = true ;
139
+
140
+ morphdom ( document . documentElement , content , {
141
+ childrenOnly : true ,
142
+ onBeforeElUpdated : function ( fromEl , toEl ) {
143
+ if ( fromEl . nodeName === "SCRIPT" && toEl . nodeName === "SCRIPT" ) {
144
+ if ( toEl . innerHTML !== fromEl . innerHTML ) {
145
+ Util . log ( `JavaScript modified, reload initiated.` ) ;
146
+ window . location . reload ( ) ;
147
+ }
148
+
149
+ return false ;
150
+ }
151
+
152
+ // Speed-up trick from morphdom docs
153
+ // https://dom.spec.whatwg.org/#concept-node-equals
154
+ if ( fromEl . isEqualNode ( toEl ) ) {
155
+ return false ;
156
+ }
157
+
158
+ if ( Util . isEleventyLinkNodeMatch ( fromEl , toEl ) ) {
159
+ return false ;
160
+ }
161
+
162
+ return true ;
163
+ } ,
164
+ addChild : function ( parent , child ) {
165
+ // Declarative Shadow DOM https://github.com/11ty/eleventy-dev-server/issues/90
166
+ if ( child . nodeName === "TEMPLATE" && child . hasAttribute ( "shadowrootmode" ) ) {
167
+ let root = parent . shadowRoot ;
168
+ if ( root ) {
169
+ // remove all shadow root children
170
+ while ( root . firstChild ) {
171
+ root . removeChild ( root . firstChild ) ;
184
172
}
173
+ }
174
+ for ( let newChild of child . content . childNodes ) {
175
+ root . appendChild ( newChild ) ;
176
+ }
177
+ } else {
178
+ parent . appendChild ( child ) ;
179
+ }
180
+ } ,
181
+ onNodeAdded : function ( node ) {
182
+ if ( node . nodeName === 'SCRIPT' ) {
183
+ Util . log ( `JavaScript added, reload initiated.` ) ;
184
+ window . location . reload ( ) ;
185
+ }
186
+ } ,
187
+ onElUpdated : function ( node ) {
188
+ // Re-attach custom elements
189
+ if ( customElements . get ( node . tagName . toLowerCase ( ) ) ) {
190
+ let placeholder = document . createElement ( "div" ) ;
191
+ node . replaceWith ( placeholder ) ;
192
+ requestAnimationFrame ( ( ) => {
193
+ placeholder . replaceWith ( node ) ;
194
+ placeholder = undefined ;
185
195
} ) ;
186
-
187
- Util . matchRootAttributes ( template . content ) ;
188
- Util . log ( `HTML delta applied without page reload.` ) ;
189
196
}
190
- break ;
191
197
}
192
- }
198
+ } ) ;
199
+
200
+ Util . matchRootAttributes ( content ) ;
201
+ Util . log ( `HTML delta applied without page reload.` ) ;
193
202
}
194
203
} catch ( e ) {
195
204
Util . error ( "Morphdom error" , e ) ;
196
205
}
197
206
198
207
if ( ! morphed ) {
199
- Util . log ( `Page reload initiated.` ) ;
200
- window . location . reload ( ) ;
208
+ Util . fullPageReload ( ) ;
201
209
}
202
210
}
203
211
}
0 commit comments