butschster / LaravelMetaTags

The most powerful and extendable tools for managing SEO Meta Tags in your Laravel project
MIT License
547 stars 51 forks source link

Method `addScript()` not working #47

Closed Shonetow closed 2 years ago

Shonetow commented 2 years ago

Describe the bug

There's no displayed error with this one, it's just the script tag that doesn't show up in the browser's source code.

// MetaTagsServiceProvider.php

protected function packages()
{
    // *NOTE THE COMMENTS BELLOW...
    PackageManager::create('app', function (Package $package) {
        $package->addScript('app.js', asset('js/app.js'), ['defer']);
    });
}

protected function registerMeta(): void
{
  $this->app->singleton(MetaInterface::class, function () {
      $meta = new Meta(
          $this->app[ManagerInterface::class],
          $this->app['config']
      );

      $meta->initialize();

      $meta->addLink('app.css', [
          'href' => asset('css/app.css'),
          'rel'  => 'stylesheet'
      ]);

      $meta->addLink('bootstrap-icons', [
          'rel'  => 'stylesheet',
          'href' => 'https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css'
      ]);

      // FIRST I TRIED THIS AND IT'S NOT WORKING...
      $meta->addScript('app.js', asset('js/app.js'), ['defer']);
      // THEN I TRIED IN THIS WAY BUT DOESN'T WORK EITHER*
      $meta->includePackages(['app']);

      return $meta;
  });
}
<!doctype html>
<html lang="en" class="h-100">
<head>
    <!-- META -->
    <title>My App</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="597dIm5sGMaTTI90tbPTs9l2QYFsJEOULSQi4Vwx">
<link href="http://myapp.test/css/app.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.8.1/font/bootstrap-icons.css">    <!-- END META -->
</head>

Expected behavior I expect to see <script src="http://myapp.test/js/app.js" defer></script> in the <head> tag on every page.

butschster commented 2 years ago

Hi, @Shonetow!

The fourth argument if method addScript specifies a place where it will be shown. By default the place is footer and will be shown in place where you put {!! Meta::footer()->toHtml() !!}

If you want to show it in a head {!! Meta::toHtml() !!}, then

 $meta->addScript('app.js', asset('js/app.js'), ['defer'], 'head');
Shonetow commented 2 years ago

Thanks, @butschster!

Solved.

butschster commented 2 years ago

Thanks, @butschster!

Solved.

You are welcome!