mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-19 05:45:27 +00:00
Merge pull request #591 from thgh/redirect-basepath
Fix redirect with basepath
This commit is contained in:
@@ -177,7 +177,7 @@ export function get_page_handler(
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (redirect) {
|
if (redirect) {
|
||||||
const location = URL.resolve(req.baseUrl || '/', redirect.location);
|
const location = URL.resolve((req.baseUrl || '') + '/', redirect.location);
|
||||||
|
|
||||||
res.statusCode = redirect.statusCode;
|
res.statusCode = redirect.statusCode;
|
||||||
res.setHeader('Location', location);
|
res.setHeader('Location', location);
|
||||||
|
|||||||
@@ -1 +1,2 @@
|
|||||||
<h1>Great success!</h1>
|
<h1>Great success!</h1>
|
||||||
|
<a href="redirect-from">redirect from</a>
|
||||||
|
|||||||
7
test/apps/with-basepath/src/routes/redirect-from.svelte
Normal file
7
test/apps/with-basepath/src/routes/redirect-from.svelte
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
<script context="module">
|
||||||
|
export function preload() {
|
||||||
|
this.redirect(301, 'redirect-to');
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<h1>unredirected</h1>
|
||||||
1
test/apps/with-basepath/src/routes/redirect-to.svelte
Normal file
1
test/apps/with-basepath/src/routes/redirect-to.svelte
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<h1>redirected</h1>
|
||||||
@@ -3,6 +3,8 @@ import * as puppeteer from 'puppeteer';
|
|||||||
import * as api from '../../../api';
|
import * as api from '../../../api';
|
||||||
import { walk } from '../../utils';
|
import { walk } from '../../utils';
|
||||||
import { AppRunner } from '../AppRunner';
|
import { AppRunner } from '../AppRunner';
|
||||||
|
import { wait } from '../../utils';
|
||||||
|
|
||||||
|
|
||||||
describe('with-basepath', function() {
|
describe('with-basepath', function() {
|
||||||
this.timeout(10000);
|
this.timeout(10000);
|
||||||
@@ -11,6 +13,11 @@ describe('with-basepath', function() {
|
|||||||
let page: puppeteer.Page;
|
let page: puppeteer.Page;
|
||||||
let base: string;
|
let base: string;
|
||||||
|
|
||||||
|
// helpers
|
||||||
|
let start: () => Promise<void>;
|
||||||
|
let prefetchRoutes: () => Promise<void>;
|
||||||
|
let title: () => Promise<string>;
|
||||||
|
|
||||||
// hooks
|
// hooks
|
||||||
before(async () => {
|
before(async () => {
|
||||||
await api.build({ cwd: __dirname });
|
await api.build({ cwd: __dirname });
|
||||||
@@ -21,7 +28,7 @@ describe('with-basepath', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
runner = new AppRunner(__dirname, '__sapper__/build/server/server.js');
|
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());
|
after(() => runner.end());
|
||||||
@@ -56,8 +63,43 @@ describe('with-basepath', function() {
|
|||||||
assert.deepEqual(non_client_assets, [
|
assert.deepEqual(non_client_assets, [
|
||||||
'custom-basepath/global.css',
|
'custom-basepath/global.css',
|
||||||
'custom-basepath/index.html',
|
'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-index.html',
|
||||||
'custom-basepath/service-worker.js'
|
'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'
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user