question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SSR for React; adapter differences between React + Vue

See original GitHub issue

Discussed in https://github.com/inertiajs/inertia/discussions/1081

<div type='discussions-op-text'>

Originally posted by ZeoKnight February 4, 2022 Having read @aarondfrancis comments here https://github.com/tighten/ziggy/issues/431 and useful blog post https://aaronfrancis.com/2022/using-ziggy-with-inertia-server-side-rendering for dealing with global scope window issues.

How do we approach this in React? the features available within the vue adaptor simply aren’t present in the react adaptor?

#Vue adaptor
import {createInertiaApp} from '@inertiajs/inertia-vue3'
createServer((page) => createInertiaApp({
    page,
    render: renderToString,
    resolve: name => require(`./Pages/${name}`),
    setup({app, props, plugin}) {
        const Ziggy = {
            // Pull the Ziggy config off of the props.
            ...props.initialPage.props.ziggy,
            // Build the location, since there is
            // no window.location in Node.
            location: new URL(props.initialPage.props.ziggy.url)
        }
 
        return createSSRApp({
            render: () => h(app, props),
        }).use(plugin).mixin({ 
            methods: {
                route: (name, params, absolute, config = Ziggy) => route(name, params, absolute, config),
            },
        })
    },
}))

#React Adaptor
import {createInertiaApp} from '@inertiajs/inertia-react'
createServer((page) => createInertiaApp({
  page,
  render: ReactDOMServer.renderToString,
  resolve: name => require(`./Pages/${name}`),
  setup: ({ App, props }) => <App {...props} />,
}))

Note the setup method does not have plugins or uses a helper method createSSRApp - react setup simply returns the component + props.

</div>

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:11

github_iconTop GitHub Comments

5reactions
rdgoutcommented, Feb 28, 2022

To offer another solution: I followed parts of @aarondfrancis his blog post but swapped out the end for this:

createServer(page => {
    globalThis.Ziggy = page.props.ziggy;

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

You see, ziggy by default already tries to find a ziggy instance in globalThis (see https://github.com/tighten/ziggy/blob/b3ebdd831a9da9dd9839c611f7aff7d82d1bf159/src/js/Router.js#L17). This allows me to use the regular route function from ziggy without having to write a wrapper. Hope it helps someone!

1reaction
vampiregreycommented, Feb 5, 2022

@ZeoKnight @aarondfrancis I am not sure if this is the right approach but this worked for me.

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

createServer((page) => createInertiaApp({
    page,
    render: ReactDOMServer.renderToString,
    resolve: name => require(`./Pages/${name}`),
    setup: ({ App, props }) => {
        const Ziggy = {
            // Pull the Ziggy config off of the props.
            ...props.initialPage.props.ziggy,
            // Build the location, since there is
            // no window.location in Node.
            location: new URL(props.initialPage.props.ziggy.url)
        }

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

        return <App {...props} />
    },
}))
Read more comments on GitHub >

github_iconTop Results From Across the Web

adapter differences between React + Vue · Discussion #1081 ...
How do we approach this in React? the features available within the vue adaptor simply aren't present in the react adaptor?
Read more >
I created the exact same app in React and Vue. Here are the ...
The only difference here is that the React app has three CSS files, whereas the Vue app doesn't have any. The reason for...
Read more >
Fundamental Differences Between React and Vue - Medium
Take a look at the following code snippets: Both of them are Hello components, the first is in React and the other is...
Read more >
SSR Frameworks: Next.js vs SvelteKit vs Nuxt vs Quasar vs ...
1. Introduction 2. React SSR frameworks 3. Vue.js SSR frameworks
Read more >
Server Rendering in JavaScript: Why SSR?
By default you can have Svelte/Vue/React front-ends together with Laravel/Ruby backends. Then other community-based adapters also exist for ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found