Coder Social home page Coder Social logo

bnomei / kirby3-htmlhead Goto Github PK

View Code? Open in Web Editor NEW
20.0 1.0 0.0 270 KB

Kirby Plugin for a best-practice HTML Head Element extendable with snippets

Home Page: https://forum.getkirby.com/t/kirby3-htmlhead-best-practice-html-head-element-extendable-with-snippets/23585

License: MIT License

PHP 99.61% CSS 0.23% JavaScript 0.16%
kirby3 kirby3-cms kirby3-plugin html5 head best-practice rss-feed google-analytics typekit google-webfonts

kirby3-htmlhead's Introduction

Kirby 3 HTMLHead

Release Downloads Build Status Coverage Status Maintainability Twitter

Kirby 3 Plugin for a best-practice HTML Head Element extendable with snippets.

Commercial Usage


Support open source!

This plugin is free but if you use it in a commercial project please consider to sponsor me or make a donation.
If my work helped you to make some cash it seems fair to me that I might get a little reward as well, right?

Be kind. Share a little. Thanks.

‐ Bruno
 
M O N E Y
Github sponsor Patreon Buy Me a Coffee Paypal dontation Hire me

Installation

  • unzip master.zip as folder site/plugins/kirby3-htmlhead or
  • git submodule add https://github.com/bnomei/kirby3-htmlhead.git site/plugins/kirby3-htmlhead or
  • composer require bnomei/kirby3-htmlhead

Works well with

Usage

Site Method: attrLang

There is a language helper available as well.

<?php ?>
<html <?= site()->langAttr() ?>>
<!-- ... -->
</html>

Page Method: htmlhead

In any template or your header snippet call the page method right after the tags that should come first.

   <!DOCTYPE html>
-  <html>
+  <html <?= site()->langAttr() ?>>
     <head>
-      <meta charset="utf-8">
-      <meta http-equiv="x-ua-compatible" content="ie=edge">
-      <meta name="viewport" content="width=device-width, initial-scale=1 shrink-to-fit=no">
-      <base href="<?= site()->url() ?>'">'
-      <title><?= $page->title() ?></title>
+      <?= $page->htmlhead() ?>

You can also override any defaults in forwarding the new or additional data to the page method.

<?= $page->htmlhead([
    // override defaults
    'htmlhead/meta-description' => function ($kirby, $site, $page) {
        return Str::unhtml($page->myDescriptionField()->kti());
    },
    // add new
    'htmlhead/link-feedrss' => function ($kirby, $site, $page) {
        return []; // use defaults of snippet
    },
]) ?>

But I would recommend that you configure which snippets are use with the config settings (see below).

Setting

There is only one setting called bnomei.htmlhead.snippets and it takes an array of callbacks. It has sensible defaults but you can add any of the provided snippets or your own snippets. The unittests for this plugin have a more specific example for you to explore.

https://github.com/bnomei/kirby3-htmlhead/blob/master/tests/config/config.php

<?php

return [
    'bnomei.htmlhead.snippets' => [
        /********************************
         * IMPORTANT: order matters! based on research from @csswizardry
         */
        'htmlhead/recommended-minimum' => null,
        'htmlhead/title' => function ($kirby, $site, $page) {
            return ['title' => $page->title()];
        },
        // async first then sync js scripts
        'htmlhead/script-js' => function ($kirby, $site, $page) {
            return ['files' => [
                '/assets/app.js'            
            ]];
        },
        'htmlhead/link-css' => function ($kirby, $site, $page) {
            return ['files' => ['/assets/app.css']];
        },
        'htmlhead/link-preload' => function ($kirby, $site, $page) {
            return ['files' => ['/assets/app.css', '/endpoint/data.json']];
        },
        // deferred scripts after sync css is faster
        'htmlhead/script-js-defer' => function ($kirby, $site, $page) {
            return ['files' => [
                [
                    'src' => '//unpkg.com/alpinejs', 
                    'defer' => true,
                ],        
            ]];
        },
        'htmlhead/link-prefetch' => function ($kirby, $site, $page) {
            return ['files' => ['/assets/next-page.js']];
        },
        'htmlhead/base' => function ($kirby, $site, $page) {
            return ['href' => kirby()->site()->url()];
        },
        'htmlhead/meta-robots' => function ($kirby, $site, $page) {
            return ['content' => 'index, follow, noodp'];
        },
        'htmlhead/meta-author' => function ($kirby, $site, $page) {
            return ['content' => $site->author()];
        },
        'htmlhead/meta-description' => function ($kirby, $site, $page) {
            return ['content' => Str::unhtml($page->seodesc())];
        },
        'htmlhead/link-feedrss' => function ($kirby, $site, $page) {
            // defaults
            return [
                'url' => url('/feed'),
                'title' => $page->title(),
            ];
        },
        'htmlhead/link-feedrss' => function ($kirby, $site, $page) {
            // defaults
            return [
                'url' => url('/feed'),
                'title' => $page->title(),
            ];
        },
    ],
    // ... other options
];

Disclaimer

This plugin is provided "as is" with no guarantee. Use it at your own risk and always test it yourself before using it in a production environment. If you find any issues, please create a new issue.

License

MIT

It is discouraged to use this plugin in any project that promotes racism, sexism, homophobia, animal abuse, violence or any other form of hate speech.

kirby3-htmlhead's People

Contributors

bnomei avatar reynaert1250 avatar s1syphos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

kirby3-htmlhead's Issues

Add async scripts

Is there a way to output scripts with the async attribute?

If not can you add it?

Idea: External css / js integrity

Hey there,
I'd like to incorporate an external CSS file in 'bnomei.htmlhead.css', and while kirby3-fingerprint is installed, there's currently no option to include its integrity value.

[FAQ] Styles not applied before JS is executed – lazysizes issue

Question:
Lazysizes does not calculate size correctly. Images are all lowrez/pixelated.

Answer:
if using loadjs from this plugin you probably have to use the css and js to load the main css file and lazysizes javascript file. That way you force the browser to wait for the css to be loaded and applied before starting lazysizes.

return [
    'bnomei.htmlhead.css' => [
        '/assets/css/styles.min.css',  // <-- sharp
    ],
    'bnomei.htmlhead.js' => [
        '/assets/js/lazysizes.min.js',  // <-- sharp
    ],
    'bnomei.htmlhead.loadjs' => [
        '*' => [
            'template' => [],
            'dependencies' => [
                // '/assets/css/styles.min.css',  // <-- low-rez
                // '/assets/js/lazysizes.min.js', // <-- low-rez
                '/assets/js/main.min.js'
            ]
        ],
    ],
];

Skip <base> completely

Can you please add an option to skip the addition of < base > completely?
I want to add anchor links and don't need the < base > element.
I tried it with null but it doesn't skip generating the line and still makes anchor links impossible.

    'htmlhead/base' => function ($kirby, $site, $page) {
      return ['href' => 'null'];
    },

SEO / Opengraph / Twitter cards

Hey there,
great plugin, many thanks are in order - now, it would be great to have an example how you populate SEO / Opengraph meta tags (with custom page methods etc as mentioned in README.md), like you did on your devkit page?

PR regarding some minor fixes is up and running 🍙

// Edit: I'm open to adding twitter card support, too. And what's your standing on og:locale and og:description? being included by default?

Cheers!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.