use fetch, tidy up

This commit is contained in:
Rich Harris
2018-03-18 22:38:14 -04:00
parent 06e24b3eed
commit 8887791e72
3 changed files with 4 additions and 14 deletions

View File

@@ -1,20 +1,10 @@
import { resolve } from 'url';
import polka from 'polka';
import compression from 'compression';
import sapper from 'sapper';
import serve from 'serve-static';
import fetch from 'node-fetch';
import { routes } from './manifest/server.js';
const { PORT } = process.env;
// this allows us to do e.g. `fetch('/api/blog-posts')` on the server
global.fetch = (url, opts) => {
url = resolve(`http://localhost:${PORT}/`, url);
return fetch(url, opts);
};
polka()
polka() // You can also use Express
.use(compression({ threshold: 0 }))
.use(serve('assets'), sapper({ routes }))
.listen(PORT);
.listen(process.env.PORT);

View File

@@ -59,7 +59,7 @@
// is called [slug].html
const { slug } = params;
return fetch(`blog/${slug}.json`).then(r => r.json()).then(post => {
return this.fetch(`blog/${slug}.json`).then(r => r.json()).then(post => {
return { post };
});
}

View File

@@ -32,7 +32,7 @@
},
preload({ params, query }) {
return fetch(`blog.json`).then(r => r.json()).then(posts => {
return this.fetch(`blog.json`).then(r => r.json()).then(posts => {
return { posts };
});
}