# Storefront

The storefront is the `Storefront` module (`packages/core/src/Modules/Storefront`): controllers, the theme's views in
`packages/theme-default/resources/views/store`, and the SCSS/JS in `packages/theme-default/resources/scss/store` and `packages/theme-default/resources/js/store`. It owns no catalogue data;
it reads everything through contracts:

| Need | Contract |
|---|---|
| Category menu, category/brand/product pages, product lists | `Catalog\Contracts\CatalogBrowser` (data: `CategoryPage`, `ProductPage`, `ProductCard`, …) |
| Search results | `Catalog\Contracts\ProductSearch`, then `CatalogBrowser::cards()` in the engine's order |
| Prices, Omnibus reference price | `Pricing\Contracts\Prices` (`PriceContext::retail()` for now) |
| Stock online and per location | `Inventory\Contracts\Stock` |
| Store locator and store pages | `Stores\Contracts\Locations::storefront()` / `storeBySlug()` (`StoreDetails`) |

## Languages and routes

`Routes/web.php` registers every page once per language in `syncshop.storefront.locales`: Romanian at the bare URL,
the others under `/{locale}`, with the path words from `packages/core/lang/{locale}/store.php › paths`. Route names are
`store.{locale}.{page}` (`store.en.product`). The `StoreLocale` middleware sets the locale and answers 404 for a
language that is switched off in `StorefrontSettings::$locales`. In views, `$shop` (`Support\StoreContext`) builds links
in the current language: `$shop->url('product', ['slug' => $slug])`.

Slugs are looked up in the page's language, then in Romanian; when the request used another slug than the language's
own, the controller redirects 301. Each page gets a `Support\Seo` (title, description, canonical, hreflang alternates,
`index`), rendered by `<x-layouts.store>`. Filtered/sorted lists and search are `noindex, follow`; paginated pages are
their own canonical.

**Attribute filters.** `CatalogBrowser::filterAttributes()` gives a category's filters: filterable attributes linked
(`attribute_category.is_filter`) to the category or an ancestor, as choice (select, multiselect, variant axes), range
(number) or flag (boolean), choices limited to values in use (`Catalog\Storefront\Facets`). `Support\Filters` reads
them from the query string (one parameter per attribute code: `prajire=medie,inchisa`, `putere=10-20` or the range
form's `putere-min`/`putere-max`, `dimabil=da`; codes clashing with the list's own parameters get an `a-` prefix),
and `ProductListQuery::$filters` carries them. A choice matches a specification or an active variant's option.
`facetCounts()` counts each option with every other filter applied but not its own attribute, so options of one
attribute combine (OR) while attributes narrow (AND). The links are plain, `nofollow`, and keep every other choice;
filtered lists are `noindex`. They are links and a GET form rather than a Livewire component: they work without
JavaScript, are crawl-safe, and add no script weight to the page-speed budget.

**Filter pages.** `Models\FilterPage` (table `filter_pages`, owned by the storefront, catalog ids only) whitelists a
category + brand pair: `/categorie/{category}/{brand}` (route `category.brand`) is then indexable with its own
title, intro and canonical; `?brand=` for that single brand redirects there, a pair without a filter page redirects to
`?brand=`. The listing partial links such brands to the clean URL without `nofollow`; filter pages appear in the
pages sitemap. Admin: Settings → SEO › Pagini de filtre (`FilterPageController`, choices from
`CatalogBrowser::categoryOptions()` and `brandsIn()`).

**Redirects.** `Models\Redirect` (table `redirects`, unique on `lower(source)`) with `Redirects\Redirects` for
normalising (leading slash, no trailing slash, sorted query), saving (targets followed to the end, existing
redirects to the new source moved to its target, loops and reserved paths refused) and finding (exact address, then
the path alone). `Http\Middleware\RedirectMissingPages` is pushed onto the global middleware: it acts only on a GET
that would answer 404, so live pages always win; 410 renders `store.gone`. Admin: Settings → SEO › Redirecționări
(`RedirectController`, CSV import up to 50,000 rows).

**Structured data.** `Seo\StructuredData` is the only place JSON-LD is made; controllers pass its nodes in
`Seo::$schema` and the layout prints one `@graph` (encoded so text can never close the `<script>`). Product (one
variant) or ProductGroup with `hasVariant` (an Offer per variant: price with VAT, currency, availability, condition,
the variant URL), BreadcrumbList, ItemList on category and brand pages, Organization and WebSite with a SearchAction on
the home page, Store/LocalBusiness with address, coordinates and opening hours on store pages. A property is set only
when its value is real; shipping details, return policy and ratings join when those features exist. Fixture tests:
`tests/Feature/Storefront/StructuredDataTest.php`.

