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

quintype-toddy-libs

v1.5.0

Published

Libraries to help build Quintype Node.js apps

Downloads

28

Readme

Quintype Toddy Libs

This is a set of libraries that is to be used to build a Quintype Node App. This README servers as documentation of the architecture. Please see malibu for a reference application using this architecture.

Architecture

Isomorphic flow

Server Side Flow

  1. If no 'regular' route is caught, it goes to the isomorphic handler
  2. The current route is matched via matchBestRoute (see routing)
  3. If a route is matched, we load data via the loadData(pageType) function.
  4. A redux store is created based on the loaded data
  5. We render the IsomorphicComponent, which determines which page to render based on pageType, from the store

Client Side Flow

  1. The startApp() function starts as soon as the JS loads (async)
  2. The startApp() function calls /route-data.json?route=/current/path.
  3. The server looks at /current/path, matching it against its known routes, and sends back the pageType, and data from loadData(pageType)
  4. A redux store is created based on the loaded data
  5. We render the IsomorphicComponent, which determines which page to render based on pageType, from the store

Links between pages

  1. The client is loaded, and you click on a link, there should be no need to reload the page
  2. Instead, the link should make a call to /route-data.json?route=/current/path, and continue from step 2 of client side app

Service Worker

  1. Service Workers act as a proxy between your browser, and all network requests (including XHR, Assets, etc...). A service worker is registered by the app.js
  2. When the service worker gets registered, it downloads a minimum set of files for offline use. Typically, this includes [/shell.html, app.js, app.css] and others
  3. When you go to a page in the browser, the service worker wakes up. It decides if it can handle the request (by matching against the same routes), and renders the shell.html if possible
  4. If the shell was rendered, the JS will wake up and continue with the client flow from step 4
  5. If no shell was rendered, the call will fallback to the server, and proceed normally.

Service Worker - API Caching (not implemented in app)

  1. TODO - Service workers can also cache API requests, so that your app works totally offline

Routing

This app aims to be a Progressive Web App. Instead of guessing routes, it looks at the config to dynamically generate the required routes. For example, with sections /politics and /politics/karnataka, it will generate the following routes: [/politics, /politics/karnataka, /politics/:storySlug, /politics/*/:storySlug].

These routes are exposed via the generateRoutes function, and matched using the matchBestRoute function. This is embedded in three places:

  • Server, for server side rendering
  • The Service Worker, for deciding which pages are part of the PWA
  • The Client js,

Implementing a new page

In your server.js, you will notice something like the following

isomorphicRoutes(app, {
  generateRoutes: generateRoutes,
  loadData: loadData,
  pickComponent: pickComponent,
});

This highlights the three important places to put stuff for an isomorphic app

  • Match the route against a pageType, typically in app/server/routes.js (see the routing section above)
  • Load the Data Required for that pageType, typically in app/server/load-data.js. This returns a promise with required data.
  • Render the correct component for that pageType, typically in app/isomorphic/pick-component.js. This must be a pure component

Useful Components

BreakingNews

This component will automatically fetch breaking news every 30 seconds, and render the provided view.

import { renderBreakingNews } from 'quintype-toddy-libs/client/start';
const BreakingNewsView = (props) => <ul>{props.breakingNews.map((news) => <li key={news.id}>{news.headline}</li>)}</ul>
renderBreakingNews('breaking-news-container', store, BreakingNewsView);

ClientSideOnly

This component will be loaded by client, and bypassed when doing server side rendering.

const { ClientSideOnly } = require("quintype-toddy-libs/components/client-side-only");
<ClientSideOnly>
  This will be shown only on the client side
</ClientSideOnly>

InfiniteScroll

This component can be used to implement InfiniteScroll. This is an internal component.

InfiniteStoryBase

This component can be used to implement InfiniteScroll on the story page. You will need to specify the function which renders the story (which will recieve props.index and props.story), and functions for triggering analytics.

const React = require("react");

const { BlankStory } = require("../story-templates/blank.jsx");
const { InfiniteStoryBase } = require("quintype-toddy-libs/components/infinite-story-base");

function StoryPageBase({index, story, otherProp}) {
  // Can switch to a different template based story-template, or only show a spoiler if index > 0
  return <BlankStory story={story} />
}

const FIELDS = "id,headline,slug,url,hero-image-s3-key,hero-image-metadata,first-published-at,last-published-at,alternative,published-at,author-name,author-id,sections,story-template,tags,cards";
function storyPageLoadItems(pageNumber) {
  return global.superagent
           .get("/api/v1/stories", {fields: FIELDS, limit:5, offset:5*pageNumber})
           .then(response => response.body.stories.map(story => ({story: story, otherProp: "value"})));
}

function StoryPage(props) {
  return <InfiniteStoryBase {...props}
                            render={StoryPageBase}
                            loadItems={storyPageLoadItems}
                            onItemFocus={(item) => console.log(`Story In View: ${item.story.headline}`)}
                            onInitialItemFocus={(item) => console.log(`Do Analytics ${item.story.headline}`)} />
}

exports.StoryPage = StoryPage;

Link

This component generates an anchor tag. Instead of doing a browser page load, it will go to the next page via AJAX. Analytics scripts will be fired correctly (and if not, it's a bug)

const { Link } = require("quintype-toddy-libs/components/link");
<Link href="/section/story-slug" otherLinkAttribute="value">Text here</Link>

LoadingIndicator

This component renders it's children when the app is moving between pages. It can be used to show a spinner. It always has the class "loading-indicator", and also "loading-indicator-loading" when loading.

const { LoadingIndicator } = require("quintype-toddy-libs/components/loading-indicator");

<LoadingIndicator>
  <div className="spinner">Please Wait</div>
</LoadingIndicator>

NavigationComponentBase

This is a base component which must be subclassed, providing a navigateTo function.

class SearchComponent extends require("quintype-toddy-libs/components/navigation-component-base") {
  render() { return <a href="#" onClick={() => this.navigateTo("/some-page-here")}>Link</a>}
}

ResponsiveImage

This component takes an image, and resizes it to the correct aspect ratio using imgix or thumbor.

const { ResponsiveImage } = require("quintype-toddy-libs/components/responsive-image");
<figure className="story-grid-item-image qt-image-16x9">
  <ResponsiveImage slug={props.story["hero-image-s3-key"]} metadata={props.story["hero-image-metadata"]}
    aspectRatio={[16,9]}
    defaultWidth={480} widths={[250,480,640]} sizes="(max-width: 500px) 98%, (max-width: 768px) 48%, 23%"
    imgParams={{auto:['format', 'compress']}}/>
</figure>

StoryElement

This component renders different types of story elements

const { StoryElement } = require("quintype-toddy-libs/components/story-element");
function StoryCard(props){
  return <div>
    {props.card['story-elements'].map((element, index) => <StoryElement element={element} key={index} story={props.story}></StoryElement>)}
  </div>
}

References

  • This architecture is heavily influenced by the method described in this video
  • Code for the available video is available here
  • I know there is a good tutorial video I've seen. But I can't remember where.
  • Great intro to pwa