<commit_before>/***
 * Copyright (c) 2015 <NAME> (@localnerve), LocalNerve LLC
 * Copyrights licensed under the BSD License. See the accompanying LICENSE file for terms.
 *
 * Handling for routes
 */
/* global Promise, Request, URL, location */
'use strict';

var toolbox = require('sw-toolbox');
var debug = require('../utils/debug')('routes');
var networkFirst = require('../utils/customNetworkFirst');

/**
 * Create a request for network use.
 * Adds a parameter to skip rendering.
 *
 * @param {Object|String} request - The Request from sw-toolbox router, or a string.
 * @returns String of the new request url.
 */
function networkRequest (request) {
  var skipRenderParam = 'render=0',
      requestUrl = (typeof request !== 'string') ? request.url : request,
      skipRenderUrl = new URL(requestUrl, location.origin);

  if (skipRenderUrl.search) {
    skipRenderUrl.search += '&' + skipRenderParam;
  } else {
    skipRenderUrl.search = '?' + skipRenderParam;
  }

  return new Request(skipRenderUrl.toString());
}

/**
 * What this does:
 * 1. Fetch the mainNav routes of the application and update the cache with the responses.
 * 2. Install route handlers for all the main nav routes.
 *
 * Route fetches add a parameter that indicates no server-side rendering
 * of the application should be done.
 * This reduces the load on the server, and the rendered application markup is not
 * required in this case, since the main app bundle is already cached.
 *
 * @param {Object} payload - The payload of the init message.
 * @param {Object} payload.RouteStore.routes - The routes of the application.
 * @return {Object} A Promise with all aggregate route fetchesAndCache results.
 */
module.exports = function cacheRoutes (payload) {
  var routes = payload.RouteStore.routes;

  debug(toolbox.options, 'received routes', routes);

  return Promise.all(Object.keys(routes).map(function (route) {
    if (routes[route].mainNav) {
      var url = routes[route].path;

      debug(toolbox.options, 'install route handler on', url);

      // Install a read-thru cache handler on the mainNav route.
      toolbox.router.get(url, networkFirst.routeHandlerFactory(
        networkRequest, networkFirst.passThru
      ), {
        debug: toolbox.options.debug
      });

      debug(toolbox.options, 'cache route', url);

      // Fetch the route and add the response to the cache.
      return networkFirst.fetchAndCache(networkRequest(url), url);
    }<commit_msg>fix route caching order - first cache, then install route handler<commit_after>/***
 * Copyright (c) 2015 <NAME> (@localnerve), LocalNerve LLC
 * Copyrights licensed under the BSD License. See the accompanying LICENSE file for terms.
 *
 * Handling for routes
 */
/* global Promise, Request, URL, location */
'use strict';

var toolbox = require('sw-toolbox');
var debug = require('../utils/debug')('routes');
var networkFirst = require('../utils/customNetworkFirst');

/**
 * Create a request for network use.
 * Adds a parameter to skip rendering.
 *
 * @param {Object|String} request - The Request from sw-toolbox router, or a string.
 * @returns String of the new request url.
 */
function networkRequest (request) {
  var skipRenderParam = 'render=0',
      requestUrl = (typeof request !== 'string') ? request.url : request,
      skipRenderUrl = new URL(requestUrl, location.origin);

  if (skipRenderUrl.search) {
    skipRenderUrl.search += '&' + skipRenderParam;
  } else {
    skipRenderUrl.search = '?' + skipRenderParam;
  }

  return new Request(skipRenderUrl.toString());
}

/**
 * What this does:
 * 1. Fetch the mainNav routes of the application and update the cache with the responses.
 * 2. Install route handlers for all the main nav routes.
 *
 * Route fetches add a parameter that indicates no server-side rendering
 * of the application should be done.
 * This reduces the load on the server, and the rendered application markup is not
 * required in this case, since the main app bundle is already cached.
 *
 * @param {Object} payload - The payload of the init message.
 * @param {Object} payload.RouteStore.routes - The routes of the application.
 * @return {Object} A Promise with all aggregate route fetchesAndCache results.
 */
module.exports = function cacheRoutes (payload) {
  var routes = payload.RouteStore.routes;

  debug(toolbox.options, 'received routes', routes);

  return Promise.all(Object.keys(routes).map(function (route) {
    if (routes[route].mainNav) {
      var url = routes[route].path;

      debug(toolbox.options, 'cache route', url);

      // Fetch and cache the mainNav route.
      return networkFirst.fetchAndCache(networkRequest(url), url).then(function () {
        debug(toolbox.options, 'install route handler on', url);

        // Install read-thru cache handler.
        toolbox.router.get(url, networkFirst.routeHandlerFactory(
          networkRequest, networkFirst.passThru
        ), {
          debug: toolbox.options.debug
        });

        return Promise.resolve();
      });
    }
{'fraction_non_alphanumeric': 0.11009356957993231, 'fraction_numerical': 0.002787178976707147, 'mean_word_length': 3.558983666061706, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4639271', 'n_tokens_mistral': 1391, 'n_tokens_neox': 1364, 'n_words': 502}