Skip to content

Commit 1a97b1a

Browse files
committed
Support for checking if the component exists when rendering
1 parent f7d630a commit 1a97b1a

File tree

5 files changed

+87
-19
lines changed

5 files changed

+87
-19
lines changed

config/inertia.php

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,36 @@
2929

3030
],
3131

32+
/*
33+
|--------------------------------------------------------------------------
34+
| Pages
35+
|--------------------------------------------------------------------------
36+
|
37+
| If you want to ensure that the pages exist, you can set `ensure_pages_exist` to true.
38+
| This will throw an exception if the component does not exist on the filesystem
39+
| when rendering a page. You may configure this separately for testing.
40+
|
41+
*/
42+
43+
'ensure_pages_exist' => false,
44+
45+
'page_paths' => [
46+
47+
resource_path('js/Pages'),
48+
49+
],
50+
51+
'page_extensions' => [
52+
53+
'js',
54+
'jsx',
55+
'svelte',
56+
'ts',
57+
'tsx',
58+
'vue',
59+
60+
],
61+
3262
/*
3363
|--------------------------------------------------------------------------
3464
| Testing
@@ -39,29 +69,16 @@
3969
| attempts to locate the component as a file relative to any of the
4070
| paths AND with any of the extensions specified here.
4171
|
72+
| By default, it uses the `page_paths` and `page_extensions` settings
73+
| defined above. You may override these values for testing purposes
74+
| by adding these two keys to this `testing` array.
75+
|
4276
*/
4377

4478
'testing' => [
4579

4680
'ensure_pages_exist' => true,
4781

48-
'page_paths' => [
49-
50-
resource_path('js/Pages'),
51-
52-
],
53-
54-
'page_extensions' => [
55-
56-
'js',
57-
'jsx',
58-
'svelte',
59-
'ts',
60-
'tsx',
61-
'vue',
62-
63-
],
64-
6582
],
6683

6784
'history' => [

src/ComponentNotFoundException.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Inertia;
4+
5+
use InvalidArgumentException;
6+
7+
class ComponentNotFoundException extends InvalidArgumentException {}

src/ResponseFactory.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Illuminate\Support\Facades\Response as BaseResponse;
1212
use Illuminate\Support\Traits\Macroable;
1313
use Inertia\Support\Header;
14+
use InvalidArgumentException;
1415
use Symfony\Component\HttpFoundation\RedirectResponse as SymfonyRedirect;
1516
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
1617

@@ -158,11 +159,27 @@ public function always($value): AlwaysProp
158159
return new AlwaysProp($value);
159160
}
160161

162+
/**
163+
* @throws ComponentNotFoundException
164+
*/
165+
protected function findComponentOrFail(string $component): void
166+
{
167+
try {
168+
app('inertia.view-finder')->find($component);
169+
} catch (InvalidArgumentException) {
170+
throw new ComponentNotFoundException("Inertia page component [{$component}] not found.");
171+
}
172+
}
173+
161174
/**
162175
* @param array|Arrayable $props
163176
*/
164177
public function render(string $component, $props = []): Response
165178
{
179+
if (config('inertia.ensure_pages_exist', false)) {
180+
$this->findComponentOrFail($component);
181+
}
182+
166183
if ($props instanceof Arrayable) {
167184
$props = $props->toArray();
168185
}

src/ServiceProvider.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,19 @@ public function register(): void
3232
$this->registerTestingMacros();
3333
$this->registerMiddleware();
3434

35+
$this->app->bind('inertia.view-finder', function ($app) {
36+
return new FileViewFinder(
37+
$app['files'],
38+
$app['config']->get('inertia.page_paths'),
39+
$app['config']->get('inertia.page_extensions')
40+
);
41+
});
42+
3543
$this->app->bind('inertia.testing.view-finder', function ($app) {
3644
return new FileViewFinder(
3745
$app['files'],
38-
$app['config']->get('inertia.testing.page_paths'),
39-
$app['config']->get('inertia.testing.page_extensions')
46+
$app['config']->get('inertia.testing.page_paths', fn () => $app['config']->get('inertia.page_paths')),
47+
$app['config']->get('inertia.testing.page_extensions', fn () => $app['config']->get('inertia.page_extensions'))
4048
);
4149
});
4250
}

tests/ResponseFactoryTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Illuminate\Support\Facades\Request;
1313
use Illuminate\Support\Facades\Route;
1414
use Inertia\AlwaysProp;
15+
use Inertia\ComponentNotFoundException;
1516
use Inertia\DeferProp;
1617
use Inertia\Inertia;
1718
use Inertia\LazyProp;
@@ -376,4 +377,22 @@ public function toArray()
376377
],
377378
]);
378379
}
380+
381+
public function test_will_throw_exception_if_component_does_not_exist_when_ensuring_is_enabled(): void
382+
{
383+
config()->set('inertia.ensure_pages_exist', true);
384+
385+
$this->expectException(ComponentNotFoundException::class);
386+
$this->expectExceptionMessage('Inertia page component [foo] not found.');
387+
388+
(new ResponseFactory)->render('foo');
389+
}
390+
391+
public function test_will_not_throw_exception_if_component_does_not_exist_when_ensuring_is_disabled(): void
392+
{
393+
config()->set('inertia.ensure_pages_exist', false);
394+
395+
$response = (new ResponseFactory)->render('foo');
396+
$this->assertInstanceOf(\Inertia\Response::class, $response);
397+
}
379398
}

0 commit comments

Comments
 (0)