Skip to content

Commit 9422d43

Browse files
erikn69barryvdh
andauthored
Support Inertia ajax calls (#1527)
* Support Inertia ajax calls * Check X-Inertia header on ajax response * change individual file_exists to glob * Update ViewCollector.php --------- Co-authored-by: Barry vd. Heuvel <[email protected]>
1 parent 1e7ddcc commit 9422d43

File tree

2 files changed

+70
-31
lines changed

2 files changed

+70
-31
lines changed

src/DataCollector/ViewCollector.php

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,52 +59,77 @@ public function getWidgets()
5959
public function addView(View $view)
6060
{
6161
$name = $view->getName();
62-
$path = $view->getPath();
62+
$type = null;
6363
$data = $view->getData();
64-
$shortPath = '';
65-
$type = '';
64+
$path = $view->getPath();
6665

66+
if (class_exists('\Inertia\Inertia')) {
67+
list($name, $type, $data, $path) = $this->getInertiaView($name, $data, $path);
68+
}
6769

68-
// Prevent duplicates
69-
$hash = $type . $path . $name . $this->collect_data ? implode(array_keys($view->getData())) : '';
70+
if (is_object($path)) {
71+
$type = get_class($view);
72+
$path = null;
73+
}
7074

71-
if (class_exists('\Inertia\Inertia') && isset($data['page']['props'], $data['page']['component'])) {
72-
$name = $data['page']['component'];
73-
$data = $data['page']['props'];
75+
if ($path) {
76+
if (!$type) {
77+
if (substr($path, -10) == '.blade.php') {
78+
$type = 'blade';
79+
} else {
80+
$type = pathinfo($path, PATHINFO_EXTENSION);
81+
}
82+
}
7483

75-
if (!@file_exists($path = resource_path('js/Pages/' . $name . '.js'))) {
76-
if (!@file_exists($path = resource_path('js/Pages/' . $name . '.vue'))) {
77-
if (!@file_exists($path = resource_path('js/Pages/' . $name . '.svelte'))) {
78-
$path = $view->getPath();
79-
}
84+
foreach ($this->exclude_paths as $excludePath) {
85+
if (str_starts_with($path, $excludePath)) {
86+
return;
8087
}
81-
} else {
82-
$type = 'react';
8388
}
8489
}
8590

86-
if ($path && is_string($path)) {
87-
$path = $this->normalizeFilePath($path);
88-
$shortPath = ltrim(str_replace(base_path(), '', $path), '/');
89-
} elseif (is_object($path)) {
90-
$type = get_class($view);
91-
$path = '';
91+
$this->addTemplate($name, $data, $type, $path);
92+
}
93+
94+
private function getInertiaView(string $name, array $data, ?string $path)
95+
{
96+
if (isset($data['page']) && is_array($data['page'])) {
97+
$data = $data['page'];
9298
}
9399

94-
if ($path && !$type) {
95-
if (substr($path, -10) == '.blade.php') {
96-
$type = 'blade';
97-
} else {
100+
if (isset($data['props'], $data['component'])) {
101+
$name = $data['component'];
102+
$data = $data['props'];
103+
104+
if ($files = glob(resource_path('js/Pages/' . $name . '.*'))) {
105+
$path = $files[0];
98106
$type = pathinfo($path, PATHINFO_EXTENSION);
107+
108+
if (in_array($type, ['js', 'jsx'])) {
109+
$type = 'react';
110+
}
99111
}
100112
}
101113

102-
foreach ($this->exclude_paths as $excludePath) {
103-
if (str_starts_with($path, $excludePath)) {
104-
return;
105-
}
114+
return [$name, $type ?? '', $data, $path];
115+
}
116+
117+
public function addInertiaAjaxView(array $data)
118+
{
119+
list($name, $type, $data, $path) = $this->getInertiaView('', $data, '');
120+
121+
if (! $name) {
122+
return;
106123
}
107124

125+
$this->addTemplate($name, $data, $type, $path);
126+
}
127+
128+
private function addTemplate(string $name, array $data, ?string $type, ?string $path)
129+
{
130+
// Prevent duplicates
131+
$hash = $type . $path . $name . ($this->collect_data ? implode(array_keys($data)) : '');
132+
108133
if ($this->collect_data === 'keys') {
109134
$params = array_keys($data);
110135
} elseif ($this->collect_data) {
@@ -125,8 +150,8 @@ public function addView(View $view)
125150
'hash' => $hash,
126151
];
127152

128-
if ($view->getPath() && $this->getXdebugLinkTemplate()) {
129-
$template['xdebug_link'] = $this->getXdebugLink(realpath($view->getPath()));
153+
if ($path && $this->getXdebugLinkTemplate()) {
154+
$template['xdebug_link'] = $this->getXdebugLink($path);
130155
}
131156

132157
$this->templates[] = $template;

src/LaravelDebugbar.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,20 @@ public function modifyResponse(Request $request, Response $response)
765765
$this->isJsonRequest($request) &&
766766
$app['config']->get('debugbar.capture_ajax', true)
767767
) {
768+
try {
769+
if ($this->hasCollector('views') && $response->headers->has('X-Inertia')) {
770+
$content = $response->getContent();
771+
772+
if (is_string($content)) {
773+
$content = json_decode($content, true);
774+
}
775+
776+
if (is_array($content)) {
777+
$this['views']->addInertiaAjaxView($content);
778+
}
779+
}
780+
} catch (Exception $e) {
781+
}
768782
try {
769783
$this->sendDataInHeaders(true);
770784

0 commit comments

Comments
 (0)