From 9ea4137b875b0f25c00adfb63e4f209f6dc34986 Mon Sep 17 00:00:00 2001 From: freedmand Date: Fri, 5 Jan 2018 19:21:25 -0800 Subject: [PATCH] Add option to extract server-side routes at directories other than /api. Also clarifies some texts and documentation. --- lib/utils/extract.js | 27 +++++++++++++++++---------- test/common/test.js | 6 ++++-- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/utils/extract.js b/lib/utils/extract.js index e41e252..0e0dd8f 100644 --- a/lib/utils/extract.js +++ b/lib/utils/extract.js @@ -56,13 +56,14 @@ function relativePath(url) { /** * Returns the Sapper API route for the specified URL path. * @param {string} url The absolute or relative URL. + * @param {string=} apiPrefix The prefix for Sapper server-side routes. * @return {string} The URL with /api/ in front. */ -function apiPath(url) { +function apiPath(url, apiPrefix = '/api') { if (url.startsWith(prefix)) { - return `${prefix}/api${url.substr(prefix.length)}`; + return `${prefix}${apiPrefix}${url.substr(prefix.length)}`; } - return `/api${url}`; + return `${apiPrefix}${url}`; } /** @@ -88,18 +89,23 @@ function getChunkFiles() { /** * Exports the Sapper app as a static website by starting at the root and - * crawling pages that are linked, their /api/ pages, and webpack routes, as - * well as copying assets. + * crawling pages that are linked, extracting server and client routes, and + * copying assets. * @param {?Array=} includeUrls If non-null, a set of additional URLs to * scrape in the extraction. This should only be set if there are routes * that cannot be reached from the root. * @param {?Array=} excludeUrls If non-null, a set of URLs to avoid * scraping in the extraction. + * @param {string=} apiPrefix The path in which all server-side Sapper routes + * are defined. The Sapper template application uses '/api' -- if you + * diverge from the template app structure, you will want to change this. If + * your server-side Sapper routes span multiple directories, you will have + * to specify each file manually with the `includeUrls` param. * @param {number=} extractionDir The directory in which to place the extracted * output. */ module.exports = function(includeUrls = null, excludeUrls = null, - extractionDir = OUTPUT_DIR) { + apiPrefix = '/api', extractionDir = OUTPUT_DIR) { // Set up the server. // this allows us to do e.g. `fetch('/api/blog')` on the server @@ -204,10 +210,11 @@ module.exports = function(includeUrls = null, excludeUrls = null, (url) => spider.queue(getFullUrl(url), handleRequest)); } - if (relPath.endsWith('/index.html') && !relPath.startsWith('/api/')) { - // Attempt to grab the /api/ version of a page that seems to be a - // basic route. - spider.queue(apiPath(doc.url), handleRequest); + if (relPath.endsWith('/index.html') && + !relPath.startsWith(`${apiPrefix}/`)) { + // Attempt to grab the server-side route corresponding to a page that + // seems to be a basic route. + spider.queue(apiPath(doc.url, apiPrefix), handleRequest); } }; diff --git a/test/common/test.js b/test/common/test.js index 1c6f332..af95b07 100644 --- a/test/common/test.js +++ b/test/common/test.js @@ -367,7 +367,8 @@ function run(env) { const allPages = walkSync(dest); expectedPages.forEach((expectedPage) => { - assert.ok(allPages.includes(expectedPage)); + assert.ok(allPages.includes(expectedPage), + `Could not find page matching ${expectedPage}`); }); expectedClientRegexes.forEach((expectedRegex) => { // Ensure each client page regular expression matches at least one @@ -379,7 +380,8 @@ function run(env) { break; } } - assert.ok(matched); + assert.ok(matched, + `Could not find client page matching ${expectedRegex}`); }); }); });