Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
redirect to external URLs - closes #490
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Oct 27, 2018
1 parent ab52aab commit e69cb36
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
4 changes: 2 additions & 2 deletions templates/src/server/middleware/get_page_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from 'path';
import cookie from 'cookie';
import devalue from 'devalue';
import fetch from 'node-fetch';
import { URL } from 'url';
import { URL, resolve } from 'url';
import { build_dir, dev, src_dir, IGNORE } from '../placeholders';
import { Manifest, Page, Props, Req, Res, Store } from './types';

Expand Down Expand Up @@ -160,7 +160,7 @@ export function get_page_handler(

try {
if (redirect) {
const location = `${req.baseUrl}/${redirect.location}`;
const location = resolve(req.baseUrl || '/', redirect.location);

res.statusCode = redirect.statusCode;
res.setHeader('Location', location);
Expand Down
14 changes: 14 additions & 0 deletions test/apps/AppRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ export class AppRunner {
}
});

await this.page.setRequestInterception(true);

this.page.on('request', interceptedRequest => {
if (/example\.com/.test(interceptedRequest.url())) {
interceptedRequest.respond({
status: 200,
contentType: 'text/html',
body: `<h1>external</h1>`
});
} else {
interceptedRequest.continue();
}
});

return {
page: this.page,
base: `http://localhost:${this.port}`,
Expand Down
3 changes: 2 additions & 1 deletion test/apps/redirects/src/routes/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<h1>root</h1>

<a href="redirect-from">redirect from</a>
<a href="redirect-to-root">redirect to root</a>
<a href="redirect-to-root">redirect to root</a>
<a href="redirect-to-external">redirect to external</a>
9 changes: 9 additions & 0 deletions test/apps/redirects/src/routes/redirect-to-external.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>unredirected</h1>

<script>
export default {
preload() {
this.redirect(301, 'https://example.com');
}
};
</script>
44 changes: 39 additions & 5 deletions test/apps/redirects/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ describe('redirects', function() {
// helpers
let start: () => Promise<void>;
let prefetchRoutes: () => Promise<void>;
let title: () => Promise<string>;

// hooks
before(async () => {
await build({ cwd: __dirname });

runner = new AppRunner(__dirname, '__sapper__/build/server/server.js');
({ base, page, start, prefetchRoutes } = await runner.start());
({ base, page, start, prefetchRoutes, title } = await runner.start());
});

after(() => runner.end());
Expand All @@ -34,7 +35,7 @@ describe('redirects', function() {
);

assert.equal(
await page.$eval('h1', node => node.textContent),
await title(),
'redirected'
);
});
Expand All @@ -53,7 +54,7 @@ describe('redirects', function() {
);

assert.equal(
await page.$eval('h1', node => node.textContent),
await title(),
'redirected'
);
});
Expand All @@ -67,7 +68,7 @@ describe('redirects', function() {
);

assert.equal(
await page.$eval('h1', node => node.textContent),
await title(),
'root'
);
});
Expand All @@ -86,8 +87,41 @@ describe('redirects', function() {
);

assert.equal(
await page.$eval('h1', node => node.textContent),
await title(),
'root'
);
});

it('redirects to external URL on server', async () => {
await page.goto(`${base}/redirect-to-external`);

assert.equal(
page.url(),
`https://example.com/`
);

assert.equal(
await title(),
'external'
);
});

it('redirects to external URL in client', async () => {
await page.goto(base);
await start();
await prefetchRoutes();

await page.click('[href="redirect-to-external"]');
await wait(50);

assert.equal(
page.url(),
`https://example.com/`
);

assert.equal(
await title(),
'external'
);
});
});

0 comments on commit e69cb36

Please sign in to comment.