From 00c792297a7e600ea6f38cbf0b5536c894ccd44c Mon Sep 17 00:00:00 2001 From: Andy Date: Tue, 9 Jul 2019 20:58:04 +0200 Subject: [PATCH] [now dev] Tests for routing and apply default frontend builder last (#2518) --- src/util/dev/server.ts | 28 +++++++++++-------- .../now-dev-default-frontend/package.json | 5 ++++ .../public/index.html | 9 ++++++ .../now-dev-default-routing/api/[endpoint].js | 3 ++ .../api/[endpoint]/[id].js | 3 ++ .../now-dev-default-routing/api/welcome.js | 3 ++ .../unit/now-dev-default-routing/package.json | 5 ++++ .../now-dev-default-routing/public/index.html | 9 ++++++ 8 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 test/fixtures/unit/now-dev-default-frontend/package.json create mode 100644 test/fixtures/unit/now-dev-default-frontend/public/index.html create mode 100644 test/fixtures/unit/now-dev-default-routing/api/[endpoint].js create mode 100644 test/fixtures/unit/now-dev-default-routing/api/[endpoint]/[id].js create mode 100644 test/fixtures/unit/now-dev-default-routing/api/welcome.js create mode 100644 test/fixtures/unit/now-dev-default-routing/package.json create mode 100644 test/fixtures/unit/now-dev-default-routing/public/index.html diff --git a/src/util/dev/server.ts b/src/util/dev/server.ts index 586d6df9d..0e05596db 100644 --- a/src/util/dev/server.ts +++ b/src/util/dev/server.ts @@ -351,19 +351,7 @@ export default class DevServer { const apiFiles = await getApiFiles(this.cwd, this.output); const hasNoBuilds = !config.builds || config.builds.length === 0; - if (pkg && hasNoBuilds) { - config.builds = config.builds || []; - - const staticBuilder = await detectBuilder(pkg); - - if (staticBuilder) { - config.builds.push(staticBuilder); - } - } - if (apiFiles.length > 0) { - // We need to use `hasNoBuilds` because it was created - // before `staticBuilder` was added if (hasNoBuilds) { const apiBuilds = await detectApiBuilders(apiFiles); @@ -384,6 +372,22 @@ export default class DevServer { } } + /** + * We need to use `hasNoBuilds` because it was created + * before the api builders were added. + * We also have to add this builder after all + * the others to prevent catch all routes etc. + */ + if (pkg && hasNoBuilds) { + config.builds = config.builds || []; + + const staticBuilder = await detectBuilder(pkg); + + if (staticBuilder) { + config.builds.push(staticBuilder); + } + } + this.validateNowConfig(config); this.cachedNowJson = config; return config; diff --git a/test/fixtures/unit/now-dev-default-frontend/package.json b/test/fixtures/unit/now-dev-default-frontend/package.json new file mode 100644 index 000000000..25c7c65e9 --- /dev/null +++ b/test/fixtures/unit/now-dev-default-frontend/package.json @@ -0,0 +1,5 @@ +{ + "scripts": { + "build": "rm -Rf dist && cp -r public dist" + } +} diff --git a/test/fixtures/unit/now-dev-default-frontend/public/index.html b/test/fixtures/unit/now-dev-default-frontend/public/index.html new file mode 100644 index 000000000..26c3a2a40 --- /dev/null +++ b/test/fixtures/unit/now-dev-default-frontend/public/index.html @@ -0,0 +1,9 @@ + + + + Hello World + + + Hello World + + diff --git a/test/fixtures/unit/now-dev-default-routing/api/[endpoint].js b/test/fixtures/unit/now-dev-default-routing/api/[endpoint].js new file mode 100644 index 000000000..174083b2f --- /dev/null +++ b/test/fixtures/unit/now-dev-default-routing/api/[endpoint].js @@ -0,0 +1,3 @@ +module.exports = (req, res) => { + res.end(`${req.query.endpoint}`); +}; diff --git a/test/fixtures/unit/now-dev-default-routing/api/[endpoint]/[id].js b/test/fixtures/unit/now-dev-default-routing/api/[endpoint]/[id].js new file mode 100644 index 000000000..e80787cb5 --- /dev/null +++ b/test/fixtures/unit/now-dev-default-routing/api/[endpoint]/[id].js @@ -0,0 +1,3 @@ +module.exports = (req, res) => { + res.end(`${req.query.endpoint}/${req.query.id}`); +}; diff --git a/test/fixtures/unit/now-dev-default-routing/api/welcome.js b/test/fixtures/unit/now-dev-default-routing/api/welcome.js new file mode 100644 index 000000000..83fe01c5c --- /dev/null +++ b/test/fixtures/unit/now-dev-default-routing/api/welcome.js @@ -0,0 +1,3 @@ +module.exports = (req, res) => { + res.end('hello and welcome'); +}; diff --git a/test/fixtures/unit/now-dev-default-routing/package.json b/test/fixtures/unit/now-dev-default-routing/package.json new file mode 100644 index 000000000..25c7c65e9 --- /dev/null +++ b/test/fixtures/unit/now-dev-default-routing/package.json @@ -0,0 +1,5 @@ +{ + "scripts": { + "build": "rm -Rf dist && cp -r public dist" + } +} diff --git a/test/fixtures/unit/now-dev-default-routing/public/index.html b/test/fixtures/unit/now-dev-default-routing/public/index.html new file mode 100644 index 000000000..26c3a2a40 --- /dev/null +++ b/test/fixtures/unit/now-dev-default-routing/public/index.html @@ -0,0 +1,9 @@ + + + + Hello World + + + Hello World + +