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 ampRoutes(app, opts)

ampRoutes handles all the amp page routes using the @quintype/amp library routes matched: GET - "/amp/:slug"* returns amp story page GET - "/amp/api/v1/amp-infinite-scroll" returns the infinite scroll config JSON. Passed to component's src attribute

To disable amp version for a specific story, you need to create a story attribute in bold with the slug {disable-amp-for-single-story} and values {true} and {false}. Set its value to "true" in the story which you want to disable amp. Please make sure to name the attributes and values in the exact same way as mentioned attribute slug: "disable-amp-for-single-story" values: "true" , "false". This will redirect '/:slug' to the non-amp page

Parameters:
Name Type Description
app Express

Express app to add the routes to

opts Object

Options object used to configure amp. Passing this is optional

templates Object

An object that's used to pass custom templates. Each key corresponds to the template name and corresponding value is the template

slots Object

An object used to pass slot data.

seo SEO

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

enableAmp boolean | function

'amp/story/:slug' should redirect to non-amp page if enableAmp is false

redirectUrls object | function

list of urls which is used to redirect URL(sourceUrl) to a different URL(destinationUrl). Eg: redirectUrls: { "/amp/story/sports/ipl-2021": {destinationUrl: "/amp/story/sports/cricket-2022", statusCode: 302,},}

headerCardRender function

Render prop for story headerCard. If passed, the headerCard in default stories will be replaced with this

relatedStoriesRender function

Render prop for relatedStories in a story page. If passed, this will replace the related stories

View Source server/routes.js, line 674

# static isomorphicRoutes(app, opts, redirectToLowercaseSlugs, shouldEncodeAmpUri, sMaxAge, maxAge, fcmServerKey, appLoadingPlaceholder, enableExternalStories, externalIdPattern)

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)

mobileConfigFields Object

List of fields that are needed in the config field of the /mobile-data.json API. This is primarily used by the React Native starter kit. (default: {})

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

lightPages boolean | function

If set to true, then all story pages will render amp pages.

cdnProvider string | function

The name of the cdn provider. Supported cdn providers are akamai, cloudflare. Default value is cloudflare.

maxConfigVersion function

An async function which resolves to a integer version of the config. This defaults to config.theme-attributes.cache-burst

redirectUrls Array.<object> | function

An array or async function which used to render the redirect url provided in the array of object - >ex- REDIRECT_URLS = [{sourceUrl: "/tag/:tagSlug",destinationUrl: "/topic/:tagSlug",statusCode: 301,}]

redirectToLowercaseSlugs boolean | function

If set or evaluates to true, then for every story-page request having capital latin letters in the slug, it responds with a 301 redirect to the lowercase slug URL. (default: true)

shouldEncodeAmpUri boolean | function

If set to true, then for every story-page request the slug will be encoded, in case of a vernacular slug this should be set to false. Receives path as param (default: true)

sMaxAge number

Overrides the s-maxage value, the default value is set to 900 seconds. We can set isomorphicRoutesSmaxage: 900 under publisher in publisher.yml config file that comes from BlackKnight or pass sMaxAge as a param.

maxAge number

Overrides the max-age value, the default value is set to 15 seconds. We can set isomorphicRoutesMaxage: 15 under publisher in publisher.yml config file that comes from BlackKnight or pass maxAge as a param.

fcmServerKey string | function

FCM serverKey is used for registering FCM Topic.

appLoadingPlaceholder string

This string gets injected into the app container when the page is loaded via service worker. Can be used to show skeleton layouts, animations or other progress indicators before it is replaced by the page content.

enableExternalStories boolean | function

If set to true, then for every request an external story api call is made and renders the story-page if the story is found. (default: false)

externalIdPattern string | function

This string specifies the external id pattern the in the url. Mention EXTERNAL_ID to specify the position of external id in the url. Ex: "/parent-section/child-section/EXTERNAL_ID"

View Source server/routes.js, line 311

# 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 606

# 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)

sMaxAge number

Support overriding of proxied response cache header s-maxage from Sketches. For Breaking News and if the cacheability is Private, it is not overwritten instead the cache control will be the same as how it's set in sketches. We can set upstreamRoutesSmaxage: 900 under publisher in publisher.yml config file that comes from BlackKnight or pass sMaxAge as a param.

maxAge number

Support overriding of proxied response cache header maxage from Sketches. For Breaking News and if the cacheability is Private, it is not overwritten instead the cache control will be the same as how it's set in sketches. We can set upstreamRoutesMaxage: 15 under publisher in publisher.yml config file that comes from BlackKnight or pass maxAge as a param.

forwardFavicon boolean

Forward favicon requests to the CMS (default false)

isSitemapUrlEnabled boolean

To enable /news_sitemap/today and /news_sitemap/yesterday sitemap news url (default /news_sitemap.xml)

View Source server/routes.js, line 46

# 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 259

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 240