How datawrapper/frontend works
Project type and purpose
• This is the Datawrapper Frontend service: a Node.js/Hapi-based full-stack “frontend” application that performs server-side rendering (SSR) of Svelte views, hydrates them client-side, serves static assets, and exposes a plugin system and localization pipeline.
File structure
• Top-level directories and files:
• locale/ (translation JSONs)
• src/auth/ (Hapi cookie-session adapter)
• src/routes/ (route handlers/controllers)
• src/utils/ (plugin loader, Svelte-view adapter, event hooks, caching)
• src/views/ (Svelte and Pug templates, layouts, partials)
• src/styles/ (SCSS for static CSS build)
• static/ (precompiled CSS, JS, fonts)
• package.json, nodemon.json, Husky hooks, ESLint config
• Key patterns:
• MVC-style routing (routes → handlers → views)
• Plugin-based extensibility via Hapi plugins + event hooks
• SSR + CSR compilation of Svelte (with cache)
• Localization via locale JSONs pulled from Lokalise
README for additional insights
• Middleware: authentication adapter, Svelte-view engine with compile-and-cache strategy, view pre-compilation on startup
• Plugin model: server.register(frontend.cjs), symlinking plugin Svelte views into src/views/plugins
• Event hooks for extending admin navigation
• Global Svelte stores exposed via Hapi view context (e.g. user info, messages)
Main components
• Client Browser
• Requests pages, assets, hydration bundles
• Hapi Frontend Service
• Authentication Adapter (src/auth)
• Route Controllers (src/routes)
• Plugin Loader & Event Hook Manager (src/utils/plugin-loader, events.js)
• Svelte-View Engine (src/utils/svelte-view)
‣ Template Compiler (SSR & CSR bundling via Rollup)
‣ Cache Layer (Map cache, optional Redis)
‣ View Pre-processor (prepareView)
‣ Global Store Context Injector
• Localization Module (loads locale/*.json, exposes messages store)
• Static File Server (serves static/ assets and /lib/csr bundles)
• External Services
• Datawrapper API (for data, chart definitions, user info)
• Lokalise API (for updating locale files)
Relationships and interactions
• Browser → Hapi Server: HTTP GET/POST → server.route handlers
• Handlers → Auth Adapter: validate session, attach user context
• Handlers → Plugin-registered routes and event hooks
• Handlers → Svelte-View Engine: SSR render of .svelte or .pug view with props + stores
• Svelte-View Engine ↔ Cache Layer: compile or retrieve compiled templates
• SSR Response → Browser: initial HTML + JSON for hydration
• Browser → Static File Server: load CSS, fonts, CSR bundles (/lib/csr/*.js)
• Browser CSR bundle → Hydrate SSR markup, connect to Svelte stores
• Hapi Server ↔ Datawrapper API: fetch chart/user data for rendering
• Hapi Server ↔ Lokalise API: update translations (development only)
Architectural patterns and design principles
• Separation of concerns: authentication, routing, view rendering, caching, localization
• Plugin architecture: Hapi plugins + event hooks for modular extensions
• SSR + CSR hydration for Svelte components with IE11 fallback detection
• Pre-compilation strategy to avoid runtime compile spikes
• Global state management via Svelte stores injected into view context
• Use of SCSS pipeline for styling, static asset hosting
Detail without overthinking
• Every major directory as a logical component
• Both the SSR path and CSR hydration path separately
• Event hook and plugin extension points explicitly
• Configuration entry points: server.js and plugin registration in src/utils/plugin-loader