diff --git a/config/inertia.php b/config/inertia.php index bab52f25..4b1d57f7 100644 --- a/config/inertia.php +++ b/config/inertia.php @@ -25,10 +25,42 @@ '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'), ], + /* + |-------------------------------------------------------------------------- + | 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 +71,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); + } }