**Changed slugs and retired products.** Products, categories and brands record the slugs they leave behind in
`slug_history` (`Catalog\Support\SlugHistory`, from the models' `updating` event). `CatalogBrowser` finds an item by an
old slug too, and the controllers redirect 301 whenever the slug asked for is not the current one. A slug given back
to its item stops being history. An archived product redirects 301 to its primary category, or answers 410 Gone
when it has none (`CatalogBrowser::discontinued()`).

## Theme

`store.scss` configures Bootstrap 5.3 (only the parts in use, listed in `_bootstrap.scss`: reboot, type, containers,
form controls, buttons, pagination, visually-hidden, and three utility groups; about 10 KB gzipped) with the neutral
tokens (Slate greys, status colours, focus ring, radii), then
`_store-theme.scss` points the components at the merchant layer, `--store-*` custom properties (DESIGN.md §9):
primary, primary-strong, on-primary, primary-text, accent, on-accent, heading/body font and radii. A merchant theme
only overrides those properties; neutrals, status colours, focus and spacing are fixed. The components (`_store-layout`,
`_store-catalog`, `_store-stores`) use `store-` BEM classes and never hard-code a merchant colour.

**Page speed.** Keep the CSS budget: a component that needs another Bootstrap part imports it in `_bootstrap.scss`,
never the whole framework. In built mode the layout inlines the whole stylesheet (`Vite::content()`, about 10 KB
compressed), so nothing blocks the first render; with the Vite dev server running it links it as usual. The only
bundled font is Inter (`_store-fonts.scss`), with an "Inter Fallback" (Arial resized to Inter's metrics) so the
swap moves nothing; codes use the device's monospace font. The product image has `fetchpriority="high"` and a
`srcset`, every image has width and height, list images load lazily, and `store.js` is a deferred module of a few
hundred bytes. `composer lighthouse` checks the budget ([Quality checks](quality-checks.md)).

The merchant's values come from `Settings\ThemeSettings` (Settings → Aspect). `Theme\Palette::derive()` turns the
primary and accent into the contrast-safe colours of DESIGN.md §9 (OKLCH lightness steps in `Theme\Colour`), and
`Theme\StoreTheme::css()` prints them (the neutral theme keeps its own link blue, `#2350C4`, while its primary is the
default near-black), the font stacks (`Theme\StoreFont`: bundled or system fonts only) and the radii
(`Theme\Corners`) as a `:root{…}` block after the stylesheet. Only checked hex values and fixed stacks reach that
block. The logo lives on the public media disk (bucket or server, per Settings → Storage), raster formats only.

Everything works without JavaScript: the category menu and the filters are `<details>`, brand filters are links, the
sort is a GET form, gallery thumbnails link to the large image. `store.js` only adds comfort (opening the menu on wide
screens, auto-submitting the sort, swapping gallery images).

Prices on cards come from `Support\Shelf`, which also fetches the Omnibus lowest price for reduced items: a discount
badge and a struck-through price compare only with that, never with the list price.

## For crawlers

`CrawlerController` serves `robots.txt` (`Seo\Robots`), `sitemap.xml` and `sitemaps/{kind}-{locale}-{page}.xml`
(`Seo\Sitemaps`, cached an hour per file) and `llms.txt` (`Seo\LlmsTxt`). The AI bot list is
`syncshop.seo.bots` (search and training groups), so it can change without a release; `SeoSettings` switches each
group. Every named group repeats the always-closed paths, because a crawler that matches a group ignores `*`.
Sitemap files hold `syncshop.seo.sitemap_size` URLs (10,000), with `lastmod` and product images, from
`CatalogBrowser::sitemap()` and `Locations::storefront()`. Only files the index lists are generated and cached, and
`trustHosts()` (bootstrap/app.php) refuses requests for hosts other than APP_URL's in production, so a forged Host
header can never end up in cached URLs.

**IndexNow.** Products, categories and brands raise `Catalog\Data\CatalogPageChanged` when saved or deleted;
`Listeners\QueueIndexNow` notes them in `indexnow_pending` while IndexNow is on (`SeoSettings::$indexnow`). The
`seo:indexnow` task (every 5 minutes) resolves their slugs (`CatalogBrowser::slugsOf()`: public items and ones that were public, never drafts),
builds the URL in each switched-on language and posts them to `syncshop.seo.indexnow_endpoint` with the key served at
`/{key}.txt`. It sends only when `app()->isProduction()`; elsewhere it empties the queue. A refused ping fails the
run and keeps the rows for the next one.

**Markdown versions.** Product and category pages answer as Markdown at their URL + `.md`, or when the request prefers
`text/markdown` (`Support\Markdown`, templates in `packages/theme-default/resources/views/store/markdown`). They carry the same facts as the
HTML (price with VAT, Omnibus, codes, variants, stock per store, specifications, GPSR), a `Link: rel="canonical"`
header to the HTML page, and the HTML pages point to them with `<link rel="alternate" type="text/markdown">`.
llms.txt links to them.

Tests: `tests/Feature/Storefront/` (`StorefrontTest`, `StructuredDataTest`, `ThemeTest`, `CrawlersTest`).
