FIX: Fallback page loading

This commit is contained in:
Tony Air 2021-03-30 12:37:02 +07:00
parent 7f9a0f9d7a
commit 4ffbfa5a01
4 changed files with 443 additions and 417 deletions

View File

@ -13,9 +13,9 @@ import { onError } from '@apollo/client/link/error';
const NAME = '_appolo'; const NAME = '_appolo';
const API_META = document.querySelector('meta[name="api_url"]'); const API_META = document.querySelector('meta[name="api_url"]');
const API_URL = API_META const API_URL = API_META ?
? API_META.getAttribute('content') API_META.getAttribute('content') :
: `${window.location.protocol }//${ window.location.host }/graphql`; `${window.location.protocol }//${ window.location.host }/graphql`;
const authMiddleware = new ApolloLink((operation, forward) => { const authMiddleware = new ApolloLink((operation, forward) => {
// add the authorization to the headers // add the authorization to the headers
@ -38,16 +38,18 @@ const link = from([
}), }),
onError(({ operation, response, graphQLErrors, networkError, forward }) => { onError(({ operation, response, graphQLErrors, networkError, forward }) => {
if (operation.operationName === 'IgnoreErrorsQuery') { if (operation.operationName === 'IgnoreErrorsQuery') {
console.error(`${NAME}: IgnoreErrorsQuery`);
response.errors = null; response.errors = null;
return; return;
} }
if (graphQLErrors) if (graphQLErrors) {
graphQLErrors.forEach(({ message, locations, path }) => graphQLErrors.forEach(({ message, locations, path }) =>
console.error( console.error(
`${NAME}: [GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`, `${NAME}: [GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
), ),
); );
}
if (networkError) { if (networkError) {
/*let msg = ''; /*let msg = '';
@ -62,11 +64,11 @@ const link = from([
msg = 'Something went wrong.'; msg = 'Something went wrong.';
break; break;
}*/ }*/
console.error(`${NAME}: [Network error] ${networkError.statusCode}`); console.error(`${NAME}: [Network error] ${networkError.statusCode}`);
window.dispatchEvent(new Event(Events.OFFLINE));
} }
console.error(`${NAME}: [APOLLO_ERROR]`);
window.dispatchEvent(new Event(Events.APOLLO_ERROR));
}), }),
new ApolloLink((operation, forward) => { new ApolloLink((operation, forward) => {
return forward(operation).map((data) => { return forward(operation).map((data) => {

View File

@ -14,6 +14,7 @@ const MainUILinks = ((W) => {
const BODY = D.body; const BODY = D.body;
class MainUILinks { class MainUILinks {
window
static init() { static init() {
const ui = this; const ui = this;
ui.GraphPage = null; ui.GraphPage = null;
@ -140,6 +141,7 @@ const MainUILinks = ((W) => {
const link = el.getAttribute('href') || el.getAttribute('data-href'); const link = el.getAttribute('href') || el.getAttribute('data-href');
ui.GraphPage.state.current = el; ui.GraphPage.state.current = el;
ui.GraphPage.load(link) ui.GraphPage.load(link)
@ -148,6 +150,8 @@ const MainUILinks = ((W) => {
el.classList.remove('loading'); el.classList.remove('loading');
el.classList.add('active'); el.classList.add('active');
D.loading_apollo_link = null;
if (ui.GraphPage.state.Link) { if (ui.GraphPage.state.Link) {
window.history.pushState({ window.history.pushState({
page: JSON.stringify(ui.GraphPage.state), page: JSON.stringify(ui.GraphPage.state),
@ -166,12 +170,15 @@ const MainUILinks = ((W) => {
console.groupEnd(`${NAME}: load on click`); console.groupEnd(`${NAME}: load on click`);
}) })
.catch((e) => { .catch((e) => {
console.error(`${NAME}: loading error`);
console.log(e); console.log(e);
BODY.classList.remove('ajax-loading'); BODY.classList.remove('ajax-loading');
el.classList.remove('loading'); el.classList.remove('loading');
el.classList.add('error', `response-${e.status}`); el.classList.add('error', `response-${e.status}`);
/*switch (e.status) { /*switch (e.status) {
case 500:
break;
case 404: case 404:
el.classList.add('not-found'); el.classList.add('not-found');
break; break;
@ -184,6 +191,11 @@ const MainUILinks = ((W) => {
window.dispatchEvent(new Event(Events.AJAX)); window.dispatchEvent(new Event(Events.AJAX));
console.groupEnd(`${NAME}: load on click`); console.groupEnd(`${NAME}: load on click`);
console.log(`${NAME}: reloading page ${link}`);
// fallback loading
W.location.href = link;
}); });
} }
} }
@ -195,6 +207,15 @@ const MainUILinks = ((W) => {
W.addEventListener(`${Events.AJAX}`, () => { W.addEventListener(`${Events.AJAX}`, () => {
MainUILinks.loaded(); MainUILinks.loaded();
}); });
// fallback
/*W.addEventListener(`${Events.APOLLO_ERROR}`, (e) => {
console.error(`${NAME}: [APOLLO_ERROR] loading failure, reloading the page`);
//W.dispatchEvent(new Event(Events.OFFLINE));
if (D.loading_apollo_link) {
W.location.href = D.loading_apollo_link;
}
});*/
})(window); })(window);
export default MainUILinks; export default MainUILinks;

View File

@ -89,8 +89,7 @@ class Page extends Component {
Object.assign(ui.empty_state, { Object.assign(ui.empty_state, {
Title: 'Offline', Title: 'Offline',
CSSClass: 'graphql__status-523', CSSClass: 'graphql__status-523',
Summary: Summary: "You're Offline. The page is not available now.",
"You're Offline. The page is not available now.",
loading: false, loading: false,
}), }),
); );
@ -119,6 +118,9 @@ class Page extends Component {
console.log(`${ui.name}: not found`); console.log(`${ui.name}: not found`);
reject({ status: 404 }); reject({ status: 404 });
} }
})
.catch((error) => {
reject({ status: 500, error: error });
}); });
} }
}); });
@ -152,7 +154,7 @@ class Page extends Component {
Summary: page.Summary, Summary: page.Summary,
Link: page.Link, Link: page.Link,
HTML: page.HTML, HTML: page.HTML,
Elements: [],//page.Elements.edges, Elements: [], //page.Elements.edges,
loading: false, loading: false,
}); });
@ -178,10 +180,10 @@ class Page extends Component {
); );
let html = ''; let html = '';
if(ui.state.HTML) { if (ui.state.HTML) {
console.log(`${ui.name}: HTML only`); console.log(`${ui.name}: HTML only`);
html = ui.state.HTML; html = ui.state.HTML;
}else if (ui.state.Elements.length) { } else if (ui.state.Elements.length) {
console.log(`${ui.name}: render`); console.log(`${ui.name}: render`);
ui.state.Elements.map((el) => { ui.state.Elements.map((el) => {
html += el.node.Render; html += el.node.Render;
@ -191,7 +193,7 @@ class Page extends Component {
html = `<div class="summary">${ui.state.Summary}</div>`; html = `<div class="summary">${ui.state.Summary}</div>`;
} }
if(ui.state.loading){ if (ui.state.loading) {
const spinner = D.getElementById('PageLoading'); const spinner = D.getElementById('PageLoading');
html = `<div class="loading">Loading ...</div>`; html = `<div class="loading">Loading ...</div>`;
} }

View File

@ -3,6 +3,7 @@
*/ */
export default { export default {
APOLLO_ERROR: 'apollo-error',
AJAX: 'ajax-load', AJAX: 'ajax-load',
AJAXMAIN: 'ajax-main-load', AJAXMAIN: 'ajax-main-load',
MAININIT: 'main-init', MAININIT: 'main-init',