pkra / laml-wc

A continuation of an experiment
https://pkra.github.io/laml-wc
0 stars 0 forks source link

SSR #7

Closed pkra closed 11 months ago

pkra commented 12 months ago

Let's add a demo/test to render server-side.

pkra commented 12 months ago

A bit rough (eval!) but gets the job done:

import { parseHTML } from 'linkedom';
import fs from 'node:fs';

const {
    window, document, customElements,
    HTMLElement,
    Event, CustomEvent
} = parseHTML(fs.readFileSync('./m522.html').toString());

document.querySelector('script').remove();

eval(fs.readFileSync('./customelements.js'))

console.log(document.toString())
pkra commented 11 months ago

no idea if the previous version ever worked but here's an updated one that does:

import { parseHTML } from 'linkedom';
import fs from 'node:fs';

const {
    window, document, customElements,
    HTMLElement,
    Event, CustomEvent
} = parseHTML(fs.readFileSync('./m522.html').toString());

// make some globals so customelements.js can find it
globalThis.HTMLElement = HTMLElement;
globalThis.customElements = customElements;
globalThis.document =  document;

// upgrade/render our elements
await import('./customelements.js');
// TODO: MathJax SSR

// add pagedjs
// document.querySelector('script[src="customelements.js"]').setAttribute('src', 'https://unpkg.com/pagedjs/dist/paged.polyfill.js');

// save
console.log(document.toString())
pkra commented 11 months ago

I added SSR demos. More work to be done but in another issue.