1
0
mirror of https://github.com/trambarhq/relaks-wordpress-example.git synced 2025-09-02 20:52:33 +02:00

Fixed pagination in CORS scenario (issue #24).

This commit is contained in:
Chung Leong
2019-01-28 11:11:49 +01:00
parent 09186c2341
commit f44390a3b5
4 changed files with 30 additions and 11 deletions

View File

@@ -3,19 +3,29 @@ const CrossFetch = require('cross-fetch');
const WORDPRESS_HOST = process.env.WORDPRESS_HOST;
async function fetch(path) {
console.log(`Retrieving data: ${path}`);
let url = `${WORDPRESS_HOST}${path}`;
let res = await CrossFetch(url);
let object = await res.json();
let resText = await res.text();
let object;
try {
object = JSON.parse(resText);
} catch (err) {
// remove any error msg that got dumped into the output stream
resText = resText.replace(/^[^\{\[]+/, '');
object = JSON.parse(resText);
}
let total = parseInt(res.headers.get('X-WP-Total'));
removeSuperfluousProps(path, object);
let text = JSON.stringify(object);
return { path, text };
return { path, text, total };
}
function removeSuperfluousProps(path, object) {
if (object instanceof Array) {
let objects = object;
for (let object of objects) {
return removeSuperfluousProps(path, object);
removeSuperfluousProps(path, object);
}
} else if (object instanceof Object) {
delete object._links;

View File

@@ -18,6 +18,7 @@ server {
proxy_ignore_headers Cache-Control Expires Set-Cookie;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Expose-Headers X-WP-Total;
add_header Cache-Control "public,max-age=0";
add_header X-Cache-Date $upstream_http_date;
add_header X-Cache-Status $upstream_cache_status;

View File

@@ -29,6 +29,7 @@ server {
proxy_ignore_headers Cache-Control Expires Set-Cookie;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Expose-Headers X-WP-Total;
add_header Cache-Control "public,max-age=0";
add_header X-Cache-Date $upstream_http_date;
add_header X-Cache-Status $upstream_cache_status;

View File

@@ -93,20 +93,27 @@ class SideNav extends AsyncComponent {
meanwhile.show(<SideNavSync {...props} />);
// load the posts of each category
for (let category of props.categories) {
if (category.count > 0) {
let url = `/wp/v2/posts/?categories=${category.id}`;
await wp.fetchList(url);
try {
for (let category of props.categories) {
if (category.count > 0) {
let url = `/wp/v2/posts/?categories=${category.id}`;
await wp.fetchList(url);
}
}
} catch (err) {
}
// load the posts of each tag
for (let tag of props.tags) {
if (tag.count > 0) {
let url = `/wp/v2/posts/?tags=${tag.id}`;
await wp.fetchList(url);
try {
for (let tag of props.tags) {
if (tag.count > 0) {
let url = `/wp/v2/posts/?tags=${tag.id}`;
await wp.fetchList(url);
}
}
} catch (err) {
}
return <SideNavSync {...props} />;
}