Merge pull request #591 from thgh/redirect-basepath

Fix redirect with basepath
This commit is contained in:
Rich Harris
2019-04-29 11:43:51 -04:00
committed by GitHub
5 changed files with 54 additions and 3 deletions

View File

@@ -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);

View File

@@ -1 +1,2 @@
<h1>Great success!</h1>
<h1>Great success!</h1>
<a href="redirect-from">redirect from</a>

View File

@@ -0,0 +1,7 @@
<script context="module">
export function preload() {
this.redirect(301, 'redirect-to');
}
</script>
<h1>unredirected</h1>

View File

@@ -0,0 +1 @@
<h1>redirected</h1>

View File

@@ -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,11 @@ describe('with-basepath', function() {
let page: puppeteer.Page;
let base: string;
// helpers
let start: () => Promise<void>;
let prefetchRoutes: () => Promise<void>;
let title: () => Promise<string>;
// hooks
before(async () => {
await api.build({ cwd: __dirname });
@@ -21,7 +28,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 +63,43 @@ 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/index.html',
'custom-basepath/service-worker-index.html',
'custom-basepath/service-worker.js'
]);
});
it('redirects on server', async () => {
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'
);
});
});