Reading List
Don't skip `else` with octane from Sebastian De Deyne RSS feed.
Don't skip `else` with octane
I came across a subtle Octane but last week. We needed to disable Inertia’s history encryption in some places in the app. By default, it’s enabled everywhere. So we added a route check to the middleware to turn it off.
class HandleInertiaRequests
{
public function handle(Request $request, Closure $next)
{
if ($request->is('admin/*')) {
Inertia::encryptHistory(false);
}
// …
}
}
The problem is, if you’re running Octane and make a request to an admin/* route, history encryption will be disabled, and there’s nothing to re-enable it for the next request.
The fix is to make sure both sides of the statement are executed.
class HandleInertiaRequests
{
public function handle(Request $request, Closure $next)
{
if ($request->is('admin/*')) {
Inertia::encryptHistory(false);
} else {
Inertia::encryptHistory(true);
}
// …
}
}
Or more succinct:
class HandleInertiaRequests
{
public function handle(Request $request, Closure $next)
{
Inertia::encryptHistory(! $request->is('admin/*'));
// …
}
}