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

NielsNumbers's avatar

How to use Vue & Blade without flash of unstyled content?

What I have seen in the tutorials from this site (See VUE series or bulding a forum) the way VUE is used is something like this:

<div id="app">
    @include('layouts.nav')
    @yield('content')
    <flash message="Hi"></flash>
</div>

and in app.js we got something like:

Vue.component('flash', require('./components/Flash.vue').default);

const app = new Vue({
    el: '#app',
});

By the default setup this will inject the correct DOM elements and the CSS via JS on pageload, so the page will only render, after JS has been loaded and executed, leading to a flash of unstyled content.

Laravel-mix offers the option to extract Vue styles in a separate css file, so it does not need to be injected by JS.

The only remaining problem is, that the something like <flash message=".."> needs to be replaced with some valid HTML like <div class="flash-message"><span>..</span></div>. The time it takes for JS to replace it, will lead to a flash of unstyled content.

I found that this package https://github.com/spatie/laravel-server-side-rendering allows server-side JS rendering, so when the page loads, the DOM and CSS should be correct, before JS takes place. But instead of the above <div id="app">..</div> one has to use {!! ssr('js/app-server.js') !!}.

If I understand it correctly, then this implies that for every page, the complete content has to be written in a vue file and one can't use blade syntax anymore.

Am I missing anything? If I have to write every site into a vue file, than I cannot only use blade syntax, there will be also no point of passing values from controller to the view - this can't be right?

I noticed that even laracast.com is using some server-side-rendering for VUE, because if I load the page with JS disabled, I will still see the page with all the correct DOM-Elements - however, I cannot image that Jeffrey has stopped using Blade engine for SSR?

So my question is basically, how can I use Vue & Blade with pre-rendered DOM elements, to avoid flash of unstyled content?

0 likes
7 replies
BryanK's avatar

The <flash></flash> will get replaced by what you have in your template area on the component.

Since you didn't share your Flash.vue code, it's impossible to say what is going on. You need to make sure that v-show is set to some var that is initially false and then is only set true if there's a message from the session or an event.

<template>
    <div class="alert alert-flash"
         :class="'alert-'+level"
         role="alert"
         v-show="show"
         v-html="body">
    </div>
</template>

You have your message set to a permanent "Hi".

Your styling should be in the <style></style>area of the component and can alter anything you wish.

NielsNumbers's avatar

Thank you for the answer.

Sorry I thought its not important how the Flash.vue looks. Lets use as an example the following:

<template>
<div class="message">
{{ message }}
</div>
</template>

<script>
export default {
    mounted() {
        console.log('Component mounted.')
    },
    probs: ['message']
}
</script>

<style>
.message{
	color: green;
}
</style>

My question remains, how can something like

<div id="app">
    @include('layouts.nav')
    @yield('content')
    <flash message="Hi"></flash>
</div>

be pre-rendered on the server?

In other words, how can I retrieve

<div id="app">
     ....

    <div class="message">Hi</div>
</div> 

from the server when JS is disabled.

BryanK's avatar

I don't understand what you're trying to do.

If JS is disabled, then <flash message="Hi"></flash> will remain. It will not be transformed into the template data.

Are you trying to get this working without JS? Or are you trying to get this working with Vue?

rhand's avatar

@elenktik sorry to get back to this ancient thread here, but did you ever manage to do SSR properly without a short time of unsettled content?

NielsNumbers's avatar
NielsNumbers
OP
Best Answer
Level 10

@rhand Indeed, I recall this post from when I first began exploring Vue. I no longer encounter the issue of unsettled content appearing for a brief period. My suspicion is that this issue arose primarily because I was in development mode and continuously running "npm run dev". Once I switch to production mode with "npm run prod", I don't experience any unsettled content.

Currently, I'm utilizing inertia.js. As per its functionality, once a page is loaded, any subsequent pages will be loaded via JavaScript (https://inertiajs.com/links). This approach has effectively addressed the problem.

Server-side rendering (SSR) is primarily beneficial for SEO purposes. With Inertia.js, setting up SSR is a straightforward process, as detailed here: https://inertiajs.com/server-side-rendering.

However, I haven't incorporated SSR into my application. The architecture of my app consists of two sections. The private dashboard is entirely developed using Vue with help of Inertia.js. On the other hand, the public section of my website is purely crafted with Blade and native JavaScript. Consequently, I don't face any SEO-related challenges.

2 likes
rhand's avatar

@Elenktik Thanks for getting back on this. All clear. We will work on making our Vue / Vue Router including JSON content load properly for SEO to parse with ease so we will have need for an SSR implementation. But that is a different story. Just checking in here to understand bottlenecks. Thanks again.

Please or to participate in this conversation.