skyrpex / laravel-nuxt-js

Build a SPA with Laravel and Nuxt.
https://www.npmjs.com/package/laravel-nuxt
MIT License
146 stars 20 forks source link

A way to create meta tags in server side #50

Open ramirezfer772 opened 3 years ago

ramirezfer772 commented 3 years ago

Hi, since laravel-nuxt has no server side rendering, is there any way to create meta tags that crawlers could read? right now my meta tags are unreadable for facebook and twitter and I'm totally stuck now, I can't imagine a way to make my meta tags being render in server side. Any idea will be appreciated, thanks

skyrpex commented 3 years ago

You should be able to define static HTML attributes using the nuxt.config.js file, like stated here: https://nuxtjs.org/api/configuration-head/.

In the laravel-nuxt build step, this HTML file is generated, and then served in production by the Pallares\LaravelNuxt\Controllers\NuxtController PHP class.

If you need dynamic attributes, you would need to extend the NuxtController, parse the HTML and add the attributes by yourself. Something like this:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Pallares\LaravelNuxt\Controllers\NuxtController as BaseNuxtController;

class NuxtController extends BaseNuxtController
{
    public function __invoke(Request $request)
    {
        $html = parent::__invoke($request);

        // Try dumping the variable. It's a plain HTML string like: <html><head>...</head><body>...</body></html>.
        // In the past, it worked for me to use a regex to insert dynamic content just before the </head> tag.

        return $html;
    }
}

And don't forget to use your customized NuxtController in the routes/web.php.