Skip to content

Commit 8889bbc

Browse files
committed
Adding updates to ssr file to include global route var
1 parent 7cae674 commit 8889bbc

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

app/Http/Middleware/HandleInertiaRequests.php

Lines changed: 7 additions & 3 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
{
@@ -38,13 +39,16 @@ public function share(Request $request): array
3839
{
3940
[$message, $author] = str(Inspiring::quotes()->random())->explode('-');
4041

41-
return [
42-
...parent::share($request),
42+
return array_merge(parent::share($request), [
4343
'name' => config('app.name'),
4444
'quote' => ['message' => trim($message), 'author' => trim($author)],
4545
'auth' => [
4646
'user' => $request->user(),
4747
],
48-
];
48+
'ziggy' => [
49+
...(new Ziggy)->toArray(),
50+
'location' => $request->url(),
51+
],
52+
]);
4953
}
5054
}

resources/js/layouts/settings/Layout.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Heading from '@/components/Heading.vue';
33
import { Button } from '@/components/ui/button';
44
import { Separator } from '@/components/ui/separator';
55
import { type NavItem } from '@/types';
6-
import { Link } from '@inertiajs/vue3';
6+
import { Link, usePage } from '@inertiajs/vue3';
77
88
const sidebarNavItems: NavItem[] = [
99
{
@@ -20,8 +20,9 @@ const sidebarNavItems: NavItem[] = [
2020
},
2121
];
2222
23-
// For SSR, we can't access the window location
24-
const currentPath = (typeof window === 'undefined') ? '' : window.location.pathname;
23+
// Get the current path from Ziggy config
24+
const page = usePage();
25+
const currentPath = page.props.ziggy?.location ? new URL(page.props.ziggy.location).pathname : '';
2526
</script>
2627

2728
<template>

resources/js/ssr.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ createServer((page) =>
1414
title: (title) => `${title} - ${appName}`,
1515
resolve: (name) => resolvePageComponent(`./pages/${name}.vue`, import.meta.glob('./pages/**/*.vue')),
1616
setup({ App, props, plugin }) {
17+
const app = createSSRApp({ render: () => h(App, props) })
18+
1719
// Configure Ziggy for SSR
1820
const ziggyConfig = {
1921
...page.props.ziggy,
@@ -24,21 +26,14 @@ createServer((page) =>
2426
const route = (name: string, params?: any, absolute?: boolean) =>
2527
ziggyRoute(name, params, absolute, ziggyConfig)
2628

29+
// Make route function available globally
30+
app.config.globalProperties.route = route
31+
2732
// Make route function available globally for SSR
2833
if (typeof window === 'undefined') {
2934
global.route = route
3035
}
3136

32-
// Create the SSR app with route function in context
33-
const app = createSSRApp({
34-
render: () => h(App, props),
35-
setup() {
36-
return {
37-
route,
38-
}
39-
},
40-
})
41-
4237
app.use(plugin)
4338

4439
return app

0 commit comments

Comments
 (0)