inertiajs / inertia-rails

The Rails adapter for Inertia.js.
https://inertiajs.com
MIT License
539 stars 43 forks source link

JS Error On Non-Inertia Pages #134

Open jakemumu opened 2 days ago

jakemumu commented 2 days ago

Hi! Just getting setup with inertia and it's awesome -- I do notice this error though. I'm only planning to use inertia on some pages of my rails app -- but I initialize it on my entrypoint/application.js

When I hit a route which doesn't not render via inertia I get this crash from the initialization of inertia:

Screenshot 2024-10-11 at 10 08 10 AM Screenshot 2024-10-11 at 10 06 42 AM

Is there a way to avoid this? -- I don't see a way at the moment

PedroAugustoRamalhoDuarte commented 2 days ago

@jakemumu to fix that you have to create two layouts and two JavaScript entrypoints; One for Inertia and one for your normal rails views

jakemumu commented 2 days ago

@jakemumu to fix that you have to create two layouts and two JavaScript entrypoints; One for Inertia and one for your normal rails views

nice!!!

Is there any way for me to help with the project documentation? maybe there is a gotcha or FAQ? -- I would never have guessed that honestly -- wouldn't it be possible to make the library handle that?

jakemumu commented 2 days ago

For anyone that comes across this here is a solution which makes it possible to retain the same layout & entry point you're already using:

// detect if the data is on the page which indicates it's an inertia render
if (document.querySelector('[data-page]')) {
  createInertiaApp({
    resolve: name => import(`../frontend/${name}.svelte`),
    setup({ el, App, props }) {
      new App({ target: el, props });
    },
  });
}