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

vincent15000's avatar

Laravel 11 / VueJS 3 / Vite / How to SSR ?

Hello,

I'd like to configure SSR for my application. I specify that I don't use Inertia.

I have read and configured SSR like mentioned in the documentation.

https://laravel.com/docs/11.x/vite#ssr

But it seems to be too easy : what should be the content of the ssr.js file ?

What anything do I have to do to have SSR ?

I never used SSR and it's totally new for me.

Thanks for your help.

V

0 likes
9 replies
LaryAI's avatar
Level 58

To set up Server-Side Rendering (SSR) with Laravel 11, VueJS 3, and Vite, you need to follow a few steps. The Laravel documentation provides a good starting point, but let's go into more detail, especially about the ssr.js file.

Step-by-Step Guide

  1. Install Dependencies: Ensure you have the necessary dependencies installed. You need vite, @vitejs/plugin-vue, and @vitejs/plugin-vue-jsx.

    npm install vite @vitejs/plugin-vue @vitejs/plugin-vue-jsx
    
  2. Configure Vite: Create or update your vite.config.js file to include the Vue plugin and SSR settings.

    import { defineConfig } from 'vite';
    import vue from '@vitejs/plugin-vue';
    import vueJsx from '@vitejs/plugin-vue-jsx';
    
    export default defineConfig({
      plugins: [vue(), vueJsx()],
      ssr: {
        noExternal: ['vue', '@vue/server-renderer']
      }
    });
    
  3. Create ssr.js: This file is responsible for rendering your Vue components on the server side. Here is a basic example of what ssr.js might look like:

    import { createSSRApp } from 'vue';
    import { renderToString } from '@vue/server-renderer';
    import App from './src/App.vue';
    
    export async function render(url, manifest) {
      const app = createSSRApp(App);
    
      // You can add any additional setup here, like Vue Router or Vuex
    
      const appHtml = await renderToString(app);
    
      return {
        appHtml
      };
    }
    
  4. Update Laravel Routes: In your Laravel routes file (e.g., web.php), you need to set up a route that will handle SSR.

    use Illuminate\Support\Facades\Route;
    use Illuminate\Support\Facades\File;
    use Illuminate\Support\Facades\App;
    
    Route::get('/{any}', function () {
        $ssr = App::make('vite.ssr');
        $manifest = json_decode(File::get(public_path('build/manifest.json')), true);
    
        $result = $ssr->render('/', $manifest);
    
        return view('app', [
            'ssr' => $result['appHtml']
        ]);
    })->where('any', '.*');
    
  5. Create a Blade Template: Create a Blade template (e.g., resources/views/app.blade.php) to serve the SSR content.

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>My SSR App</title>
        @vite('resources/js/app.js')
    </head>
    <body>
        <div id="app">{!! $ssr !!}</div>
    </body>
    </html>
    
  6. Run Vite in SSR Mode: Finally, you need to run Vite in SSR mode. You can add a script to your package.json to make this easier.

    "scripts": {
      "dev": "vite",
      "build": "vite build",
      "ssr": "vite build --ssr"
    }
    

    Run the SSR build with:

    npm run ssr
    

Summary

By following these steps, you should be able to set up SSR for your Laravel 11 application using VueJS 3 and Vite. The key part is the ssr.js file, which handles the server-side rendering of your Vue components. Make sure to adjust the configurations and routes according to your application's needs.

vincent15000's avatar

Can somebody help me ?

I don't think that the AI response is relevant.

vincent15000's avatar

@Udev Yes but I the application with Laravel + VueJS already exists.

If it's really necessary, I could migrate the application to Nuxt, but for the moment I prefer trying to do it manually.

martinbean's avatar
Level 80

@vincent15000 Why use Vue in the first place if you want server-side rendering? You’re now just sending data from a server to the client that you want the server to render; you could just skip the whole thing if you just used server-side rendering in the first place.

1 like
vincent15000's avatar

@martinbean All what I have read about SSR let me think that it's not a good idea to set up SSR for my application, there are too many low level configuration to do.

The best to do is either keep the application without SSR, or migrate it to Nuxt.

martinbean's avatar

Because it's a real time application which needs great reactivity.

@vincent15000 And? You can add interactivity to server-rendered apps.

You’re just going in a full circle here. You’re using a server to render a client-side app that you now want to render on the server.

Please or to participate in this conversation.