Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSR Support #51

Closed
arthur-er opened this issue Jan 9, 2022 · 14 comments
Closed

SSR Support #51

arthur-er opened this issue Jan 9, 2022 · 14 comments

Comments

@arthur-er
Copy link
Contributor

SSR Support for Inertia is officially out, and should be here as well

Currrently Inertia docs suggests running a separate server for rendering the pages as the official adapters don't run in NodeJS, but as Adonis is a NodeJS framework we can add support for Inertia SSR on the same server.

I'm currently using SSR without support from the adapter (using a view global that renders and returns the inertia page) and its working as a breeze.

My suggestion to implement this is just including a SSR Render config function on the already existing inertia config that receives the render function to support different front-end frameworks and include the SSR request in the existing @inertia edge tag with a extra @inertiaHead tag to match Inertia Laravel implementation

I'm willing to work in this if approved

@eidellev
Copy link
Owner

Thank you. I completely agree and was planing on starting to implement SSR later this week. If you have a working implementation and would like to open a PR that would be great. Please note that I just added a contribution guide to the readme.
Let me know if you have any questions. This is really exciting :-)

@arthur-er
Copy link
Contributor Author

arthur-er commented Jan 11, 2022

I don't know exactly how to fit it into the package, current it works like that:

// start/view.ts
import React from 'react'
import ReactDOMServer from 'react-dom/server'
import View from '@ioc:Adonis/Core/View'

View.global('ssr', (page) => {
  try {
    return createInertiaApp({
      resolve: (name: string) => require(`Resources/Pages/${name}`).default,
      page,
      render: ReactDOMServer.renderToString,
      setup: ({ App, props }) => React.createElement(App, processProps(props)),
    })
  } catch (error) {
    return undefined
  }
})
@set('ssr_page', await ssr(page))
<!DOCTYPE html>
<html>
  <head>
    @each(element in ssr_page?.head)
      {{{ element }}}
    @end
  </head>
  <body>
    @if(ssr_page)
      {{{ ssr_page.body }}}
    @else
      @inertia()
    @end
  </body>
</html>

@kevin-DL
Copy link

I don't know exactly how to fit it into the package, current it works like that:

// start/view.ts
import React from 'react'
import ReactDOMServer from 'react-dom/server'
import View from '@ioc:Adonis/Core/View'

View.global('ssr', (page) => {
  try {
    return createInertiaApp({
      resolve: (name: string) => require(`Resources/Pages/${name}`).default,
      page,
      render: ReactDOMServer.renderToString,
      setup: ({ App, props }) => React.createElement(App, processProps(props)),
    })
  } catch (error) {
    return undefined
  }
})
@set('ssr_page', await ssr(page))
<!DOCTYPE html>
<html>
  <head>
    @each(element in ssr_page?.head)
      {{{ element }}}
    @end
  </head>
  <body>
    @if(ssr_page)
      {{{ ssr_page.body }}}
    @else
      @inertia()
    @end
  </body>
</html>

Hello, this looks great, would it work the same way for Vue?

@arthur-er
Copy link
Contributor Author

Yes, just swap the react part of inertia setup for the vue ones

@kevin-DL
Copy link

kevin-DL commented Feb 23, 2022 via email

@kevin-DL
Copy link

kevin-DL commented Mar 2, 2022

So i tried like this

View.global('ssr', async (page) => {
  try {
    return createInertiaApp({
      resolve: (name) => require(`Resources/js/Pages/${name}.vue`),
      page,
      render: renderToString,
      setup: ({ app, props, plugin }) => {
        return createSSRApp({
          render: () => h(app, props),
        }).use(plugin)
      },
    }).catch((e) => {
      console.log({
        e,
      })
    })
  } catch (error) {
    console.log({
      error,
    })
    return undefined
  }
})

but i keep getting an error

`
^

SyntaxError: Unexpected token '<'`

Do you have any idea what i am doing wrong?

Cheers.

@justDare
Copy link

Any update on this one for out-of-the-box package support? Or does anyone have a working example they'd be open to sharing?

I'm close to a working react example I'd be able to share but still running into some webpack issues with TSX files.

@eidellev
Copy link
Owner

@justDare official support is coming eventually (I just had a busy couple of months with the whole covid-19 situation).
meanwhile, if you manage to make things work with your react example I'd be happy if you would consider opening a PR.

@eidellev
Copy link
Owner

SSR support for react was added in 6.1.0
Vue and svelte will be added soon
I'm keeping this ticket open until then

@arthur-er thank you for you example code, it really helped!

@EsTharian
Copy link

EsTharian commented Mar 26, 2022

SSR support at v6.1.0 breaks the others, mine is Svelte and I got below error. @eidellev

edit: v6.0.1 works well 👍

image

@eidellev
Copy link
Owner

@EsTharian thank you for reporting this. i'm already working on a fix + support for vue3.
meanwhile you can safely downgrade.

@bradley-varol
Copy link

Vue and svelte will be added soon

Sorry to be that guy but is there an ETA on the Vue SSR support? I'm about to start a project and want to make sure it's going to be supported. Thanks 🙌🏻

@eidellev
Copy link
Owner

eidellev commented Jun 5, 2022

It's a little hard for me to commit to a timeline, this being a side project of mine, but support for Vue is coming.

@eidellev
Copy link
Owner

Vue is officially supported in v7.0.0 onward! 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants