Skip to content

Commit

Permalink
Minor changes to dynamic imports
Browse files Browse the repository at this point in the history
  • Loading branch information
hising committed Jun 14, 2020
1 parent 35e87a7 commit e0d35ba
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions src/core/cms.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,30 +79,40 @@ export const isLocalScript = (script) => {
return true;
};

export const renderPage = (page) => {
setAppContent(page.html);
if (page.attributes.hasOwnProperty('scripts')) {
const customScripts = page.attributes.scripts;
customScripts.forEach((script) => {
if (isLocalScript(script)) {
import('../' + script);
} else {
// TODO: Add to head and remove on page view
}
});
}
setAppTitle(page.attributes.title || document.title);
listenForEmbeds();
};

export const loadPage = async (tpl, type = 'pages') => {
setAppContent('<div class="page-loader"><span>Loading</span></div>', true);
removeMetaTag('robots', 'noindex'); // TODO: Do this when you leave a noindex page
try {
let page = null;
if (pageCache.hasOwnProperty(tpl)) {
page = pageCache[tpl];
renderPage(pageCache[tpl]);
} else {
page = await import(/* webpackChunkName: "pages" */ `../${type}/${tpl}.md`);
pageCache[tpl] = page;
}
setAppContent(page.html);
if (page.attributes.hasOwnProperty('scripts')) {
const customScripts = page.attributes.scripts;
customScripts.forEach((script) => {
if (isLocalScript(script)) {
import('../' + script);
} else {
// TODO: Add to head and remove on page view
}
});
import(
/* webpackPrefetch: true */ /* webpackChunkName: "pages" */ `../${type}/${tpl}.md`
)
.then(({default: page}) => {
pageCache[tpl] = page;
renderPage(page);
})
.catch(() => {
handle404(error);
});
}
setAppTitle(page.attributes.title || document.title);
listenForEmbeds();
} catch (error) {
await handle404(error);
}
Expand Down

0 comments on commit e0d35ba

Please sign in to comment.