From 1a97b1a0d3ad0a0c230da1d7c45976d603f45973 Mon Sep 17 00:00:00 2001 From: Pascal Baljet Date: Thu, 19 Jun 2025 18:11:35 +0200 Subject: [PATCH 1/2] Support for checking if the component exists when rendering --- config/inertia.php | 51 ++++++++++++++++++++---------- src/ComponentNotFoundException.php | 7 ++++ src/ResponseFactory.php | 17 ++++++++++ src/ServiceProvider.php | 12 +++++-- tests/ResponseFactoryTest.php | 19 +++++++++++ 5 files changed, 87 insertions(+), 19 deletions(-) create mode 100644 src/ComponentNotFoundException.php diff --git a/config/inertia.php b/config/inertia.php index bab52f25..88557bb3 100644 --- a/config/inertia.php +++ b/config/inertia.php @@ -29,6 +29,36 @@ ], + /* + |-------------------------------------------------------------------------- + | Pages + |-------------------------------------------------------------------------- + | + | If you want to ensure that the pages exist, you can set `ensure_pages_exist` to true. + | This will throw an exception if the component does not exist on the filesystem + | when rendering a page. You may configure this separately for testing. + | + */ + + 'ensure_pages_exist' => false, + + 'page_paths' => [ + + resource_path('js/Pages'), + + ], + + 'page_extensions' => [ + + 'js', + 'jsx', + 'svelte', + 'ts', + 'tsx', + 'vue', + + ], + /* |-------------------------------------------------------------------------- | Testing @@ -39,29 +69,16 @@ | attempts to locate the component as a file relative to any of the | paths AND with any of the extensions specified here. | + | By default, it uses the `page_paths` and `page_extensions` settings + | defined above. You may override these values for testing purposes + | by adding these two keys to this `testing` array. + | */ 'testing' => [ 'ensure_pages_exist' => true, - 'page_paths' => [ - - resource_path('js/Pages'), - - ], - - 'page_extensions' => [ - - 'js', - 'jsx', - 'svelte', - 'ts', - 'tsx', - 'vue', - - ], - ], 'history' => [ diff --git a/src/ComponentNotFoundException.php b/src/ComponentNotFoundException.php new file mode 100644 index 00000000..81776acb --- /dev/null +++ b/src/ComponentNotFoundException.php @@ -0,0 +1,7 @@ +find($component); + } catch (InvalidArgumentException) { + throw new ComponentNotFoundException("Inertia page component [{$component}] not found."); + } + } + /** * @param array|Arrayable $props */ public function render(string $component, $props = []): Response { + if (config('inertia.ensure_pages_exist', false)) { + $this->findComponentOrFail($component); + } + if ($props instanceof Arrayable) { $props = $props->toArray(); } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index b0e0e0c8..665d0b0a 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -32,11 +32,19 @@ public function register(): void $this->registerTestingMacros(); $this->registerMiddleware(); + $this->app->bind('inertia.view-finder', function ($app) { + return new FileViewFinder( + $app['files'], + $app['config']->get('inertia.page_paths'), + $app['config']->get('inertia.page_extensions') + ); + }); + $this->app->bind('inertia.testing.view-finder', function ($app) { return new FileViewFinder( $app['files'], - $app['config']->get('inertia.testing.page_paths'), - $app['config']->get('inertia.testing.page_extensions') + $app['config']->get('inertia.testing.page_paths', fn () => $app['config']->get('inertia.page_paths')), + $app['config']->get('inertia.testing.page_extensions', fn () => $app['config']->get('inertia.page_extensions')) ); }); } diff --git a/tests/ResponseFactoryTest.php b/tests/ResponseFactoryTest.php index c2af15ad..52970279 100644 --- a/tests/ResponseFactoryTest.php +++ b/tests/ResponseFactoryTest.php @@ -12,6 +12,7 @@ use Illuminate\Support\Facades\Request; use Illuminate\Support\Facades\Route; use Inertia\AlwaysProp; +use Inertia\ComponentNotFoundException; use Inertia\DeferProp; use Inertia\Inertia; use Inertia\LazyProp; @@ -376,4 +377,22 @@ public function toArray() ], ]); } + + public function test_will_throw_exception_if_component_does_not_exist_when_ensuring_is_enabled(): void + { + config()->set('inertia.ensure_pages_exist', true); + + $this->expectException(ComponentNotFoundException::class); + $this->expectExceptionMessage('Inertia page component [foo] not found.'); + + (new ResponseFactory)->render('foo'); + } + + public function test_will_not_throw_exception_if_component_does_not_exist_when_ensuring_is_disabled(): void + { + config()->set('inertia.ensure_pages_exist', false); + + $response = (new ResponseFactory)->render('foo'); + $this->assertInstanceOf(\Inertia\Response::class, $response); + } } From 88d55d7a44dc2f586805136c60a2cd0f47b6b6aa Mon Sep 17 00:00:00 2001 From: Pascal Baljet Date: Fri, 20 Jun 2025 16:29:26 +0200 Subject: [PATCH 2/2] Added default env for `inertia.ssr.dispatch_without_bundle` --- config/inertia.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/inertia.php b/config/inertia.php index 88557bb3..4b1d57f7 100644 --- a/config/inertia.php +++ b/config/inertia.php @@ -25,6 +25,8 @@ 'url' => env('INERTIA_SSR_URL', 'http://127.0.0.1:13714'), + 'dispatch_without_bundle' => (bool) env('INERTIA_SSR_DISPATCH_WITHOUT_BUNDLE', false), + // 'bundle' => base_path('bootstrap/ssr/ssr.mjs'), ],