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

@trilon/ng-universal

v3.0.1

Published

Angular Universal Schematics and Helpers - Brought to you by Trilon.io

Downloads

60

Readme

Angular Universal Schematics & Utility Helpers - Trilon

npm Minzipped Size MIT License



Installation

Install & save the library to your package.json:

$ npm i -S @trilon/ng-universal

Modules Available


NgUniversalModule Setup

Now add ApplicationInsightsModule to your Angular Root AppModule:

// Import the Application Insights module and the service provider
import { NgUniversalModule } from '@trilon/ng-universal';

@NgModule({
  imports: [
    // ...
    // Add the Module to your imports
    NgUniversalModule
  ]
})
export class AppModule { }

Angular Universal Helpers

Now that the Library is setup, you have a few great helpers to make Angular Universal a bit simpler and easier to work with!

Angular SEO - Meta & Link Generator

Handling Angular SEO and dynamic Meta & Link generation can be quite the task! Introducing easy-to-use helpers that allow you create a BASE Meta setup for your Application, and easily update the portions needed when visiting different pages/sections of your Application.

Setup a Base SEO Configuration

At the Root of your application utilize the SeoService to initialize a base setup for your Meta/Link/SEO needs. This way you will only have to update fragments of your Meta at different Routes/Components when needed, while this base structure will always be present.

Note: You can also reinitialize this at any part of your Application if you need a fundamentally different Base SEO setup. (ie: /blog/ sections for example, that will always need author|article setup)

import { SeoService } from '@trilon/ng-universal';

@Component({
  selector: 'app-root'
})
export class AppComponent {

  constructor(
    private seo: SeoService
  ) {
    const config: SeoConfig = {
      title: 'Trilon SeoService Demo',
      description: 'Trilon SEO - Description',
      locale: 'en_US',
      url: 'https://trilon.io',
      type: 'website',
      msapplicationTileColor: '#000',
      themeColor: '#fff',
      og: {
        site_name: 'Trilon Consulting',
        image_url: 'https://trilon.io/meta/og-image.png'
      },
      twitter: {
        image_url: 'https://trilon.io/meta/twitter-image.png',
        summary_card: 'summary_large_image',
      },
      keywords: 'trilon, nestjs consulting, nestjs courses, node consulting, angular consulting',
      article: {
        tags: ['seo', 'trilon', 'universal'],
        section: 'trilon'
      },
      link: [
        { rel: 'alternate', type: 'application/rss+xml', title: 'RSS', href: 'https://trilon.io' },
        { rel: 'canonical', href: 'https://trilon.io/blog' }
      ],
    };

    // initialize your base Meta setup
    // (this can be done again at any point if you need to replace it entirely)
    this.seo.initializeBaseMeta(config);
    // ^^^^
  }
}

Now let's say we've traveled to a different Route, and we want that Component to update a few important pieces of the SEO, without having to re-do everything.

export class TrilonBlogComponent {
  constructor(private seo: SeoService) {
    this.seo.update({
      title: 'Blog - Trilon.io',
      description: 'Learn more about NestJS, Angular and Fullstack Development at the Trilon Blog!'
      url: 'https://trilon.io/blog'
    })
  }
}

This will update just the necessary portions added above, while leaving everything else intact!

Angular Structured Data / ld+json / Rich Snippets

export class TrilonBlogComponent {
  constructor(private seo: SeoService) {
    this.seo.updateStructuredData(
      {
        "@context": "https://schema.org",
        "@type": "Organization",
        "url": "http://www.trilon.io",
        "name": "Fullstack Consulting",
        "contactPoint": {
          "@type": "ContactPoint",
          "website": "https://trilon.io",
          "contactType": "Consulting"
        }
      }
    )
  }
}

SeoService Methods:

  • updateStructuredData(json)
    • Set or Update your ld+json script structured data / rich snippets
  • initializeBaseMeta(SeoConfig)
    • Set your initial Meta setup for your entire application
  • SeoService.update(SeoConfig)
    • Update (even partially) the SEO/Meta

PlatformService

Typically in Angular Universal Applications you have sections of code that can only run in certain platforms (browser or server), with PlatformService you can simply add it in the constructor of any Component/Service within your application, and run code specific to that platform - without causing Errors in the other platform.

import { PlatformService } from '@trilon/ng-universal';

@Component({ /* ... */ })
export SomeComponent {
  constructor(private platformService: PlatformService) {
    if (platformService.isBrowser) {
      // Run browser-specific code that would cause errors in the Server/Node platform!
      // $('body').addClass('');
    }

    if (platformService.isServer) {
      // Run Server/Node-specific code
    }
  }
}

IsBrowser | IsService Directives

Equally important with Angular Universal is displaying only Components/UI that are neccessary for the given platform. To improve performance, or to avoid Components all-together we can use *isBrowser or *isServer Directives to display/hide specific things given a platform.

Take as an example a Twitter Feed section that's connected to 3rd party Components/Libraries and API-calls. We don't need Universal to display these as there is no SEO benefit, and most likely they will slow down our Render time. In this case it's most beneficial to simply avoid it entirely during server-side rendering, and have only the Browser display and render this Component!

<ng-container *isBrowser>
  <app-twitter-feed></app-twitter-feed>
</ng-container>

More Documentation & Utilies Coming soon...


TransferHttpCacheModule Setup

In order to prevent UI flickers with Angular Universal we want to make sure we're caching Http responses and re-using them during the client-side render. This Module helps not only Cache GET requests, but POST requests as well.

// Import the Application Insights module and the service provider
import { TransferHttpCacheModule } from '@trilon/ng-universal';

// Filter out which POST requests you -want- to Cache
export function cachePostFilter(req, key) {
  // If intercepted request URL contains any part of the below list, cache it
  const cacheList = ['/posts', '/products'];
  
  const cacheRequest = cacheList.filter(p => {
    // Test against current req.url that's intercepted
    if (req.url.includes(p)) {
      return true;
    }
    return false;
  }).length >= 0;

  return cacheRequest;
}

@NgModule({
  imports: [
    // ...
    // Add the Module to your imports
    TransferHttpCacheModule.forRoot({
      cachePOSTFilter: cachePostFilter
    })
  ]
})
export class AppModule { }

License

MIT License

Copyright (c) 2019 Trilon

Twitter Follow


Trilon Consulting

JavaScript, Node, NestJS Consulting from Open-Source Fanatics and Key Contributors!

Check out Trilon.io for more info!

Contact us at [email protected], and let's talk about your projects needs.