2
2
3
3
namespace Inertia ;
4
4
5
+ use Carbon \CarbonInterval ;
5
6
use Closure ;
6
7
use GuzzleHttp \Promise \PromiseInterface ;
7
8
use Illuminate \Contracts \Support \Arrayable ;
@@ -31,18 +32,23 @@ class Response implements Responsable
31
32
32
33
protected $ clearHistory ;
33
34
35
+ protected $ encryptHistory ;
36
+
34
37
protected $ viewData = [];
35
38
39
+ protected $ cacheFor = [];
40
+
36
41
/**
37
42
* @param array|Arrayable $props
38
43
*/
39
- public function __construct (string $ component , array $ props , string $ rootView = 'app ' , string $ version = '' , bool $ clearHistory = false )
44
+ public function __construct (string $ component , array $ props , string $ rootView = 'app ' , string $ version = '' , bool $ clearHistory = false , bool $ encryptHistory = false )
40
45
{
41
46
$ this ->component = $ component ;
42
47
$ this ->props = $ props instanceof Arrayable ? $ props ->toArray () : $ props ;
43
48
$ this ->rootView = $ rootView ;
44
49
$ this ->version = $ version ;
45
50
$ this ->clearHistory = $ clearHistory ;
51
+ $ this ->encryptHistory = $ encryptHistory ;
46
52
}
47
53
48
54
/**
@@ -84,6 +90,13 @@ public function rootView(string $rootView): self
84
90
return $ this ;
85
91
}
86
92
93
+ public function cache (string |array $ cacheFor ): self
94
+ {
95
+ $ this ->cacheFor = is_array ($ cacheFor ) ? $ cacheFor : [$ cacheFor ];
96
+
97
+ return $ this ;
98
+ }
99
+
87
100
/**
88
101
* Create an HTTP response that represents the object.
89
102
*
@@ -94,12 +107,19 @@ public function toResponse($request)
94
107
{
95
108
$ props = $ this ->resolveProperties ($ request , $ this ->props );
96
109
97
- $ page = [
98
- 'component ' => $ this ->component ,
99
- 'props ' => $ props ,
100
- 'url ' => Str::start (Str::after ($ request ->fullUrl (), $ request ->getSchemeAndHttpHost ()), '/ ' ),
101
- 'meta ' => $ this ->resolveMeta ($ request ),
102
- ];
110
+ $ page = array_merge (
111
+ [
112
+ 'component ' => $ this ->component ,
113
+ 'props ' => $ props ,
114
+ 'url ' => Str::start (Str::after ($ request ->fullUrl (), $ request ->getSchemeAndHttpHost ()), '/ ' ),
115
+ 'version ' => $ this ->version ,
116
+ 'clearHistory ' => $ this ->clearHistory ,
117
+ 'encryptHistory ' => $ this ->encryptHistory ,
118
+ ],
119
+ $ this ->resolveMergeProps ($ request ),
120
+ $ this ->resolveDeferredProps ($ request ),
121
+ $ this ->resolveCacheDirections ($ request ),
122
+ );
103
123
104
124
if ($ request ->header (Header::INERTIA )) {
105
125
return new JsonResponse ($ page , 200 , [Header::INERTIA => 'true ' ]);
@@ -220,7 +240,7 @@ public function resolvePropertyInstances(array $props, Request $request): array
220
240
AlwaysProp::class,
221
241
MergeProp::class,
222
242
WhenVisible::class,
223
- ])->first (fn ($ class ) => $ value instanceof $ class );
243
+ ])->first (fn ($ class ) => $ value instanceof $ class );
224
244
225
245
if ($ resolveViaApp ) {
226
246
$ value = App::call ($ value );
@@ -245,14 +265,23 @@ public function resolvePropertyInstances(array $props, Request $request): array
245
265
}
246
266
247
267
/**
248
- * Resolve the meta data for the response.
268
+ * Resolve the cache directions for the response.
249
269
*/
250
- public function resolveMeta (Request $ request ): array
270
+ public function resolveCacheDirections (Request $ request ): array
251
271
{
252
- return array_merge ([
253
- 'assetVersion ' => $ this ->version ,
254
- 'clearHistory ' => $ this ->clearHistory ,
255
- ], $ this ->resolveMergeProps ($ request ), $ this ->resolveDeferredProps ($ request ));
272
+ if (count ($ this ->cacheFor ) === 0 ) {
273
+ return [];
274
+ }
275
+
276
+ return [
277
+ 'cache ' => collect ($ this ->cacheFor )->map (function ($ value ) {
278
+ if ($ value instanceof CarbonInterval) {
279
+ return $ value ->totalSeconds ;
280
+ }
281
+
282
+ return intval ($ value );
283
+ }),
284
+ ];
256
285
}
257
286
258
287
public function resolveMergeProps (Request $ request ): array
0 commit comments