Skip to content

Commit 3e7b127

Browse files
committed
fix: add ziggy to inertia middleware for ssr
1 parent bb24863 commit 3e7b127

File tree

4 files changed

+13
-0
lines changed

4 files changed

+13
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ yarn-error.log
2121
/.nova
2222
/.vscode
2323
/.zed
24+
/bootstrap/ssr

app/Http/Middleware/HandleInertiaRequests.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Foundation\Inspiring;
66
use Illuminate\Http\Request;
77
use Inertia\Middleware;
8+
use Tighten\Ziggy\Ziggy;
89

910
class HandleInertiaRequests extends Middleware
1011
{
@@ -45,6 +46,10 @@ public function share(Request $request): array
4546
'auth' => [
4647
'user' => $request->user(),
4748
],
49+
'ziggy' => fn (): array => [
50+
...(new Ziggy)->toArray(),
51+
'location' => $request->url(),
52+
],
4853
];
4954
}
5055
}

resources/js/layouts/settings/layout.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ const sidebarNavItems: NavItem[] = [
2424
];
2525

2626
export default function SettingsLayout({ children }: { children: React.ReactNode }) {
27+
// For SSR, we can't access the window location, so can only render the layout on the client
28+
if (typeof window === 'undefined') {
29+
return null;
30+
}
31+
2732
const currentPath = window.location.pathname;
2833

2934
return (

resources/js/types/index.ts renamed to resources/js/types/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { LucideIcon } from 'lucide-react';
2+
import type { Config } from "ziggy-js";
23

34
export interface Auth {
45
user: User;
@@ -25,6 +26,7 @@ export interface SharedData {
2526
name: string;
2627
quote: { message: string; author: string };
2728
auth: Auth;
29+
ziggy: Config & { location: string };
2830
[key: string]: unknown;
2931
}
3032

0 commit comments

Comments
 (0)