- JavaScript 77%
- TypeScript 10%
- HTML 6%
- Rust 6%
- CSS <1%
- 2 more languages <1%
Simplified Full Stack #
Inspired by this essay. I want to see the limitations of this way of building web applications. So I recreated a modern web app by the City of Baltimore. The app is simple, it lets residents explore and track tickets for city service requests.
The little section I recreated is the exploration page. I chose it because it looks very interactive. There's a map, an infinite scroll and info boxes that react to clicks.
TL;DR: I think it's a very nice way to work. The big idea is that APP STATE is delegated to the server. The frontend becomes the View in an MVC framework. It holds as little state as possible. Instead of taking JSON from the server, it takes HTML markup and places it where it needs to be. The result is a very light frontend and a "heavy" backend.
I had to do a bit of exploration to get the workflow just right. I picked up tools that provides something similar to what I'm used to with the popular frontend frameworks.
- Reusable components: Web components
- Inline CSS (scoped): css-scope-inline
- JS where you need them: Alpine.js
And the thing that makes it all possible: HTMX. These libraries are good at one thing and they work well together. Look at how they look together, it's chaos! if it wasn't so pretty xD.
This is rust. But the backend shouldn't matter but in my mind of course faster is better. Its very simple really, I only implemented one endpoint for the whole page effectively. One is to serve the initial HTML and this to serve the content for the infinite scroll.
It's the usual stuff nothing special. You fetch some data and you send it to the client. But instead of structuring JSON, I'm structuring markup, HTML in plaintext. With JSON you have to pack data in a box then unpack it at the frontend to build the markup in plaintext. If the same person is doing the packing and unpacking, that can feel redundant very quick.
This way of building thing is cool for small teams. When everyone has some idea of what's happening in the whole stack. If somebody were to be a dedicated frontend developer with this stack, they'll just end up backend code since the endpoint API design will be tied to the UI. Unless they work on some crazy complicated web components.
let mut result_markup = String::new();
for request in service_requests {
let media_list = sqlx::query_as!(
Media,
r#"
SELECT
m._id as id,
file_name,
url,
is_private,
mime_type
FROM media as m
JOIN service_request___media as srm on m._id = srm.m_id
WHERE srm.sr_id = $1
"#,
request._id
)
.fetch_all(conn)
.await?;
let mut media_markup = String::new();
for media in media_list {
media_markup.push_str(&format!(
r#"
<b-frame ratio="1:1">
<img src="{0}" alt="{1}" />
</b-frame>
"#,
media.url, media.file_name
));
}
result_markup.push_str(&format!(
r##"
<b-box
x-data="{{open: false}}"
x-init="L.marker([{2}, {3}]).addTo(map)"
@click="map.flyTo([{2}, {3}], 17, {{
duration: .20,
}});
open = !open;
">
<style>
me {{
position: relative;
text-align: center;
h1 {{
font-size: var(--s3);
}}
&:hover {{
cursor: pointer;
}}
}}
</style>
<h1>{0}</h1>
<b-reel>
{1}
</b-reel>
<template x-teleport="#map">
<b-box x-show="open">
<style>
me {{
position: absolute;
bottom: 1%;
left: 1%;
max-inline-size: var(--measure);
max-block-size: var(--measure);
z-index: 1000;
}}
</style>
<div x-data="{{
copyText: '{5}',
copyNotification: false,
copyToClipboard() {{
navigator.clipboard.writeText(this.copyText);
this.copyNotification = true;
let that = this;
setTimeout(function(){{
that.copyNotification = false;
}}, 3000);
}}
}}">
<button @click="copyToClipboard();">
<style>
me {{
display: flex;
svg {{
width: calc(var(--s0));
height: calc(var(--s0));
}}
}}
</style>
<span x-show="!copyNotification">Copy to Clipboard</span>
<svg x-show="!copyNotification" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6"><path stroke-linecap="round" stroke-linejoin="round" d="M9 12h3.75M9 15h3.75M9 18h3.75m3 .75H18a2.25 2.25 0 002.25-2.25V6.108c0-1.135-.845-2.098-1.976-2.192a48.424 48.424 0 00-1.123-.08m-5.801 0c-.065.21-.1.433-.1.664 0 .414.336.75.75.75h4.5a.75.75 0 00.75-.75 2.25 2.25 0 00-.1-.664m-5.8 0A2.251 2.251 0 0113.5 2.25H15c1.012 0 1.867.668 2.15 1.586m-5.8 0c-.376.023-.75.05-1.124.08C9.095 4.01 8.25 4.973 8.25 6.108V8.25m0 0H4.875c-.621 0-1.125.504-1.125 1.125v11.25c0 .621.504 1.125 1.125 1.125h9.75c.621 0 1.125-.504 1.125-1.125V9.375c0-.621-.504-1.125-1.125-1.125H8.25zM6.75 12h.008v.008H6.75V12zm0 3h.008v.008H6.75V15zm0 3h.008v.008H6.75V18z" /></svg>
<span x-show="copyNotification" x-cloak>Copied to Clipboard</span>
<svg x-show="copyNotification" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" x-cloak><path stroke-linecap="round" stroke-linejoin="round" d="M11.35 3.836c-.065.21-.1.433-.1.664 0 .414.336.75.75.75h4.5a.75.75 0 00.75-.75 2.25 2.25 0 00-.1-.664m-5.8 0A2.251 2.251 0 0113.5 2.25H15c1.012 0 1.867.668 2.15 1.586m-5.8 0c-.376.023-.75.05-1.124.08C9.095 4.01 8.25 4.973 8.25 6.108V8.25m8.9-4.414c.376.023.75.05 1.124.08 1.131.094 1.976 1.057 1.976 2.192V16.5A2.25 2.25 0 0118 18.75h-2.25m-7.5-10.5H4.875c-.621 0-1.125.504-1.125 1.125v11.25c0 .621.504 1.125 1.125 1.125h9.75c.621 0 1.125-.504 1.125-1.125V18.75m-7.5-10.5h6.375c.621 0 1.125.504 1.125 1.125v9.375m-8.25-3l1.5 1.5 3-3.75" /></svg>
</button>
</div>
<h2>{4}</h2>
<span>{5}</span>
<div>
<span>EXPECTED RESOLUTION</span>
<h3 x-text="new Date('{6}').toString()"></h3>
<div>
</b-box>
</template>
</b-box>
"##,
request.id,
media_markup,
request.long,
request.lat,
request.service_name,
request.address,
request.expected_date_time
));
Messy? Sure, but I think its worth it. A very nice side-effect of it is that there's way less JavaScript to ship! Those libraries I listed are written in JS after all. But you as the user of those libraries get write significantly less or none at all (depends on your needs ofc.)
But lets look at the difference:
Original: https://balt311.baltimorecity.gov/citizen/s/service-requests?servicerequested=all My Implementation: https://311-services.fly.dev/
Granted, my version has a lot less features. But try opening up the dev tools,
disable cache, and refresh. The difference is huge (at least on first load.)
The original has to download 2.49MB of JS/CSS compared to 130kB with my
implementation. I still have a lot of room to add more features and JavaScript.
Of course this is a small app, I've yet to experience what it's like at scale. In terms of code, features, and team size.