inertiajs / inertia

Inertia.js lets you quickly build modern single-page React, Vue and Svelte apps using classic server-side routing and controllers.
https://inertiajs.com
MIT License
6.02k stars 405 forks source link

"undefined" is not valid JSON at JSON.parse in createInertiaApp() #1853

Closed whatnotery closed 2 months ago

whatnotery commented 2 months ago

Version:

Describe the problem:

I am trying to setup a rails application with inertia and svelte

I've got the following files set-up as per the documentation however it will not render the svelte component it errors in the console with this message:

Uncaught (in promise) SyntaxError: "undefined" is not valid JSON
    at JSON.parse (<anonymous>)
    at createInertiaApp (@inertiajs_svelte.js?v=5b546d2f:5036:37)
    at application.js:3:1

I saw this error referenced in #1358 as well although I'm not sure they're directly related if anyone has insight on how to resolve this I'd appreciate it

app/frontend/entrypoints/application.js

import { createInertiaApp } from '@inertiajs/svelte'

createInertiaApp({
  resolve: name => {
    const pages = import.meta.glob('./Pages/**/*.svelte', { eager: true })
    return pages[`./Pages/${name}.svelte`]
  },
  setup({ el, app, props }) {
    new app({ target: el, props })
  },
})

config/routes.rb

Rails.application.routes.draw do  
  root "static#home"
end

app/controllers/static_controller.rb

class StaticController < ApplicationController
    def home
      render inertia: 'app'
    end
  end

app/views/layouts/application.html.erb

<!DOCTYPE html>
<head>
    <%= vite_client_tag %>
    <%= vite_javascript_tag "application", defer: true %>
</head>
<body>
    <div id="app">
    </div>
</body>

app/frontend/pages/app.svelte

<h1>Hello World!</h1>
<p>This is the App component</p>

Steps to reproduce:

setup a rails/inertia/svelte app with the preceding set up

whatnotery commented 2 months ago

This was error on my part and I have resolved this