From 9522cb45396a8e4689c1435a053be8501f4b6c78 Mon Sep 17 00:00:00 2001 From: Thomas Ghysels Date: Sat, 2 Mar 2019 18:33:23 +0100 Subject: [PATCH 1/2] Add failing test for #589 --- .../with-basepath/src/routes/index.svelte | 3 +- .../src/routes/redirect-from.svelte | 7 +++ .../src/routes/redirect-to.svelte | 1 + test/apps/with-basepath/test.ts | 44 ++++++++++++++++++- 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 test/apps/with-basepath/src/routes/redirect-from.svelte create mode 100644 test/apps/with-basepath/src/routes/redirect-to.svelte diff --git a/test/apps/with-basepath/src/routes/index.svelte b/test/apps/with-basepath/src/routes/index.svelte index 0cc4b72..45bc3a3 100644 --- a/test/apps/with-basepath/src/routes/index.svelte +++ b/test/apps/with-basepath/src/routes/index.svelte @@ -1 +1,2 @@ -

Great success!

\ No newline at end of file +

Great success!

+redirect from diff --git a/test/apps/with-basepath/src/routes/redirect-from.svelte b/test/apps/with-basepath/src/routes/redirect-from.svelte new file mode 100644 index 0000000..45805e1 --- /dev/null +++ b/test/apps/with-basepath/src/routes/redirect-from.svelte @@ -0,0 +1,7 @@ + + +

unredirected

\ No newline at end of file diff --git a/test/apps/with-basepath/src/routes/redirect-to.svelte b/test/apps/with-basepath/src/routes/redirect-to.svelte new file mode 100644 index 0000000..eeb0dfc --- /dev/null +++ b/test/apps/with-basepath/src/routes/redirect-to.svelte @@ -0,0 +1 @@ +

redirected

\ No newline at end of file diff --git a/test/apps/with-basepath/test.ts b/test/apps/with-basepath/test.ts index 5741d1b..bfb50d8 100644 --- a/test/apps/with-basepath/test.ts +++ b/test/apps/with-basepath/test.ts @@ -3,6 +3,8 @@ import * as puppeteer from 'puppeteer'; import * as api from '../../../api'; import { walk } from '../../utils'; import { AppRunner } from '../AppRunner'; +import { wait } from '../../utils'; + describe('with-basepath', function() { this.timeout(10000); @@ -11,6 +13,10 @@ describe('with-basepath', function() { let page: puppeteer.Page; let base: string; + let start: () => Promise; + let prefetchRoutes: () => Promise; + let title: () => Promise; + // hooks before(async () => { await api.build({ cwd: __dirname }); @@ -21,7 +27,7 @@ describe('with-basepath', function() { }); runner = new AppRunner(__dirname, '__sapper__/build/server/server.js'); - ({ base, page } = await runner.start()); + ({ base, start, page, prefetchRoutes, title } = await runner.start()); }); after(() => runner.end()); @@ -56,8 +62,44 @@ describe('with-basepath', function() { assert.deepEqual(non_client_assets, [ 'custom-basepath/global.css', 'custom-basepath/index.html', + 'custom-basepath/redirect-from/index.html', + 'custom-basepath/redirect-to', 'custom-basepath/service-worker-index.html', 'custom-basepath/service-worker.js' ]); }); + + it('redirects on server', async () => { + console.log('base', base) + await page.goto(`${base}/custom-basepath/redirect-from`); + + assert.equal( + page.url(), + `${base}/custom-basepath/redirect-to` + ); + + assert.equal( + await title(), + 'redirected' + ); + }); + + it('redirects in client', async () => { + await page.goto(`${base}/custom-basepath`); + await start(); + await prefetchRoutes(); + + await page.click('[href="redirect-from"]'); + await wait(50); + + assert.equal( + page.url(), + `${base}/custom-basepath/redirect-to` + ); + + assert.equal( + await title(), + 'redirected' + ); + }); }); \ No newline at end of file From 13b64cd1bb57bd719de8fea147add912082612cb Mon Sep 17 00:00:00 2001 From: Thomas Ghysels Date: Sun, 3 Mar 2019 10:40:49 +0100 Subject: [PATCH 2/2] Fix redirect with basepath Fix #589 --- runtime/src/server/middleware/get_page_handler.ts | 2 +- test/apps/with-basepath/test.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/runtime/src/server/middleware/get_page_handler.ts b/runtime/src/server/middleware/get_page_handler.ts index c0a39cc..e22f2da 100644 --- a/runtime/src/server/middleware/get_page_handler.ts +++ b/runtime/src/server/middleware/get_page_handler.ts @@ -177,7 +177,7 @@ export function get_page_handler( try { if (redirect) { - const location = URL.resolve(req.baseUrl || '/', redirect.location); + const location = URL.resolve((req.baseUrl || '') + '/', redirect.location); res.statusCode = redirect.statusCode; res.setHeader('Location', location); diff --git a/test/apps/with-basepath/test.ts b/test/apps/with-basepath/test.ts index bfb50d8..a81875d 100644 --- a/test/apps/with-basepath/test.ts +++ b/test/apps/with-basepath/test.ts @@ -13,6 +13,7 @@ describe('with-basepath', function() { let page: puppeteer.Page; let base: string; + // helpers let start: () => Promise; let prefetchRoutes: () => Promise; let title: () => Promise; @@ -63,14 +64,13 @@ describe('with-basepath', function() { 'custom-basepath/global.css', 'custom-basepath/index.html', 'custom-basepath/redirect-from/index.html', - 'custom-basepath/redirect-to', + 'custom-basepath/redirect-to/index.html', 'custom-basepath/service-worker-index.html', 'custom-basepath/service-worker.js' ]); }); it('redirects on server', async () => { - console.log('base', base) await page.goto(`${base}/custom-basepath/redirect-from`); assert.equal(