Merge pull request #419 from sveltejs/gh-417

decode req.params
This commit is contained in:
Rich Harris
2018-09-04 07:31:53 -04:00
committed by GitHub
7 changed files with 65 additions and 6 deletions

View File

@@ -63,6 +63,8 @@ function generate_client(
import root from '${get_file(path_to_routes, manifest_data.root)}';
import error from '${posixify(`${path_to_routes}/_error.html`)}';
const d = decodeURIComponent;
${manifest_data.components.map(component => {
const annotation = bundler === 'webpack'
? `/* webpackChunkName: "${component.name}" */ `
@@ -88,7 +90,7 @@ function generate_client(
if (part === null) return 'null';
if (part.params.length > 0) {
const props = part.params.map((param, i) => `${param}: match[${i + 1}]`);
const props = part.params.map((param, i) => `${param}: d(match[${i + 1}])`);
return `{ component: ${part.component.name}, params: match => ({ ${props.join(', ')} }) }`;
}
@@ -138,6 +140,8 @@ function generate_server(
// This file is generated by Sapper — do not edit it!
${imports.join('\n')}
const d = decodeURIComponent;
export const manifest = {
server_routes: [
${manifest_data.server_routes.map(route => `{
@@ -145,7 +149,7 @@ function generate_server(
pattern: ${route.pattern},
handlers: ${route.name},
params: ${route.params.length > 0
? `match => ({ ${route.params.map((param, i) => `${param}: match[${i + 1}]`).join(', ')} })`
? `match => ({ ${route.params.map((param, i) => `${param}: d(match[${i + 1}])`).join(', ')} })`
: `() => ({})`}
}`).join(',\n\n\t\t\t\t')}
],
@@ -165,7 +169,7 @@ function generate_server(
];
if (part.params.length > 0) {
const params = part.params.map((param, i) => `${param}: match[${i + 1}]`);
const params = part.params.map((param, i) => `${param}: d(match[${i + 1}])`);
props.push(`params: match => ({ ${params.join(', ')} })`);
}

View File

@@ -67,7 +67,7 @@ function select_route(url: URL): Target {
if (url.search.length > 0) {
url.search.slice(1).split('&').forEach(searchParam => {
const [, key, value] = /([^=]+)=(.*)/.exec(searchParam);
query[key] = value || true;
query[key] = value ? decodeURIComponent(value.replace(/\+/g, ' ')) : true;
});
}
return { url, path, page, match, query };

View File

@@ -1,4 +1,4 @@
import { init, prefetchRoutes } from '../../../runtime.js';
import { init, goto, prefetchRoutes } from '../../../runtime.js';
import { Store } from 'svelte/store.js';
import { manifest } from './manifest/client.js';
@@ -10,4 +10,5 @@ window.init = () => {
});
};
window.prefetchRoutes = prefetchRoutes;
window.prefetchRoutes = prefetchRoutes;
window.goto = goto;

View File

@@ -0,0 +1,12 @@
<h1>{slug} ({message})</h1>
<script>
export default {
preload({ params, query }) {
return {
slug: params.slug,
message: query.message
};
}
};
</script>

View File

@@ -0,0 +1,15 @@
export function get(req, res) {
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.end(`
<!doctype html>
<html>
<head><meta charset="utf-8"></head>
<body>
<h1>${req.params.slug}</h1>
</body>
</html>
`);
}

View File

@@ -15,6 +15,7 @@
<a href='credentials?creds=include'>credentials</a>
<a rel=prefetch class='{page === "blog" ? "selected" : ""}' href='blog'>blog</a>
<a href="const">const</a>
<a href="echo/page/encöded?message=hëllö+wörld">echo/page/encöded?message=hëllö+wörld</a>
<div class='hydrate-test'></div>

View File

@@ -753,6 +753,32 @@ function run({ mode, basepath = '' }) {
assert.equal(title, 'reserved words are okay as routes');
});
});
it('encodes req.params and req.query for server-rendered pages', () => {
return nightmare.goto(`${base}/echo/page/encöded?message=hëllö+wörld`)
.page.title()
.then(title => {
assert.equal(title, 'encöded (hëllö wörld)');
});
});
it('encodes req.params and req.query for client-rendered pages', () => {
return nightmare.goto(base).init()
.click('a[href="echo/page/encöded?message=hëllö+wörld"]')
.wait(100)
.page.title()
.then(title => {
assert.equal(title, 'encöded (hëllö wörld)');
});
});
it('encodes req.params for server routes', () => {
return nightmare.goto(`${base}/echo/server-route/encöded`)
.page.title()
.then(title => {
assert.equal(title, 'encöded');
});
});
});
describe('headers', () => {