Skip to content

Commit f6ad746

Browse files
committed
encrypt history
1 parent 0c99750 commit f6ad746

File tree

5 files changed

+86
-15
lines changed

5 files changed

+86
-15
lines changed

config/inertia.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,10 @@
7272

7373
],
7474

75+
'history' => [
76+
77+
'encrypt' => false,
78+
79+
],
80+
7581
];

src/EncryptHistoryMiddleware.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Inertia;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
use Symfony\Component\HttpFoundation\Response;
8+
9+
class EncryptHistoryMiddleware
10+
{
11+
/**
12+
* Handle the incoming request.
13+
*
14+
* @return Response
15+
*/
16+
public function handle(Request $request, Closure $next)
17+
{
18+
Inertia::encryptHistory();
19+
20+
return $next($request);
21+
}
22+
}

src/Inertia.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* @method static void share(string|array|\Illuminate\Contracts\Support\Arrayable $key, mixed $value = null)
1010
* @method static mixed getShared(string|null $key = null, mixed $default = null)
1111
* @method static void clearHistory()
12+
* @method static void encryptHistory($encrypt = true)
1213
* @method static void flushShared()
1314
* @method static void version(\Closure|string|null $version)
1415
* @method static string getVersion()

src/Response.php

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Inertia;
44

5+
use Carbon\CarbonInterval;
56
use Closure;
67
use GuzzleHttp\Promise\PromiseInterface;
78
use Illuminate\Contracts\Support\Arrayable;
@@ -31,18 +32,23 @@ class Response implements Responsable
3132

3233
protected $clearHistory;
3334

35+
protected $encryptHistory;
36+
3437
protected $viewData = [];
3538

39+
protected $cacheFor = [];
40+
3641
/**
3742
* @param array|Arrayable $props
3843
*/
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)
4045
{
4146
$this->component = $component;
4247
$this->props = $props instanceof Arrayable ? $props->toArray() : $props;
4348
$this->rootView = $rootView;
4449
$this->version = $version;
4550
$this->clearHistory = $clearHistory;
51+
$this->encryptHistory = $encryptHistory;
4652
}
4753

4854
/**
@@ -84,6 +90,13 @@ public function rootView(string $rootView): self
8490
return $this;
8591
}
8692

93+
public function cache(string|array $cacheFor): self
94+
{
95+
$this->cacheFor = is_array($cacheFor) ? $cacheFor : [$cacheFor];
96+
97+
return $this;
98+
}
99+
87100
/**
88101
* Create an HTTP response that represents the object.
89102
*
@@ -94,12 +107,19 @@ public function toResponse($request)
94107
{
95108
$props = $this->resolveProperties($request, $this->props);
96109

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+
);
103123

104124
if ($request->header(Header::INERTIA)) {
105125
return new JsonResponse($page, 200, [Header::INERTIA => 'true']);
@@ -220,7 +240,7 @@ public function resolvePropertyInstances(array $props, Request $request): array
220240
AlwaysProp::class,
221241
MergeProp::class,
222242
WhenVisible::class,
223-
])->first(fn ($class) => $value instanceof $class);
243+
])->first(fn($class) => $value instanceof $class);
224244

225245
if ($resolveViaApp) {
226246
$value = App::call($value);
@@ -245,14 +265,23 @@ public function resolvePropertyInstances(array $props, Request $request): array
245265
}
246266

247267
/**
248-
* Resolve the meta data for the response.
268+
* Resolve the cache directions for the response.
249269
*/
250-
public function resolveMeta(Request $request): array
270+
public function resolveCacheDirections(Request $request): array
251271
{
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+
];
256285
}
257286

258287
public function resolveMergeProps(Request $request): array

src/ResponseFactory.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ class ResponseFactory
2929

3030
protected $clearHistory = false;
3131

32+
protected $encryptHistory = false;
33+
34+
public function __construct()
35+
{
36+
$this->encryptHistory = config('inertia.history.encrypt', false);
37+
}
38+
3239
public function setRootView(string $name): void
3340
{
3441
$this->rootView = $name;
@@ -89,6 +96,11 @@ public function clearHistory(): void
8996
$this->clearHistory = true;
9097
}
9198

99+
public function encryptHistory($encrypt = true): void
100+
{
101+
$this->encryptHistory = $encrypt;
102+
}
103+
92104
/**
93105
* @deprecated Use `optional` instead.
94106
*/
@@ -137,7 +149,8 @@ public function render(string $component, $props = []): Response
137149
array_merge($this->sharedProps, $props),
138150
$this->rootView,
139151
$this->getVersion(),
140-
$this->clearHistory
152+
$this->clearHistory,
153+
$this->encryptHistory
141154
);
142155
}
143156

0 commit comments

Comments
 (0)