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

Pacific's avatar
Level 21

Bootstrap.js import on inertias ssr.js

Hello,

I have the inertia ssr configuration setup, everything works great but I have notice that one file is missing, the import to my bootstrap.js file is only on my app.js.

Do I need to put it on my ssr file too? What’s happening with axios and Echo are they being hydrated or something?

TBH I have no idea how it’s working and I would like to now a little bit more.

Thank you!

0 likes
3 replies
LaryAI's avatar
Level 58

When using Inertia.js with server-side rendering (SSR), you need to ensure that any client-side specific code is not executed on the server. This is because certain objects like window and document that are available in the browser are not available in a Node.js environment, which is typically where SSR is executed.

Your bootstrap.js file likely contains client-side setup code, such as initializing Axios for HTTP requests or setting up Laravel Echo for real-time events. If this file contains code that references browser-specific objects or is only meant to be run in the client, you should not import it directly into your SSR setup.

However, you still need to make sure that any necessary setup for Axios or Echo is available in the SSR context if you're making API calls or need real-time functionality during SSR. You can do this by creating a separate entry point for your SSR context or by conditionally running code based on the environment.

Here's an example of how you might conditionally import and execute your bootstrap.js file:

// ssr.js
import { createSSRApp } from 'vue';
import { renderToString } from '@vue/server-renderer';
import createInertiaApp from '@inertiajs/inertia-vue3/server';
import { InertiaApp } from '@inertiajs/inertia-vue3';

// Import your bootstrap file conditionally
if (typeof window !== 'undefined') {
  require('./bootstrap');
}

// ...rest of your SSR setup

In this example, the bootstrap.js file will only be imported if the window object is defined, which would only be the case in a browser environment.

For Axios, you can set up a default instance that can be used both on the server and the client. If you need to use Laravel Echo during SSR, you'll have to ensure that it doesn't rely on browser-specific objects or find a server-compatible alternative.

Remember that any state changes or side effects that happen during SSR need to be serialized and sent to the client, where they can be "hydrated" into a fully interactive app. This means that if you're setting up Axios defaults or Echo channels, you'll need to ensure that the client-side app can pick up where the server left off.

If you're using Inertia's SSR, you should also check the official documentation and community forums for any specific advice or best practices related to setting up your project for SSR.

1 like
gych's avatar
gych
Best Answer
Level 29

You don't have to include it in ssr.js, bootstrap js for example contains code that only works via client side rendering. Like you said in your post it will be hydrated.

As a small example window. doesn't work via ssr it only works via client browser.

2 likes
Pacific's avatar
Level 21

I didn’t thought on the window object on that regard.

Thank you!

1 like

Please or to participate in this conversation.