Skip to content

Commit

Permalink
Fix sapper preloading by adding server api-route
Browse files Browse the repository at this point in the history
  • Loading branch information
VanishMax committed Jan 5, 2020
1 parent b427db6 commit 8b0b031
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"dependencies": {
"compression": "^1.7.1",
"node-fetch": "^2.6.0",
"polka": "next",
"sapper-environment": "^1.0.1",
"sirv": "^0.4.0"
Expand Down
12 changes: 12 additions & 0 deletions src/routes/store/index.json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import request from '@/utils/request';
import fetch from 'node-fetch';

export async function get(req, res, next) {
const result = await request(fetch, '/products?limit=24');
if (result) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(result));
} else {
next();
}
}
5 changes: 2 additions & 3 deletions src/routes/store.svelte → src/routes/store/index.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<script context="module">
import request from '@/utils/request';
export async function preload() {
const res = await request(this.fetch, '/products');
return {items: res};
const res = await this.fetch('/store.json');
return {items: await res.json()};
}
</script>

Expand Down
10 changes: 5 additions & 5 deletions src/store/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,24 +118,24 @@ export const createProduct = async () => {
const product = get(item);
let varieties = [];
product.varieties.forEach(variety => {
let color = variety.color.replace(/[^0-9a-f]/gi, '');
let color = variety.color ? variety.color.replace(/[^0-9a-f]/gi, '') : null;
let imgs = variety.images.map(img => img.url);

if (product.inSizes) {
for (let size in variety.sizes) {
if (variety.sizes[size] > 0) {
varieties.push({
color: color,
size,
color,
images: imgs,
amount: variety.sizes[size],
size,
});
}
}
} else {
varieties.push({
color: variety.color.replace(/[^0-9a-f]/gi, ''),
images: variety.images.map(img => img.url),
color,
images: imgs,
amount: variety.quantity,
size: null,
});
Expand Down
8 changes: 5 additions & 3 deletions src/utils/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ export default async (fetchType = fetch, url = '', method = 'GET', body = {}, is
if (isForm) options.body = body;
else if (Object.entries(body).length) options.body = JSON.stringify(body);

let headers = new Headers();
if (!isForm && Object.entries(body).length) headers.append('Content-Type', 'application/json');
options.headers = headers;
if (!isForm && Object.entries(body).length) {
options.headers = {
'Content-Type': 'application/json',
};
}

return await fetchType(API_HOST + url, options).then(res => {
if (res.status === 204) return true;
Expand Down

0 comments on commit 8b0b031

Please sign in to comment.