Skip to content

Commit

Permalink
[2.x] Fix Inertia SSR usage with Ziggy route function in setup() (#1069)
Browse files Browse the repository at this point in the history
* Use Ziggy Vue plugin instead of route mixin

Ziggy Vue plugin allows `route()` to be injected into `setup()` function in components[1][2], which is needed for Inertia SSR since the @routes blade directive isn't available.

[1]: tighten/ziggy#518
[2]: tighten/ziggy#564 (comment)

* Fix quotations

* Update app.js

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
prestonholt and taylorotwell authored Jun 17, 2022
1 parent 2b0887a commit d33e331
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 12 deletions.
3 changes: 2 additions & 1 deletion stubs/inertia/resources/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import './bootstrap';
import { createApp, h } from 'vue';
import { createInertiaApp } from '@inertiajs/inertia-vue3';
import { InertiaProgress } from '@inertiajs/progress';
import { ZiggyVue } from 'ziggy';

const appName = window.document.getElementsByTagName('title')[0]?.innerText || 'Laravel';

Expand All @@ -12,7 +13,7 @@ createInertiaApp({
setup({ el, app, props, plugin }) {
return createApp({ render: () => h(app, props) })
.use(plugin)
.mixin({ methods: { route } })
.use(ZiggyVue, Ziggy)
.mount(el);
},
});
Expand Down
14 changes: 4 additions & 10 deletions stubs/inertia/resources/js/ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createSSRApp, h } from 'vue';
import { renderToString } from '@vue/server-renderer';
import { createInertiaApp } from '@inertiajs/inertia-vue3';
import createServer from '@inertiajs/server';
import route from 'ziggy';
import { ZiggyVue } from 'ziggy';

const appName = 'Laravel';

Expand All @@ -15,15 +15,9 @@ createServer((page) =>
setup({ app, props, plugin }) {
return createSSRApp({ render: () => h(app, props) })
.use(plugin)
.mixin({
methods: {
route: (name, params, absolute) => {
return route(name, params, absolute, {
...page.props.ziggy,
location: new URL(page.props.ziggy.url),
});
},
},
.use(ZiggyVue, {
...page.props.ziggy,
location: new URL(page.props.ziggy.url),
});
},
})
Expand Down
1 change: 1 addition & 0 deletions stubs/inertia/webpack.mix.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mix.js('resources/js/app.js', 'public/js').vue()
])
.alias({
'@': 'resources/js',
ziggy: "vendor/tightenco/ziggy/dist/vue",
});

if (mix.inProduction()) {
Expand Down
2 changes: 1 addition & 1 deletion stubs/inertia/webpack.ssr.mix.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mix.js('resources/js/ssr.js', 'public/js')
})
.alias({
'@': 'resources/js',
ziggy: 'vendor/tightenco/ziggy/dist/index',
ziggy: 'vendor/tightenco/ziggy/dist/vue',
})
.webpackConfig({
target: 'node',
Expand Down

0 comments on commit d33e331

Please sign in to comment.