mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-12 03:05:12 +00:00
prevent client-side navigation to server routes - fixes #145
This commit is contained in:
@@ -63,7 +63,7 @@
|
||||
return this.error(500, 'something went wrong');
|
||||
}
|
||||
|
||||
return fetch(`/api/blog/${slug}`).then(r => {
|
||||
return fetch(`/blog/${slug}.json`).then(r => {
|
||||
if (r.status === 200) {
|
||||
return r.json().then(post => ({ post }));
|
||||
this.error(r.status, '')
|
||||
|
||||
@@ -70,7 +70,7 @@ const posts = [
|
||||
<ul>
|
||||
<li>It's powered by <a href='https://svelte.technology'>Svelte</a> instead of React, so it's faster and your apps are smaller</li>
|
||||
<li>Instead of route masking, we encode route parameters in filenames. For example, the page you're looking at right now is <code>routes/blog/[slug].html</code></li>
|
||||
<li>As well as pages (Svelte components, which render on server or client), you can create <em>server routes</em> in your <code>routes</code> directory. These are just <code>.js</code> files that export functions corresponding to HTTP methods, and receive Express <code>request</code> and <code>response</code> objects as arguments. This makes it very easy to, for example, add a JSON API such as the one powering this very page (look in <code>routes/api/blog</code>)</li>
|
||||
<li>As well as pages (Svelte components, which render on server or client), you can create <em>server routes</em> in your <code>routes</code> directory. These are just <code>.js</code> files that export functions corresponding to HTTP methods, and receive Express <code>request</code> and <code>response</code> objects as arguments. This makes it very easy to, for example, add a JSON API such as the one <a href='/blog/how-is-sapper-different-from-next.json'>powering this very page</a></li>
|
||||
<li>Links are just <code><a></code> elements, rather than framework-specific <code><Link></code> components. That means, for example, that <a href='/blog/how-can-i-get-involved'>this link right here</a>, despite being inside a blob of HTML, works with the router as you'd expect.</li>
|
||||
</ul>
|
||||
`
|
||||
@@ -32,7 +32,7 @@
|
||||
},
|
||||
|
||||
preload({ params, query }) {
|
||||
return fetch(`/api/blog/contents`).then(r => r.json()).then(posts => {
|
||||
return fetch(`/blog/list.json`).then(r => r.json()).then(posts => {
|
||||
return { posts };
|
||||
});
|
||||
}
|
||||
|
||||
@@ -12,6 +12,10 @@ run('development');
|
||||
Nightmare.action('page', {
|
||||
title(done) {
|
||||
this.evaluate_now(() => document.querySelector('h1').textContent, done);
|
||||
},
|
||||
|
||||
text(done) {
|
||||
this.evaluate_now(() => document.body.textContent, done);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -193,7 +197,7 @@ function run(env) {
|
||||
});
|
||||
})
|
||||
.then(requests => {
|
||||
assert.ok(!!requests.find(r => r.url === '/api/blog/why-the-name'));
|
||||
assert.ok(!!requests.find(r => r.url === '/blog/why-the-name.json'));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -219,7 +223,7 @@ function run(env) {
|
||||
});
|
||||
})
|
||||
.then(mouseover_requests => {
|
||||
assert.ok(mouseover_requests.findIndex(r => r.url === '/api/blog/what-is-sapper') !== -1);
|
||||
assert.ok(mouseover_requests.findIndex(r => r.url === '/blog/what-is-sapper.json') !== -1);
|
||||
|
||||
return capture(() => {
|
||||
return nightmare
|
||||
@@ -228,7 +232,7 @@ function run(env) {
|
||||
});
|
||||
})
|
||||
.then(click_requests => {
|
||||
assert.ok(click_requests.findIndex(r => r.url === '/api/blog/what-is-sapper') === -1);
|
||||
assert.ok(click_requests.findIndex(r => r.url === '/blog/what-is-sapper.json') === -1);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -376,6 +380,17 @@ function run(env) {
|
||||
assert.equal(title, 'Internal server error');
|
||||
});
|
||||
});
|
||||
|
||||
it('does not attempt client-side navigation to server routes', () => {
|
||||
return nightmare.goto(`${base}/blog/how-is-sapper-different-from-next`)
|
||||
.init()
|
||||
.click(`[href="/blog/how-is-sapper-different-from-next.json"]`)
|
||||
.wait(200)
|
||||
.page.text()
|
||||
.then(text => {
|
||||
JSON.parse(text);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('headers', () => {
|
||||
@@ -415,13 +430,13 @@ function run(env) {
|
||||
'blog/what-is-sapper/index.html',
|
||||
'blog/why-the-name/index.html',
|
||||
|
||||
'api/blog/contents',
|
||||
'api/blog/a-very-long-post',
|
||||
'api/blog/how-can-i-get-involved',
|
||||
'api/blog/how-is-sapper-different-from-next',
|
||||
'api/blog/how-to-use-sapper',
|
||||
'api/blog/what-is-sapper',
|
||||
'api/blog/why-the-name',
|
||||
'blog/list.json',
|
||||
'blog/a-very-long-post.json',
|
||||
'blog/how-can-i-get-involved.json',
|
||||
'blog/how-is-sapper-different-from-next.json',
|
||||
'blog/how-to-use-sapper.json',
|
||||
'blog/what-is-sapper.json',
|
||||
'blog/why-the-name.json',
|
||||
|
||||
'favicon.png',
|
||||
'global.css',
|
||||
|
||||
Reference in New Issue
Block a user