From aad87857ce13aef9864174c77f50dc3492f4aa05 Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Mon, 6 Aug 2018 20:22:33 -0700 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20replace=20leading=20slash=20in=20pre?= =?UTF-8?q?load=E2=80=99s=20redirect?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/middleware.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/middleware.ts b/src/middleware.ts index 6941ab3..5d1e6b0 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -336,6 +336,7 @@ function get_page_handler(manifest: Manifest, store_getter: (req: Req) => Store) if (redirect && (redirect.statusCode !== statusCode || redirect.location !== location)) { throw new Error(`Conflicting redirects`); } + location = location.replace(/^\//g, ''); // leading slash (only) redirect = { statusCode, location }; }, error: (statusCode: number, message: Error | string) => { @@ -582,4 +583,4 @@ function escape_html(html: string) { }; return html.replace(/["'&<>]/g, c => `&${chars[c]};`); -} \ No newline at end of file +} From c6da26e1a025dbf40c887a025d92d3380472de17 Mon Sep 17 00:00:00 2001 From: Luke Edwards Date: Mon, 6 Aug 2018 20:29:28 -0700 Subject: [PATCH 2/2] =?UTF-8?q?add=20redirect=20test=20to=20root=20(?= =?UTF-8?q?=E2=80=9C/=E2=80=9C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/app/routes/index.html | 1 + test/app/routes/redirect-root.html | 7 +++++++ test/common/test.js | 27 +++++++++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 test/app/routes/redirect-root.html diff --git a/test/app/routes/index.html b/test/app/routes/index.html index cb37b8c..b695afe 100644 --- a/test/app/routes/index.html +++ b/test/app/routes/index.html @@ -8,6 +8,7 @@ about slow preload redirect +redirect (root) broken link error link credentials diff --git a/test/app/routes/redirect-root.html b/test/app/routes/redirect-root.html new file mode 100644 index 0000000..ddd4b49 --- /dev/null +++ b/test/app/routes/redirect-root.html @@ -0,0 +1,7 @@ + diff --git a/test/common/test.js b/test/common/test.js index c46751f..70bf890 100644 --- a/test/common/test.js +++ b/test/common/test.js @@ -436,6 +436,33 @@ function run({ mode, basepath = '' }) { }); }); + it('redirects on server (root)', () => { + return nightmare.goto(`${base}/redirect-root`) + .path() + .then(path => { + assert.equal(path, `${basepath}/`); + }) + .then(() => nightmare.page.title()) + .then(title => { + assert.equal(title, 'Great success!'); + }); + }); + + it('redirects in client (root)', () => { + return nightmare.goto(base) + .wait('[href="redirect-root"]') + .click('[href="redirect-root"]') + .wait(200) + .path() + .then(path => { + assert.equal(path, `${basepath}/`); + }) + .then(() => nightmare.page.title()) + .then(title => { + assert.equal(title, 'Great success!'); + }); + }); + it('handles 4xx error on server', () => { return nightmare.goto(`${base}/blog/nope`) .path()