@@ -8,6 +8,25 @@ use crate::Request;
8
8
use crate :: SsrNode ;
9
9
use chrono:: { DateTime , Utc } ;
10
10
11
+ /// Clones a `Request` from its internal parts.
12
+ fn clone_req ( raw : & Request ) -> Request {
13
+ let mut builder = Request :: builder ( ) ;
14
+
15
+ for ( name, val) in raw. headers ( ) {
16
+ builder = builder. header ( name, val) ;
17
+ }
18
+
19
+ builder
20
+ . uri ( raw. uri ( ) )
21
+ . method ( raw. method ( ) )
22
+ . version ( raw. version ( ) )
23
+ // We always use an empty body because, in a Perseus request, only the URI matters
24
+ // Any custom data should therefore be sent in headers (if you're doing that, consider a
25
+ // dedicated API)
26
+ . body ( ( ) )
27
+ . unwrap ( ) // This should never fail...
28
+ }
29
+
11
30
/// Gets the path with the locale, returning it without if i18n isn't being
12
31
/// used.
13
32
fn get_path_with_locale ( path_without_locale : & str , translator : & Translator ) -> String {
@@ -170,6 +189,9 @@ async fn should_revalidate(
170
189
template : & Template < SsrNode > ,
171
190
path_encoded : & str ,
172
191
mutable_store : & impl MutableStore ,
192
+ translator : & Translator ,
193
+ path : & str ,
194
+ req : Request ,
173
195
) -> Result < bool , ServerError > {
174
196
let mut should_revalidate = false ;
175
197
// If it revalidates after a certain period of time, we needd to check that
@@ -197,7 +219,9 @@ async fn should_revalidate(
197
219
198
220
// Now run the user's custom revalidation logic
199
221
if template. revalidates_with_logic ( ) {
200
- should_revalidate = template. should_revalidate ( ) . await ?;
222
+ should_revalidate = template
223
+ . should_revalidate ( path. to_string ( ) , translator. get_locale ( ) , req)
224
+ . await ?;
201
225
}
202
226
Ok ( should_revalidate)
203
227
}
@@ -299,7 +323,6 @@ pub struct GetPageProps<'a, M: MutableStore, T: TranslationsManager> {
299
323
/// load server-side routing). Because this handles templates with potentially
300
324
/// revalidation and incremental generation, it uses both mutable and immutable
301
325
/// stores.
302
- // TODO possible further optimizations on this for futures?
303
326
pub async fn get_page_for_template < M : MutableStore , T : TranslationsManager > (
304
327
GetPageProps {
305
328
raw_path,
@@ -313,6 +336,10 @@ pub async fn get_page_for_template<M: MutableStore, T: TranslationsManager>(
313
336
} : GetPageProps < ' _ , M , T > ,
314
337
template : & Template < SsrNode > ,
315
338
) -> Result < PageData , ServerError > {
339
+ // Since `Request` is not actually `Clone`able, we hack our way around needing
340
+ // it twice An `Rc` won't work because of future constraints, and an `Arc`
341
+ // seems a little unnecessary
342
+ let req_2 = clone_req ( & req) ;
316
343
// Get a translator for this locale (for sanity we hope the manager is caching)
317
344
let translator = translations_manager
318
345
. get_translator_for_locale ( locale. to_string ( ) )
@@ -350,7 +377,16 @@ pub async fn get_page_for_template<M: MutableStore, T: TranslationsManager>(
350
377
// It's cached
351
378
Some ( ( html_val, head_val) ) => {
352
379
// Check if we need to revalidate
353
- if should_revalidate ( template, & path_encoded, mutable_store) . await ? {
380
+ if should_revalidate (
381
+ template,
382
+ & path_encoded,
383
+ mutable_store,
384
+ & translator,
385
+ path,
386
+ req,
387
+ )
388
+ . await ?
389
+ {
354
390
let ( html_val, head_val, state) = revalidate (
355
391
template,
356
392
& translator,
@@ -448,7 +484,16 @@ pub async fn get_page_for_template<M: MutableStore, T: TranslationsManager>(
448
484
449
485
// Handle if we need to revalidate
450
486
// It'll be in the mutable store if we do
451
- if should_revalidate ( template, & path_encoded, mutable_store) . await ? {
487
+ if should_revalidate (
488
+ template,
489
+ & path_encoded,
490
+ mutable_store,
491
+ & translator,
492
+ path,
493
+ req,
494
+ )
495
+ . await ?
496
+ {
452
497
let ( html_val, head_val, state) = revalidate (
453
498
template,
454
499
& translator,
@@ -492,7 +537,7 @@ pub async fn get_page_for_template<M: MutableStore, T: TranslationsManager>(
492
537
// page will be built soon If we're not, and there's no build state,
493
538
// then we still need to build, which we'll do after we've checked for
494
539
// amalgamation
495
- let state = get_request_state ( template, & translator, path, req ) . await ?;
540
+ let state = get_request_state ( template, & translator, path, req_2 ) . await ?;
496
541
states. request_state = state;
497
542
}
498
543
0 commit comments