Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 36 additions & 17 deletions config/inertia.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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' => [
Expand Down
7 changes: 7 additions & 0 deletions src/ComponentNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Inertia;

use InvalidArgumentException;

class ComponentNotFoundException extends InvalidArgumentException {}
17 changes: 17 additions & 0 deletions src/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Support\Facades\Response as BaseResponse;
use Illuminate\Support\Traits\Macroable;
use Inertia\Support\Header;
use InvalidArgumentException;
use Symfony\Component\HttpFoundation\RedirectResponse as SymfonyRedirect;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;

Expand Down Expand Up @@ -158,11 +159,27 @@ public function always($value): AlwaysProp
return new AlwaysProp($value);
}

/**
* @throws ComponentNotFoundException
*/
protected function findComponentOrFail(string $component): void
{
try {
app('inertia.view-finder')->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();
}
Expand Down
12 changes: 10 additions & 2 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
);
});
}
Expand Down
19 changes: 19 additions & 0 deletions tests/ResponseFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}