StaffCoder
TypeScript
Vanilla TypeScriptTypeScript 7.0
6 modules · 24 challengesSign up
Module 5 · Modules & Code Organisation

Shared Storefront Bootstrap

easyIdempotent init functions with module-scope guards—No data yet

Problem statement

A small storefront co-locates a home view and a product view on the same page. Two entry modules and one shared bootstrap module together turn the empty page shell into the finished storefront. Nothing happens at module import time. The shared setup runs exactly once even when both entries request it, each entry sets up only its own view, and calling an entry's setup a second time does not stack duplicate click listeners, using Vanilla TypeScript and standard DOM APIs.

Demo

The page, styles, seeded data, and DOM helpers are already scaffolded and wired. Only the three modules in src/js/ — bootstrap.ts, homeEntry.ts, and productEntry.ts — are missing. Implement them so every test passes. Do not edit any other file.

What to implement

  • Do not do any page work when a module is first loaded. All work happens when the page calls the entry's setup function.
  • Show the site header (brand name and tagline) exactly once, no matter how many entries ask for it to be set up.
  • Show the home view (featured line and Explore button) when the home entry's setup runs. Do not show anything from the product view.
  • Show the product view (name, price, and Add to cart button) when the product entry's setup runs. Do not show anything from the home view.
  • Each time the Explore button is clicked, increment the Explored count on the page by exactly one. Each time the Add to cart button is clicked, increment the In cart count on the page by exactly one.
  • Running an entry's setup a second time must not add a second click listener; one click still moves each counter by exactly one.
  • Everything shown must come from the seeded data. Nothing should be hardcoded.

More in Modules & Code Organisation

See the module
Storefront Utilities PackageSplit a monolithic entry into three named-export modules — format (formatPrice, formatSavingsPercent), validate (isValidEmail), and dom (setText, setDisabled) — and wire them from a thin entry inside a single DOMContentLoaded handler. No file may assign to window, importing any helper in isolation must not touch the document, and the helper import graph must have no cycles.—No data yeteasyStorefront Config Module Write config.ts — DEFAULTS, UnknownConfigKeyError, getConfig, and the generically-typed getConfigValue<K>(key) — as the single seam between a raw window.__STOREFRONT_CONFIG__ object and every consumer. Defaults document absent keys, injected values (including false and "") win over defaults, the returned object is frozen, unknown keys throw the named error, and no consumer file references the raw global.—No data yeteasyAnalytics Chart LoaderImplement createChartLoader(importFn) and the default loadChart so a heavy chart module is only fetched via import('./chart') when the user clicks Load. The loader dedupes concurrent calls to a single in-flight promise, caches the resolved module for every future call, and does not cache failures so Retry starts clean. The entry never references ./chart statically.—No data yeteasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.