chore: replace hand-rolled layout() with @fastify/view + EJS #19
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
HTML is generated by TypeScript template-literal functions in
src/views/. Alayout()function insrc/views/layout.tsbuilds the full<!DOCTYPE html>shell as a string, and each page module (e.g.upload.ts,file-list.ts) builds its body as a string and passes it tolayout(). Routes callreply.send(layout('Title', body, opts)).This works but is non-standard — no syntax highlighting in templates, no partial reuse, manual
escHtml()calls required everywhere, and the pattern differs from every other project in the fleet which is moving to@fastify/view.Proposed fix
Introduce
@fastify/view+ EJS and convert the view modules to.ejstemplate files.1. Install
Register in the server with
root: 'views/'.2. Layout template
Create
views/_layout.ejsfrom the currentlayout()function. Template variables mirror the currentLayoutOptions:3. Convert page modules to templates
Each
src/views/foo.tsthat returns an HTML string becomesviews/foo.ejs. EJS auto-escapes<%= %>output, so explicitescHtml()calls on user data are no longer needed.4. Update routes
Replace
reply.send(viewFn(data))withreply.view('foo.ejs', { title, authed, ...data }). Thesrc/views/*.tsmodules andlayout.tscan be deleted.View inventory
layout.tsviews/_layout.ejstitle,authed,hideHeaderlogin.tsviews/login.ejsupload.tsviews/upload.ejsfile-list.tsviews/file-list.ejsfilesfile-view.tsviews/file-view.ejsfilenot-found.tsviews/not-found.ejsFiles to touch
package.json— add@fastify/view,ejssrc/server.ts(or wherever Fastify is configured) — register pluginreply.send(view(...))→reply.view('template.ejs', vars)src/views/— delete all.tsmodules once templates are in placeviews/directory with.ejstemplatesResolved: PR #20 (merge_commit
829037f89c)