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 +} 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 b8b90fe..b213022 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()