0.1.16 • Published 4 years ago

node-metas v0.1.16

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

Node Metas

Note: This is a very early preview. So right now I expect to introduce breaking changes with each release.

"Node Metas" is a NodeJS library that starts a production ready NodeJS Webserver, serves a specific dir and allows you to manipulate the meta headers inside the index.html.

This is super useful if you only need to add server-side rendered meta tags but you don't want to render the whole app serverside.

Yes, but why ?!?

I had to use NextJS in a bunch of projects and it's not that I don't like next, I'm just not a big fan of the strict structure. Your whole app needs to follow the NextJS logic for the server-side renderer to work. But in a lot of cases I only really needed it for some meta tags (for example for social media sharing). That's why this library follows a much simpler approach. This being said it works with any kind of framework. It just takes the public dir, serves the files and adds meta tags for routes that are defined under "routes".

API

At the core this lib provides the possibility to define routes and to pass a function (sync or async) that returns an object of { name: content } meta tags.

{
  path: '/',
  metas: request => {
    return {
      'name-of-the-meta': 'content of the meta',
      url: '/',
    }
  },
}

Full example

import nodeMetas from './lib/index';
import fetch from 'node-fetch';

nodeMetas({
  routes: [
    {
      path: '/user/:id/',
      metas: request => ({
        'user-id': 'id' in request.params ? request.params.id : '',
        hello: 'world',
      }),
    },
    {
      path: '/post/:id/',
      metas: async request => { // can be async
        const id = 'id' in request.params ? request.params.id : 0;
        let metas = {};
        try {
          const resp = await (
            await fetch(`https://sayhello.ch/wp-json/wp/v2/posts/${id}/`)
          ).json();
          metas = {
            title: resp.title.rendered,
          };
        } catch (error) {
          console.log('Error for /post/:id/');
        }
        return metas;
      },
    },
  ],
  port: 3000, // optional
  indexFile: 'index.gtml', // optional
  serveDir: 'dist/', // optional
});
0.1.16

4 years ago

0.1.15

4 years ago

0.1.10

4 years ago

0.1.11

4 years ago

0.1.12

4 years ago

0.1.13

4 years ago

0.1.14

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.4

4 years ago

0.1.5

4 years ago

0.1.0

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.3

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago