Extract helper module in preparation for npm package extraction.

This commit is contained in:
Dillon Kearns 2019-08-05 18:08:01 -07:00
parent 91a8c0e0bc
commit c2bc4be5fb
2 changed files with 32 additions and 24 deletions

View File

@ -2,29 +2,10 @@
import { Elm } from "../Main.elm";
// @ts-ignore
import { imageAssets, routes } from "./image-assets";
import { pagesInit } from "./helper";
document.addEventListener("DOMContentLoaded", function() {
let app = Elm.Main.init({
node: document.getElementById("app"),
flags: { imageAssets, routes }
});
app.ports.toJsPort.subscribe((Heads: [Head]) => {
if (navigator.userAgent.indexOf("Headless") >= 0) {
Heads.forEach(Head => {
appendTag(Head);
});
}
document.dispatchEvent(new Event("prerender-trigger"));
});
pagesInit({
mainElmModule: Elm.Main,
imageAssets,
routes
});
type Head = { name: string; attributes: [[string, string]] };
function appendTag(tagDetails: Head) {
const meta = document.createElement(tagDetails.name);
tagDetails.attributes.forEach(([name, value]) => {
meta.setAttribute(name, value);
});
document.getElementsByTagName("head")[0].appendChild(meta);
}

View File

@ -0,0 +1,27 @@
export function pagesInit({ mainElmModule, imageAssets, routes }: any) {
document.addEventListener("DOMContentLoaded", function() {
let app = mainElmModule.init({
node: document.getElementById("app"),
flags: { imageAssets, routes }
});
app.ports.toJsPort.subscribe((Heads: [Head]) => {
if (navigator.userAgent.indexOf("Headless") >= 0) {
Heads.forEach(Head => {
appendTag(Head);
});
}
document.dispatchEvent(new Event("prerender-trigger"));
});
});
type Head = { name: string; attributes: [[string, string]] };
function appendTag(tagDetails: Head) {
const meta = document.createElement(tagDetails.name);
tagDetails.attributes.forEach(([name, value]) => {
meta.setAttribute(name, value);
});
document.getElementsByTagName("head")[0].appendChild(meta);
}
}