I am trying to use SSR inertiajs with react, in laravel.
Version details:
Php: 8.1.2
Laravel: 8.82.0
Breeze: 1.7.1
Inertiajs: 0.11.0
Npm: 8.1.2
/resources/js/ssr.js
createServer((page) => createInertiaApp({
page,
render: ReactDOMServer.renderToString,
resolve: name => require(`./Pages/${name}`),
setup: ({ App, props }) => <App {...props} />,
}))
/webpack.ssr.mix.js
mix
.options({ manifest: false })
.js('resources/js/ssr.js', 'public/js')
.react()
.alias({ '@': path.resolve('resources/js') })
.webpackConfig({
target: 'node',
externals: [nodeExternals()],
})
/resources/views/app.blade.php
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
...
<link rel="stylesheet" href="{{ mix('css/app.css') }}">
@routes
<script src="{{ mix('js/app.js') }}" defer></script>
@inertiaHead
</head>
<body class="font-sans antialiased">
@inertia
...
</body>
</html>
Error:
ReferenceError: route is not defined
at Welcome (/public/js/ssr.js:1413:19)
3
Answers
Have you tried this? It was in the Routing – Inertia.
app.config.globalProperties.$route = route
First you need to get ziggy-js
Then run
Which should create a ziggy.js file in the resources/js directory. After modify your ssr.js to
Solution by JefteCaro
More solutions in case you’d like to read more
replace
with
in /resources/views/app.blade.php