Skip to content

Instantly share code, notes, and snippets.

View arutnik's full-sized avatar

Andres Rutnik arutnik

View GitHub Profile
@arutnik
arutnik / example.js
Created September 17, 2019 16:42
ARR Node Js Instrumenting example
const createNamespace = require('cls-hooked').createNamespace;
const clsNamespace = createNamespace('mycomp');
//... Create middleware that adds the incoming header value to clsNamespace
//... Run the below code during server start up
require('./http-instrumenter')(console, clsNamespace);
@arutnik
arutnik / http-instrumenter.js
Created September 17, 2019 16:36
ARR Node Js Instrumenting
const http = require('http');
const https = require('https');
function urlToOptions(url) {
const options = {
protocol: url.protocol,
hostname: typeof url.hostname === 'string' && url.hostname.startsWith('[') ?
url.hostname.slice(1, -1) :
url.hostname,
hash: url.hash,
@arutnik
arutnik / app.component.ts
Last active August 2, 2018 19:38
SEO-SPA: Hiding with CSS
@Component({
selector: 'app-root',
template: `
<span class="hidden">This text is important for a
crawler to see, but I can't have it on screen
because I need to put my human users first and
they get to see a rich visual experience instead
of this text. So I hide it with CSS. Remember that
this will penalize this text's importance, but it's
better than not having it visible to crawlers at all.
@arutnik
arutnik / version-printer.service.ts
Created July 8, 2018 20:29
SEO-SPA: Transfer State
import { Injectable, Inject, PLATFORM_ID } from '@angular/core';
import { TransferState, makeStateKey } from '@angular/platform-browser';
import { isPlatformServer } from '@angular/common';
/**
* A service that when injected in the app component
* will print a version injected by the server
* once loaded on the client
*/
@Injectable()
@arutnik
arutnik / webpack.server.config.js
Created July 8, 2018 20:17
SEO-SPA: Strip lib from Webpack
plugins: [
new webpack.NormalModuleReplacementPlugin(/jquery-funpicker/, // Adding this rule strips jquery-funpicker
path.join(__dirname, 'webpack/empty.json')), // out of the server bundle
new webpack.ContextReplacementPlugin(
// fixes WARNING Critical dependency: the request of a dependency is an expression
/(.+)?angular(\\|\/)core(.+)?/,
path.join(__dirname, 'src'), // location of your src
{} // a map of your routes
),
new webpack.ContextReplacementPlugin(
@arutnik
arutnik / if-platform.directive.ts
Created July 8, 2018 20:14
SEO-SPA: If Platform Directive
import { Directive, Input, TemplateRef, ViewContainerRef, PLATFORM_ID, Inject } from '@angular/core';
import { isPlatformServer, isPlatformBrowser } from '@angular/common';
/**
* Adds the template content to the DOM only if the platform is the requested platform
*
* Usage to make a component only visible on client renders:
* <div *ifPlatform="'browser'">....</div>
*
* Usage to make a component only visible on server renders:
* <div *ifPlatform="'server'">...</div>
@arutnik
arutnik / element-focus.browser.service.ts
Created July 8, 2018 20:09
SEO-SPA: Browser DI Service
import { ElementFocusService } from './element-focus.service';
import { Injectable, ElementRef } from '@angular/core';
@Injectable()
export class ElementFocusBrowserService extends ElementFocusService {
constructor() {
super();
}
@arutnik
arutnik / element-focus.server.service.ts
Created July 8, 2018 20:06
SEO-SPA: Server DI Service
import { ElementFocusService } from './element-focus.service';
import { Injectable, ElementRef } from '@angular/core';
@Injectable()
export class ElementFocusServerService extends ElementFocusService {
constructor() {
super();
}
@arutnik
arutnik / element-focus.service.ts
Created July 8, 2018 20:05
SEO-SPA: Abstract DI Service
import { ElementRef } from '@angular/core';
export abstract class ElementFocusService {
/**
* Transfers focus to an element
* @param element
*/
public abstract focusElement(element: ElementRef)
}
@arutnik
arutnik / usage.ts
Created July 8, 2018 20:04
SEO-SPA: DI Service Usage
//In app.browser.module.ts providers:
{
provide: ElementFocusService,
useClass: ElementFocusBrowserService
},
//In app.server.module.ts providers:
{