Module

routes

This namespace exports multiple utility functions for setting up routes

import { upstreamQuintypeRoutes, isomorphicRoutes, getWithConfig, proxyGetRequest } from "@quintype/framework/server/routes";

View Source server/routes.js, line 1

Methods

static isomorphicRoutes(app, opts)

isomorphicRoutes brings all the moving parts of the server side rendering together. It accepts all the pieces needed, and implements all the plumbing to make these pieces work together.

Note that isomorphicRoutes adds a route that matches /*, so it should be near the end of your app/server/app.js.

Parameters:
Name Type Description
app Express

Express app to add the routes to

opts Object

Options

generateRoutes function

A function that generates routes to be matched given a config. See routing for more information. This call should be memoized, as it's called on every request

renderLayout function

A function that renders the layout given the content injected by isomorphicRoutes. See renderLayout

loadData function

An async function that loads data for the page, given the pageType. See loadData

pickComponent function

An async function that picks the correct component for rendering each pageType. See pickComponent

loadErrorData function

An async function that loads data if there is an error. If handleNotFound is set to true, this function is also called to load data for the 404 page

seo SEO

An SEO object that will generate html tags for each page. See @quintype/seo

manifestFn function

An async function that accepts the config, and returns content for the /manifest.json. Common fields like name, start_url will be populated by default, but can be owerwritten. If not set, then manifest will not be generated.

assetLinkFn function

An async function that accepts config and returns { packageName, authorizedKeys } for the Android /.well-known/assetlinks.json. If not implemented, then AssetLinks will return a 404.

oneSignalServiceWorkers boolean

Deprecated: If set to true, then generate /OneSignalSKDWorker.js which combines the Quintype worker as well as OneSignal's worker. (default: false). Please see https://developers.quintype.com/malibu/tutorial/onesignal

staticRoutes *

WIP: List of static routes

serviceWorkerPaths Array.<string>

List of paths to host the service worker on (default: ["/service-worker.js"])s

appVersion number

The version of this app. In case there is a version mismatch between server and client, then client will update ServiceWorker in the background. See app/isomorphic/app-version.js.

preloadJs boolean

Return a Link header preloading JS files. In h/2 compatible browsers, this Js will be pushed. (default: false)

preloadRouteData boolean

Return a Link header preloading /route-data.json. In h/2 compatible browsers, this Js will be pushed. (default: false)

handleCustomRoute boolean

If the page is not matched as an isomorphic route, then match against a static page or redirect in the CMS, and behave accordingly. Note, this runs after the isomorphic routes, so any live stories or sections will take precedence over a redirection set up in the editor. (default: true)

handleNotFound boolean

If set to true, then handle 404 pages with pageType set to "not-found". (default: true)

redirectRootLevelStories boolean

If set to true, then stories URLs without a section (at /:storySlug) will redirect to the canonical url (default: false)

mobileApiEnabled boolean

If set to true, then /mobile-data.json will respond to mobile API requests. This is primarily used by the React Native starter kit. (default: true)

templateOptions boolean

If set to true, then /template-options.json will return a list of available components so that components can be sorted in the CMS. This reads data from config/template-options.yml. See Adding a homepage component for more details

View Source server/routes.js, line 212

static proxyGetRequest(app, route, handler, opts)

proxyGetRequest can be used to forward requests to another host, and cache the results on our CDN. This can be done as follows in app/server/app.js.

proxyGetRequest(app, "/path/to/:resource.json", (params) => `https://example.com/${params.resource}.json`, {logError})

The handler can return the following:

  • null / undefined - The result will be a 503
  • any truthy value - The result will be returned as a 200 with the result as content
  • A url starting with http(s) - The URL will be fetched and content will be returned according to the above two rules
Parameters:
Name Type Description
app Express

The app to add the route to

route string

The new route

handler function

A function which takes params and returns a URL to proxy

opts
cacheControl

The cache control header to set on proxied requests (default: "public,max-age=15,s-maxage=240,stale-while-revalidate=300,stale-if-error=3600")

View Source server/routes.js, line 315

static upstreamQuintypeRoutes(app, opts)

upstreamQuintypeRoutes connects various routes directly to the upstream API server.

Requests like /api/* and /stories.rss are directly forwarded, but also it is also possible to forward other routes.

Parameters:
Name Type Description
app Express

The express app to add the routes to

opts Object

Options

extraRoutes Array.<string>

Additionally forward some routes upstream. This takes an array of express compatible routes, such as ["/foo/*"]

forwardAmp boolean

Forward amp story routes upstream (default false)

forwardFavicon boolean

Forward favicon requests to the CMS (default false)

View Source server/routes.js, line 32

inner getWithConfig(app, route, handler, opts)

Use getWithConfig to handle GET requests. The handle that is accepted is of type module:routes~Handler, which is similar to an express handler, but already has a client initialized, and the config fetched from the server.

Parameters:
Name Type Description
app Express

Express app to add the route to

route string

The route to implement

handler module:routes~Handler

The Handler to run

opts Object

Options that will be passed to the handler. These options will be merged with a config and client

View Source server/routes.js, line 174

Type Definitions

Handler

A handler is an extension of an express handler. Handlers are declared with the following arguments

function handler(req, res, next, { config, client, ...opts }) {
 // do something cool
}

View Source server/routes.js, line 155