Files
sapper/test/app/routes/blog/[slug].html
2018-03-17 13:18:18 -04:00

36 lines
701 B
HTML

<:Head>
<title>{{post.title}}</title>
</:Head>
<h1>{{post.title}}</h1>
<div class='content'>
{{{post.html}}}
</div>
<script>
export default {
preload({ params, query }) {
// the `slug` parameter is available because this file
// is called [slug].html
const { slug } = params;
if (slug === 'throw-an-error') {
return this.error(500, 'something went wrong');
}
return fetch(`blog/${slug}.json`).then(r => {
if (r.status === 200) {
return r.json().then(post => ({ post }));
this.error(r.status, '')
}
if (r.status === 404) {
this.error(404, 'Not found');
} else {
throw new Error('Something went wrong');
}
});
}
};
</script>