3

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
  • Please add they way you defined route Commented Feb 4, 2022 at 16:14
  • 1
    If someone has same problem please check github.com/inertiajs/inertia/issues/1083 Commented Feb 5, 2022 at 3:49
  • Hello Parth, First of all, you can upgrade from webpack to vite and if you face an issue, then the "bee bee" solution is working fine. Commented Sep 29, 2023 at 15:12

4 Answers 4

2

First you need to get ziggy-js

npm install ziggy-js

Then run

php artisan ziggy:generate

Which should create a ziggy.js file in the resources/js directory. After modify your ssr.js to

import React from 'react'
import ReactDOMServer from 'react-dom/server'
import { createInertiaApp } from '@inertiajs/inertia-react'
import createServer from '@inertiajs/server'
import route from "ziggy-js";
import { Ziggy } from '@/ziggy'


createServer((page) => createInertiaApp({
    page,
    render: ReactDOMServer.renderToString,
    resolve: name => require(`./Pages/${name}`),
    setup: ({ App, props }) => {

        // Set global function route
        global.route = (name, params, absolute, config = Ziggy) => route(name, params, absolute, config);

        return <App {...props} />
    },
}))

Solution by JefteCaro

More solutions in case you'd like to read more

Sign up to request clarification or add additional context in comments.

3 Comments

Hello bee bee, This solution worked for me in the latest laravel 10 and inertia js version. Thanks a lot.
One question is that, Why such important information is not updated on the official documentation. It is mandatory to do this way, otherwise we are getting this above mentioned error.
It works perfectly for me! Thanks, You saved my lot time!!!!
0

Have you tried this? It was in the Routing - Inertia.

app.config.globalProperties.$route = route

Comments

0

replace

<script src="{{ mix('js/app.js') }}" defer></script>

with

<script src="{{ mix('js/ssr.js') }}" defer></script>

in /resources/views/app.blade.php

Comments

0

In my own case, I was getting the error: ReferenceError: route is not defined even after trying out all the suggestions above. This was because in my app, I use the helper route('name') to create all my URLs. What I did to fix it was to add route to the globalThis module, which makes the route helper available for Node.js.

This works for me:

createServer((page) => {
    globalThis.route<RouteName> = (name, params, absolute) =>
        route(name, params, absolute, {
            ...page.props.ziggy,
            location: new URL(page.props.ziggy.location),
        });

    return createInertiaApp({
        page,
        render: ReactDOMServer.renderToString,
        title: (title) => `${title} - ${appName}`,
        resolve: async (name) => {
            const pages = import.meta.glob("./Pages/**/*.tsx");
            let page: any = pages[`./Pages/${name}.tsx`];

            if (typeof page === "function") {
                page = await page();
            }

            if (typeof page === "undefined") {
                throw new Error(`Page not found: ${name}`);
            }

            if (!page.default.layout) {
                const publicPageFolders = ["Public/"];

                const isPublicPage = publicPageFolders.some((folder) =>
                    name.startsWith(folder),
                );

                if (isPublicPage) {
                    page.default.layout = (page: any) => (
                        <GuestLayout children={page} />
                    );
                }
            }

            return page;
        },
        setup: ({ App, props }) => {
            return <App {...props} />;
        },
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.