From 8887791e725858b3f6fc9c8d69b07890cc146492 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Sun, 18 Mar 2018 22:38:14 -0400 Subject: [PATCH] use fetch, tidy up --- app/server.js | 14 ++------------ routes/blog/[slug].html | 2 +- routes/blog/index.html | 2 +- 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/app/server.js b/app/server.js index bbd9dcc..74258bc 100644 --- a/app/server.js +++ b/app/server.js @@ -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); diff --git a/routes/blog/[slug].html b/routes/blog/[slug].html index da4728b..df17455 100644 --- a/routes/blog/[slug].html +++ b/routes/blog/[slug].html @@ -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 }; }); } diff --git a/routes/blog/index.html b/routes/blog/index.html index 6c98653..1b5f62c 100644 --- a/routes/blog/index.html +++ b/routes/blog/index.html @@ -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 }; }); }