Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

azbx's avatar
Level 17

SyntaxError: "undefined" is not valid JSON

When i use inertia ssr, i cannot see my css and i get this error.

what can i do to fix this

ssr.ts

app.ts

1 like
24 replies
Tray2's avatar

That error basically means that the variable you are expecting to be json isn't.

It is either empty or missing.

1 like
azbx's avatar
Level 17

@Tray2 so this could mean something like an axios call to an api could be failing

1 like
azbx's avatar
Level 17

@JussiMannisto well this happens globally but its something with inertia. I have speculated that it's from the sidebar as in react sometimes if localstorage is undefined then it returns that but I'm in vuejs.

JussiMannisto's avatar

@azbx Look at the error in the console. It doesn't just show an error message, it also shows where the error originates. That might give you a clue.

azbx's avatar
Level 17

@JussiMannisto sometimes there is no error, it just goes to the inertia json dataset code. In non-ssr inertia it works and has no error.

azbx's avatar
Level 17

@Tray2 could it also be a localStorage issue?

JussiMannisto's avatar

@azbx

it just goes to the inertia json dataset code

What does this mean?

Whenever there's an error in the browser console, it will show you the file and line number where it occurred, e.g. somefile.js:55, plus the stack trace. If you click on that, it will show you exactly where the error comes from. It might not tell you the root cause of the error, but it's a starting point.

Guessing isn't debugging. Investigate.

JussiMannisto's avatar

@azbx You're using built assets instead of npm run dev, so the minified code makes debugging harder. But it looks like the erring function has the same signature as createInertiaApp. It also has a JSON parsing function that tries to load something called dataset.page. That brings up a question about your ssr.ts:

.then((app) => {
	const appRender = app;
	appRender.body = appRender.body.replace(/data-page=".*?"/, "");
	return appRender;
})

Why is this code there? It strips away the data-page attribute from the server-rendered HTML. But that attribute is used for passing data, such as props, to the client as serialized JSON. My guess is that createInertiaApp() in your app.js is trying to parse the contents of data-page, but it crashes because the attribute is empty.

azbx's avatar
Level 17

@JussiMannisto i jsut went into dev mode and got this

app.ts:52 [Vue warn]: Hydration node mismatch:

  • rendered on server: (start of fragment)
  • expected on client: Symbol(v-cmt) at <Sidebar image="/assets/img/site-banners/market-background.svg" superBanActive=true OfficialImageBackground=true >
Tray2's avatar

@azbx It can basically be any variable that your code expects to be json, and isn't.

I suggest that you take a look in the console, it usually tells you on which line the code is failing.

1 like
JussiMannisto's avatar

@azbx Nevermind that. That's just a warning and a complete separate issue. Switching to run dev won't fix the JSON bug that you have.

Did you try removing the code that I showed, and did it fix the JSON parsing error? I'm pretty sure that's the source of the bug, and Inertia cannot work properly if you remove that attribute on the server side. Why is the attribute removed to begin with?

1 like
azbx's avatar
Level 17

@JussiMannisto i removed it to make the html output cleaner. it has worked with ssr before.

JussiMannisto's avatar

@azbx It may have worked until you rebuilt the assets and restarted the SSR server. But I really doubt Inertia could've worked after that.

I don't recommend deleting things unless you understand their purpose.

JussiMannisto's avatar
Level 50

@azbx Just to confirm this, I spun up an SSR site using Breeze:

composer create-project laravel/laravel vue-ssr && cd vue-ssr
composer require laravel/breeze && php artisan breeze:install vue --ssr
php artisan inertia:start-ssr

And everything worked perfectly as you can see in this screenshot.

I then added your data-page clearing code snippet to ssr.js and recompiled the assets. Can you guess the result?

Uncaught (in promise) SyntaxError: "undefined" is not valid JSON
    at JSON.parse (<anonymous>)
    at Nb (app-DFS2u8Ov.js:92:37344)
    at app-DFS2u8Ov.js:92:56471
1 like
azbx's avatar
Level 17

@JussiMannisto i realized when i removed it that i also had it on another load balencer server.

when i changed it there, the issue was resolved.

1 like

Please or to participate in this conversation.