mirror of
https://github.com/kevin-DL/sapper-template.git
synced 2026-01-20 21:45:10 +00:00
use fetch, tidy up
This commit is contained in:
@@ -1,20 +1,10 @@
|
|||||||
import { resolve } from 'url';
|
|
||||||
import polka from 'polka';
|
import polka from 'polka';
|
||||||
import compression from 'compression';
|
import compression from 'compression';
|
||||||
import sapper from 'sapper';
|
import sapper from 'sapper';
|
||||||
import serve from 'serve-static';
|
import serve from 'serve-static';
|
||||||
import fetch from 'node-fetch';
|
|
||||||
import { routes } from './manifest/server.js';
|
import { routes } from './manifest/server.js';
|
||||||
|
|
||||||
const { PORT } = process.env;
|
polka() // You can also use Express
|
||||||
|
|
||||||
// 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()
|
|
||||||
.use(compression({ threshold: 0 }))
|
.use(compression({ threshold: 0 }))
|
||||||
.use(serve('assets'), sapper({ routes }))
|
.use(serve('assets'), sapper({ routes }))
|
||||||
.listen(PORT);
|
.listen(process.env.PORT);
|
||||||
|
|||||||
@@ -59,7 +59,7 @@
|
|||||||
// is called [slug].html
|
// is called [slug].html
|
||||||
const { slug } = params;
|
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 };
|
return { post };
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
},
|
},
|
||||||
|
|
||||||
preload({ params, query }) {
|
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 };
|
return { posts };
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user