mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-12-27 05:44:17 +03:00
Extract helper module in preparation for npm package extraction.
This commit is contained in:
parent
91a8c0e0bc
commit
c2bc4be5fb
@ -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);
|
||||
}
|
||||
|
27
examples/docs/src/js/helper.ts
Normal file
27
examples/docs/src/js/helper.ts
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user