mirror of
https://github.com/kevin-DL/sapper-template.git
synced 2026-01-12 02:15:17 +00:00
use fetch, tidy up
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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 };
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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 };
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user