Giter Club home page Giter Club logo

stencil-head's Introduction

Built With Stencil

DEPRECATE: The Stencil team finally released its own Stencil Helmet plugin https://stenciljs.com/docs/static-site-generation-meta-tags

Head Component

Head component for Stencil and Web Components

Handle document meta/head tags with web components an easy way.

Autocomplete|Intellisense for the most common properties, adding more every day.

Install

  • Run npm i stencil-head --save

Usage

import {HeadInterface, Head } from 'stencil-head'

const head: HeadInterface = {
      title: 'Home Page | app-head',
}
<Head data={head}/>

Example

import {Component, h} from '@stencil/core'
import {HeadInterface, Head } from 'stencil-head'

@Component({
  tag: 'app-home'
})
export class AppHome {

  render() {
    const head: HeadInterface = {
      title: 'Home Page | app-head',
      html: {
        lang: 'en'
      },
      metas: [{
        name: 'description',
        content: 'This is the description for the home page'
      },
      {
      property: 'og:description',
      content: 'This is the og:description for the home page'
      }],
    }
    return (
      <div>
        <Head data={head}/>
        <div class='app-home'>
          <h1>
            <app-translate keyLang="key"/>
          </h1>
          <p>
            Welcome to the Stencil App Starter.
            You can use this starter to build entire apps all with
            web components using Stencil!
            Check out our docs on <a href='https://stenciljs.com'>stenciljs.com</a> to get started.
          </p>
        </div>
      </div>
    )
  }
}

Note the declaration of the constant head This includes as a return interface HeadInterface that way you can get all the autocomplete in any IDE that supports Typescript

 export interface HeadInterface {
  html?: object;
  charset?: "utf-8" | "iso-8859-1" | string;
  title: string;
  metas?: MetaInterface[];
  links?: Link[];
  scripts?: Script[];
  styles?: string[];
  baseHref?: string;
}

In the following link you can see all properties and interfaces. https://github.com/chiqui3d/stencil-head/blob/master/src/interfaces.ts

Returns all Head properties in string format

import {HeadInterface, BaseHead } from 'stencil-head'

const head: HeadInterface = {
  title: 'Home Page | app-head',
  baseHref: 'http://localhost:3333/',
  metas: [{
    name: 'description',
    content: 'Esto es una descripción de la página de inicio'
  },
    {
      property: 'og:description',
      content: 'Esto es una descripción de la página de inicio'
    }],
  links: [{
    rel: 'stylesheet',
    href: 'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css',
    integrity: 'sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T',
    crossorigin: 'anonymous'
  },
    {
      href: 'https://fonts.googleapis.com/css?family=Roboto:100,900|Material+Icons'
    },
    {
      rel: 'canonical',
      href: 'https://github.com/chiqui3d/stencil-head'
    }],
  scripts: [{
    src: 'https://code.jquery.com/jquery-3.3.1.slim.min.js',
    integrity: 'sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo',
    crossorigin: 'anonymous'
  },
    {
      src: 'https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js',
      integrity: 'sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1',
      crossorigin: 'anonymous'
    },
    {
      src: 'https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js',
      integrity: 'sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM',
      crossorigin: 'anonymous'
    },
    {
      type: 'text/javascript',
      code: `console.log('Hello app-head');`
    }],
  styles: [
    `h2{color:blue}`
  ]
}
const headHtml = new BaseHead(head).html();
console.log(headHtml)
}

output:

<base href="http://localhost:3333/">
<title>Home Page | app-head</title>
<meta name="description" content="Esto es una descripción de la página de inicio">
<meta property="og:description" content="Esto es una descripción de la página de inicio">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Roboto:100,900|Material+Icons">
<link rel="canonical" href="https://github.com/chiqui3d/stencil-head">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script type="text/javascript">console.log('Hello app-head');</script>
<style type="text/css">h2{color:blue}</style>

Note

I'm currently using it for several static websites with the Stencil --prerender option https://stenciljs.com/docs/prerendering, and it works great. I just need to do some tests for the title and description to see how Google treats this currently when added by Javascript, although currently Google says that it can read Javascript.

Any recommendation of Typescript, Stencil or Writing will be welcome.

stencil-head's People

Contributors

chiqui3d avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

stencil-head's Issues

Can't add additional metas with SSR or Pre-render

Hi, I can't add additional meta tags to the page from a component when using a hydrated ssr app or prerender (browser is ok)...

TypeError: last.after is not a function at Meta.setMetas

However its fine modifying existing ones, see example and uncomment the og:title tag in index.html
https://github.com/acorcutt/stencil-ssr-now

There is a title on the page so it should still be finding a node to append to...

const last = lastMetaNode && lastMetaNode[lastMetaNode.length-1] || document.querySelector('head:first-child title')

SSR outputs before the meta update completes

Hi,

Occasionally the server/pre render completes before all the meta tags have been injected, sometimes it might write all the tags, others just half or none, so I'm guessing the async is not working properly.

I've tried adding a short delay to the Head component and waiting for the dom to show a change but still no luck - any ideas?

function timeout(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
await new BaseHead(data).createHead();
if (Build.isServer) {
    await timeout(500);
}

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.