npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@treehouse/contentful

v0.2.2

Published

Content model wrappers for Contentful API.

Downloads

1

Readme

Contentful wrapper

Caching and entity wrapper to sync content from a Contentful space and integrate in your web application. The purpose of this module is to provide a layer of abstraction between your site and the Contentful API for the most common use case: rendering your content from the server.

Getting started

By default, the Contentful wrapper uses cache-manager to cache everything in memory. It is highly recommended in Production to use Redis or some other cache backend external to your web dyno which will allow you to scale horizontally in response to traffic.

The environment that you run your application in will determine the Contentful API that is used. In production the standard API is used to return only published content. Otherwise, the Preview API is used which includes both published and unpublished content. By default, process.env.NODE_ENV is used to determine the environment; you can also override in the configuration.

Basic setup

const Contentful = require('@treehouse/contentful');
const ContentAPI = new Contentful('SPACEID', {
  accessToken: 'PRODUCTION_ACCESS_TOKEN',
  previewToken: 'PREVIEW_API_TOKEN'
});

ContentAPI.syncPaged().then(() => {
  // Content sync complete!
});

Wrapper configuration

All entries are wrapped in a convenience class that performs markdown parsing, determines slug URLs, and even wraps image URLs in a decorator function. Configuration can be controlled with the wrapper key during initialization:

const ContentAPI = new Contentful('SPACEID', {
  accessToken: 'PRODUCTION_ACCESS_TOKEN',
  previewToken: 'PREVIEW_API_TOKEN',
  wrapper: {
    lang: 'en-US',
    urlMapping: {
      blogPosts: 'blog'
    }
  }
});

Detailed documentation on the available methods and configuration options for the wrapper class can be found here.

Routing

As content is sync'd a routing map is built based on the slug in Contentful that can be used in your application to serve the page contents. By default, the slug is determined based on the machine ID of the entry in Contentful. In many cases this may be sufficient, but if you inherit an existing content model or use any of the default content types you may want to override this. To do so, pass a keyed object to config.wrapper.urlMapping where the key is the machine ID of the content type in Contentful and the value is the path to use in routing:

const ContentAPI = new Contentful('SPACEID', {
  ...
  wrapper: {
    urlMapping: {
      blogPosts: 'blog'
    }
  }
});

This configuration will map the blogPosts content type in Contentful to the route /blog. An entry with the slug my-awesome-blog-post will have a final route of /blog/my-awesome-blog-post

The final cached routing map is a keyed object where the key represents the final resolved path. The value is an array with two values that can be used for lookup of the content entity from cache: the first value corresponding to the machine ID of the content type and the second value corresponding to the ID of the content. So the example above would produce the following routing map:

routes: {
  '/blog/my-awesome-blog-post': ['blogPosts', '3iJsN1jc5qQGa8cwmesQQq']
}

When content is removed from cache the route is removed as well.