Skip to content

Commit

Permalink
pass ziggy data as props
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmajor committed Oct 10, 2024
1 parent c63aa6e commit 52ff527
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 9 deletions.
2 changes: 2 additions & 0 deletions app/Http/Middleware/HandleInertiaRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Request;
use Inertia\Middleware;
use Tighten\Ziggy\Ziggy;

class HandleInertiaRequests extends Middleware
{
Expand All @@ -15,6 +16,7 @@ public function share(Request $request): array
return [
...parent::share($request),
'user' => $request->user(),
'ziggy' => (new Ziggy())->toArray(),
];
}
}
4 changes: 1 addition & 3 deletions config/ziggy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
return [

'output' => [
'path' => 'resources/js/helpers/ziggy.js',
'path' => 'resources/js/types/ziggy.js',
],

'skip-route-function' => true,

'only' => [
'ajax.*',
'tales.*',
Expand Down
3 changes: 3 additions & 0 deletions resources/js/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ document.startViewTransition ??= (cb: () => void) => cb();
createInertiaApp({
resolve,
setup({ el, App, props }) {
// @ts-expect-error
globalThis.Ziggy = props.initialPage.props.ziggy;

// @ts-expect-error
mount(App, { target: el, props });
},
Expand Down
File renamed without changes.
5 changes: 0 additions & 5 deletions resources/js/types/ziggy.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion resources/views/app.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
@vite(['resources/js/browser.ts', "resources/js/Pages/{$page['component']}.svelte"])
@endunless

@routes
@inertiaHead

@production
Expand Down
7 changes: 7 additions & 0 deletions tests/Unit/InertiaSharedPropsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Inertia\Testing\AssertableInertia as Assert;
use PHPUnit\Framework\Attributes\TestDox;
use Tests\TestCase;
use Tighten\Ziggy\Ziggy;

final class InertiaSharedPropsTest extends TestCase
{
Expand All @@ -28,9 +29,12 @@ public function testShareGuest(): void
->get('inertia-shared-props-test')
->assertOk()
->assertInertia(function (Assert $page) {
$ziggy = (new Ziggy())->toarray();

$this->assertSame([
'errors' => [],
'user' => null,
'ziggy' => $ziggy,
], $page->toArray()['props']);
});
}
Expand All @@ -43,12 +47,15 @@ public function testShareUser(): void
->get('inertia-shared-props-test')
->assertOk()
->assertInertia(function (Assert $page) use ($user) {
$ziggy = (new Ziggy())->toarray();

$this->assertSame([
'errors' => [],
'user' => [
'username' => $user->username,
'id' => $user->id,
],
'ziggy' => $ziggy,
], $page->toArray()['props']);
});
}
Expand Down

0 comments on commit 52ff527

Please sign in to comment.