Load head metadata from a manifest file which can be shared with a SPA project

Overview

Laravel Head Manifest

Installation

Step 1: Add Laravel Head Manifest to your laravel project

composer require critiq/laravel-head-manifest

Step 2: Add LaravelHeadManifestServiceProvider to your app's configured providers

In app/config/app.php:

 'providers' => [

        ... // Other providers,

        App\Providers\RouteServiceProvider::class,
        + Critiq\LaravelHeadManifest\LaravelHeadManifestServiceProvider::class,

Step 3: Create your manifest file

In your public folder, create head-manifest.json with the following base structure:

{
    "defaultTitle": "Welcome to Laravel Head Manifest",
    "globalMeta": [],
    "defaultMeta": [],
    "paths": {
        "test/1": {
            "title": "Test1 Title",
            "meta": [
                {
                    "name": "Description",
                    "property": "description",
                    "content": "This is a description"
                }
            ]
        },
        "test/2": {
            "title": "Test2 Title",
            "meta": []
        },
    }
    
}

Step 4: In your blade file(s), add the $metadata variable

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        +{!! $metadata !!}

        ... // Rest of blade template

Usage

Define your path structures

Basic path structure

In the manifest's paths values, you may define keys representing a path and an associated object containing the associated path's title and meta objects (more on meta objects later).

{
    ... // Rest of manifest
    "paths": {
        "/": {
            "title": "Home",
            "meta": [],
        }
    }
}

If you do not specify a title in the path object, it will fall back to your manifest's defaultTitle value.

Metadata structure

The metadata structure is a dictionary of attributes which directly translates to the attributes of an HTML meta tag

For instance:

... // Rest of manifest
"meta": [
    { "name": "description", "content": "This is a sample meta description"}
],

Will convert to:

<meta name="description" content="This is a sample meta description" />

Global Metadata

Specifying the root globalMeta list of metadata objects will always be added. This is useful if you need metadata to exist on every page.

Default Metadata

Specifying the root defaultMeta list of metadata objects will only be added if the request path isn't matched, or if a path's meta field isn't specified. If you want to omit the default metadata from a path, pass an empty array as the meta value.

Path variables

Laravel Head Manifest supports variable paths. While defining your path key, prefix a path section with : followed by the name of the variable.

Example:

{
    ... // Rest of manifest
    "paths": {
        "/page/:pageName": {
            "title": "You're visiting: :pageName",
        }
    }
}

If you were to navigate to /page/cats, this would match the /page/:pageName path and set pageName variable to "cats". You may reference this variable in any string within the path object.

Match all paths with prefix

If you would like to support matching all nested paths from a root path, your may append your path name with a *.

Example:

{
    ... // Rest of manifest
    "paths": {
        "/admin/*": {
            "title": "Secret admin dashboard",
        }
    }
}

Visiting paths such as /admin, /admin/banusers and /admin/purge/everything will all match the defined /admin/* path.

Get resolved path data from the server

Laravel Head Manifest includes a simple controller which may be used to resolve a path and return the head data for a specified path (without having to navigate to said path). This is in the form of the LaravelHeadManifestController. While there are many ways to use this controller, due to how Laravel's default RouteServiceProvider is structured, we recommend the following:

Step 1: In app/Http/Controllers, create a HeadManifestController.php file and define the controller with:

<?php

namespace App\Http\Controllers;

use Critiq\LaravelHeadManifest\LaravelHeadManifestController;

class HeadManifestController extends LaravelHeadManifestController { }

Step 2: In your web.php, create a GET route to your new controller

Route::get('head-manifest', 'HeadManifestController@index');

Note: Depending on your project structure, this may not work right away. I recommend reading up on how Laravel's routing system works (notably namespaces in the RouteServiceProvider). It's not essential to create your own HeadManifestController, you may instead route to the LaravelHeadManifestController class directly.

Why this project?

SPA apps don't always work well with our robot friends (Google crawlers, Facebook/Slack/Discord/etc unfurling, and many, many more). The goal of this project is to provide a way to elegantly define your head data for use in SPA contexts without sacrificing the benefits of page-specific metadata.

You might also like...
Authentication for SPA (Laravel Sanctum + Vue.js)

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Laravel Proxy Package for handling sessions when behind load balancers or other intermediaries.

Laravel Trusted Proxies Setting a trusted proxy allows for correct URL generation, redirecting, session handling and logging in Laravel when behind a

Load files and classes as lazy collections in Laravel.

Lody Load files and classes as lazy collections in Laravel. Installation composer require lorisleiva/lody Usage Lody enables you to fetch all exist

Automatically load your helpers in your laravel application.

Laravel AutoHelpers Automatically load your helpers in your laravel application. Installation You can install the package via composer: composer requi

 Load Laravel service providers based on your application's environment.
Load Laravel service providers based on your application's environment.

Laravel EnvProviders A more finetuned way of managing your service providers in Laravel. This package allows you to configure the environment certain

Load .env files for PHP.

PHP DotEnv Loader Simple library to load and get values from .env file(s). Install composer require murilo-perosa/dot-env How to Use Namespace use Mur

With dadjokes every time you load your control panel you'll be greeted by an epic dad joke on the dashboard.

Filament Dad Jokes Widget With DadJokes every time you load your control panel you'll be greeted by an epic dad joke on the dashboard. Installation Yo

Migrator is a GUI migration manager for Laravel which you can create, manage and delete your migration.
Migrator is a GUI migration manager for Laravel which you can create, manage and delete your migration.

Migrator Migrator is a GUI migration manager for Laravel which you can create, manage and delete your migration. Installation: To install Migrator you

A site which you can apply jobs or search for employees with cool functionalities..
A site which you can apply jobs or search for employees with cool functionalities..

About App An App which you can apply jobs or search for employees with cool functionalities. Some Pics of App Click Image to Zoom in Sign In & Up Empl

Owner
Critiq
Critiq
Renamify is a package for Laravel used to rename a file before uploaded to prevent replacing exists file which has the same name to this new uploaded file.

Renamify Laravel package for renaming file before uploaded on server. Renamify is a package for Laravel used to rename a file before uploaded to preve

MB'DUSENGE Callixte 2 Oct 11, 2022
Component for reading phar.io manifest information from a PHP Archive (PHAR)

Manifest Component for reading phar.io manifest information from a PHP Archive (PHAR). Installation You can add this library as a local, per-project d

null 7.1k Jan 9, 2023
Simple SSR Head for Inertia.js Laravel

Inertia.js Laravel SSR Head Simple SSR Head for Inertia Laravel, solve the social media metadata display of small Inertia.js x Laravel site. NOT a ful

Lucas Yang 12 Nov 14, 2022
Simple laravel hook for adding meta tags to head for inertia

laravel seo hook for js frameworks simple hook for adding meta tags to <head></head> for js frameworks inertia:react,vue, etc... in app/Meta.php put M

Razmik Ayvazyan 2 Aug 23, 2022
Shared code for the MaxMind Web Service PHP client APIs

Common Code for MaxMind Web Service Clients This is not intended for direct use by third parties. Rather, it is for shared code between MaxMind's vari

MaxMind 264 Jan 3, 2023
Flysystem storage with local metadata storage for speed and manageability.

Laravel Filer This project was started to scratch my itch on our growing Laravel site: Metadata for all files is stored in a local repository - Suppor

Nick Vahalik 16 May 23, 2022
Retrieve the EC2 Metadata using Laravel's eloquent syntax.

Laravel EC2 Metadata Retrieve the EC2 Metadata using Laravel's eloquent syntax. ?? Supporting If you are using one or more Renoki Co. open-source pack

Renoki Co. 4 Dec 27, 2021
Flow package to synchronize metadata and binary data of imported Neos.Media assets

Wwwision.AssetSync Flow package to synchronize metadata and resources of imported Neos.Media assets Installation Install this package via: composer re

Bastian Waidelich 5 Feb 7, 2022
Tiny hands is a Laravel multi-tenant boilerplate with SPA and i18n.

About Tiny Hands Tiny hands is a Laravel multi-tenant boilerplate with SPA and i18n using the following technology stack: Backend Laravel 8.0 API with

Bertrand Kintanar 12 Jun 23, 2022
Laravel Sanctum, Websockets & Vue [SPA]

Laravel Auth [SPA] 2021-07-15_22-24-33.mp4 composer require laravel/ui php artisan ui:auth Laravel Sanctum composer require laravel/sanctum php art

Kritish Dhaubanjar 6 Jun 1, 2022