@@ -126,7 +126,7 @@ async function ssr(url, {useCache = true, onlyCriticalRequests = true,
126
126
page . on ( 'response' , async resp => {
127
127
const href = resp . url ( ) ;
128
128
const type = resp . request ( ) . resourceType ( ) ;
129
- const sameOriginResource = href . startsWith ( url ) ;
129
+ const sameOriginResource = new URL ( href ) . origin === new URL ( url ) . origin ;
130
130
// Only inline local resources.
131
131
if ( sameOriginResource ) {
132
132
if ( type === 'stylesheet' ) {
@@ -199,6 +199,25 @@ async function ssr(url, {useCache = true, onlyCriticalRequests = true,
199
199
return html ;
200
200
}
201
201
202
+ async function doSSR ( url , req ) {
203
+ // This ignores other query params on the URL besides the tweets.
204
+ url = new URL ( url ) ;
205
+ if ( 'tweets' in req . query ) {
206
+ url . searchParams . set ( 'tweets' , '' ) ;
207
+ }
208
+
209
+ const html = await ssr ( url . href , {
210
+ useCache : 'nocache' in req . query ? false : true ,
211
+ inlineStyles : 'noinline' in req . query ? false : true ,
212
+ inlineScripts : 'noinline' in req . query ? false : true ,
213
+ onlyCriticalRequests : 'noreduce' in req . query ? false : true ,
214
+ reuseChrome : 'reusechrome' in req . query ? true : false ,
215
+ headless : 'noheadless' in req . query ? false : true ,
216
+ } ) ;
217
+
218
+ return html ;
219
+ }
220
+
202
221
dbHelper . setApp ( firebasedAdmin . initializeApp ( {
203
222
// credential: firebasedAdmin.credential.applicationDefault()
204
223
credential : firebasedAdmin . credential . cert (
@@ -229,6 +248,19 @@ app.use(function addRequestHelpers(req, res, next) {
229
248
230
249
// app.use(bodyParser.urlencoded({extended: true}));
231
250
app . use ( bodyParser . json ( ) ) ;
251
+
252
+ // Handle index.html page dynamically.
253
+ app . get ( '/' , async ( req , res , next ) => {
254
+ // Server prerendered page to search crawlers.
255
+ if ( req . get ( 'User-Agent' ) . match ( / g o o g l e b o t | b i n g b o t / i) ) {
256
+ const html = await doSSR ( `${ req . getOrigin ( ) } /index.html` , req ) ;
257
+ // res.append('Link', `<${url}/styles.css>; rel=preload; as=style`); // Push styles.
258
+ return res . status ( 200 ) . send ( html ) ;
259
+ }
260
+ next ( ) ;
261
+ } ) ;
262
+
263
+
232
264
app . use ( express . static ( 'public' , { extensions : [ 'html' , 'htm' ] } ) ) ;
233
265
app . use ( express . static ( 'node_modules' ) ) ;
234
266
// app.use(express.static('node_modules/firebase'));
@@ -275,21 +307,7 @@ app.use(express.static('node_modules'));
275
307
// SSR render, 3G Slow:
276
308
// FP/FCP: 2.3s, 8.37s faster!
277
309
app . get ( '/ssr' , catchAsyncErrors ( async ( req , res ) => {
278
- // This ignores other query params on the URL besides the tweets.
279
- const url = new URL ( req . getOrigin ( ) ) ;
280
- if ( 'tweets' in req . query ) {
281
- url . searchParams . set ( 'tweets' , '' ) ;
282
- }
283
-
284
- const html = await ssr ( url . href , {
285
- useCache : 'nocache' in req . query ? false : true ,
286
- inlineStyles : 'noinline' in req . query ? false : true ,
287
- inlineScripts : 'noinline' in req . query ? false : true ,
288
- onlyCriticalRequests : 'noreduce' in req . query ? false : true ,
289
- reuseChrome : 'reusechrome' in req . query ? true : false ,
290
- headless : 'noheadless' in req . query ? false : true ,
291
- } ) ;
292
- // res.append('Link', `<${url}/styles.css>; rel=preload; as=style`); // Push styles.
310
+ const html = await doSSR ( `${ req . getOrigin ( ) } /index.html` , req ) ;
293
311
res . status ( 200 ) . send ( html ) ;
294
312
} ) ) ;
295
313
0 commit comments