Comments (3)
I am leaving react.js for mithril as all the SPA frameworks nowadays are focusing on SSR and server component. In fact there are many use cases for client-side only(do not care about SEO at all), I hope mithril stays the way it is, be the best lightweight CSR SPA forward.
from mithril.js.
I wanted to follow up on this, as I need to make the decision soon (The decision: to use React or Next instead...gasp!)
To summarize my situation: my component code must live on the server. Pushing these components to clients is the product, so this is a hard requirement (I not doing some weird SEO thing) I don't own the client., but can only provide a code snippet to the client dev. Using Mithril would be ideal because it is so lightweight.
To nail down what I'm hoping for, the client code should look something like this:
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Mithril Server Component Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="https://unpkg.com/mithril/mithril.js"></script>
</head>
<body>
<div>
<div id="divID"></div>
</div>
<!-- client's SPA goes here -->
<script>
const loadComponent = (divID, apiKey, componentID, otherParams) => {
const url = `myComponentServer?component=${componentID}&otherParams=${otherParams}`;
const h = new Headers();
h.append("Content-Type", "application/json");
h.append(ApiKeyHeader, apiKey);
const req = new Request(url, {
method: "get",
headers: h,
});
fetch(req)
.then(async (res) => {
if (res.status !== 200) {
throw new Error(`Bad request: (${res.status}): ${res.statusText}`);
}
const body = await res.json();
return body;
})
.then((content) => {
const { component } = content;
m.mount(document.getElementById(divID), component)
})
.catch((e) => {
logEventConsole(`Error: loadSurvey: ${apiKey}, ${customerID}, ${surveyID}, ${userID}`, e);
});
};
document.addEventListener('DOMContentLoaded', (event) => {
loadComponent("divID", apiKey, componentID, otherParams);
})
</script>
</body>
</html>
Looks pretty normal so far. The trick is the server endpoint, which might look like:
const myComponent = () => ({
tag: "myComponent",
oninit: () => {},
attrs: () => {},
state: () => {},
view: (v) => m("main", [
m("h1", {class: "title"}, "My first app"),
m("button", {onclick: () => {alert('clicked!')}}, "A button"),
]);
});
app.get("myComponentServer", validateKey, (req: Request, res: Response) => {
const component = getSurvey(req, res, account); // returns myComponent based on componentID
if (!component) return;
const comp = magicallyRenderedComponent(component); // returns component js
const compJSON = JSON.stringify(comp);
res.status(200).send(compJSON);
});
The server needs to return something the client's mithril can mount. Is this possible? From what I can tell, magicallyRenderedComponent doesn't exist.
(When I first started, I discovered mithril-node-render, but it appears to only return the html tagging and strips out all the js code so that won't do. )
from mithril.js.
GIving up.
from mithril.js.
Related Issues (20)
- docs: tic tac toe example broken HOT 2
- TS + JSX users should be always using class components only, and this needs to be documented. HOT 1
- Is there any chance Mithril could run in Deno? HOT 4
- I can prevent a mithril component from scrolling to the top on a rerender. HOT 5
- Confused about component redraws (and keys) HOT 8
- Why are v.attrs passed to oninit() for a child component an empty object? HOT 5
- There is no onsize/onresize lifecycle method HOT 4
- Typescript return type for mithril components HOT 14
- warning 'reuse attrs object' while receiving a component from high order function/component HOT 4
- Broken link to ospec in API page
- make vite the default HOT 4
- Redraw on resolve of async event handler HOT 1
- Please update the Vue comparison HOT 1
- onbeforeremove not being triggered when list element is removed HOT 2
- Form attribute not set in custom element HOT 1
- Add "main" field to package.json
- Need help with "Failed to execute 'insertBefore' on 'Node'" errors. HOT 2
- Is there a bug with dynamically changing svgs? HOT 15
- Current vnode `dom` property is undefined in `onbeforeupdate` HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mithril.js.