Registering an Extension
An extension's Nuxt module registers with CWA in the cwa:extend hook. It calls register() once, with a name, a prefix and what it adds.
The Module File
// cwa-shop/module.ts
import { createResolver, defineNuxtModule } from '@nuxt/kit'
export default defineNuxtModule({
meta: { name: '@acme/cwa-shop' },
setup(_options, nuxt) {
const { resolve } = createResolver(import.meta.url)
nuxt.hook('cwa:extend', (register) => { register({ name: '@acme/cwa-shop', prefix: 'Shop', dir: resolve('./cwa'), adminPages: [
{ path: 'shop-orders', file: resolve('./admin/orders.vue'), item: resolve('./admin/order.vue'), menu: { label: 'Orders' } },
],
pageCache: { '/basket': false, '/account/**': false },
}) }) },
})
CWA calls the hook once every module has set up, so the extension can be listed before or after @cwa/nuxt in the site's modules.
| Option | Required | What it does |
|---|---|---|
name | Yes | The extension's name, used in build messages. Usually the package name |
prefix | Yes | The PascalCase prefix every component, layout and page template name starts with. See The Prefix |
dir | No | An absolute path to a folder laid out like app/cwa/. See The cwa/ Folder |
adminPages | No | Pages added under /_cwa. See Admin Pages |
pageCache | No | Paths kept out of the shared page cache. See Paths the Page Cache Skips |
An extension only adds. It can't remove or change CWA's own pages, components or settings.
The Prefix
Every name in the extension's cwa/ folder starts with its prefix: a folder ShopProductCard/, a layout ShopCheckout.vue, a page template ShopProduct.vue. The prefix stops two extensions, or an extension and a site, from using the same name by accident.
Cwa. Every name must start with it and add something after it: ShopProductCard is fine, ProductCard and Shop aren't. Two extensions can't share a prefix.The prefix only applies to the Vue files. The API's component classes need the same names, prefix included, because a component's PHP class, Vue folder and display file are one name. See Architecture › Naming Conventions.
The cwa/ Folder
dir holds up to three folders, the same as a site's app/app/cwa/:
| Folder | Holds | Registered as |
|---|---|---|
components/ | One folder per component: <Name>/<Name>.vue, with admin/ tabs and ui/ variants | CwaComponent<Name> |
layouts/ | <Name>.vue layouts | CwaLayout<Name> |
pages/ | <Name>.vue page templates | CwaPage<Name> |
Only the folders that exist are read, so an extension with only components needs only components/. Each file works as it would in a site: Component Files, Layout Files and Page Template Files describe them, defineCwaConfig included.
A component from an extension is a component like any other: editors add it from the Add dialog, and CwaComponentNames.ShopProductCard names it in a group's allowed-components.
Replacing an Extension's File
A site replaces an extension's component, layout or page template by adding one with the same name to its own app/app/cwa/. The site's file wins, and the build logs which file replaced which:
@cwa/nuxt: app/cwa/components/ShopProductCard replaces ShopProductCard from @acme/cwa-shop
The site's folder replaces the extension's whole folder for that name. Its manager tabs come only from the site's admin/ folder and its UI variants only from the site's ui/, so copy any of the extension's tabs or variants you still want.
Paths the Page Cache Skips
The module lets a shared cache store rendered pages (see Page Caching). A page that shows something different to each visitor, such as a basket, mustn't be stored, because the next visitor would get it. pageCache lists those paths:
pageCache: { '/basket': false, '/account/**': false },
Each entry becomes the route rule cwa: { pageCache: false }, and a page under it is sent with Cache-Control: private, no-store and no Surrogate-Key. /account/** covers /account itself as well as every path below it.
A site can set the same route rule in its own routeRules, for a page of its own:
// app/nuxt.config.ts
export default defineNuxtConfig({
routeRules: {
'/my-orders': { cwa: { pageCache: false } }, },
})
When rules overlap:
- A site rule that sets
cwa.pageCachefor the same path wins over the extension's. Other keys in a site rule for that path are kept, and the extension'spageCache: falseis added beside them. A site that sets'/account/**': { cwa: { pageCache: true } }keeps its account pages cacheable. - For different paths, the most specific matching rule wins, as for any Nuxt route rule. A site rule for
/account/helpoverrides the extension's/account/**.
false is the only value an extension can give.
When the Build Fails
CWA checks every extension when the site builds and stops the build on the first problem. Each message names the extension and the file:
| Problem | Message |
|---|---|
| The same extension registered twice | @acme/cwa-shop is registered twice |
| A prefix that isn't PascalCase | @acme/cwa-shop's prefix "shop" must be PascalCase letters and digits |
A prefix starting with Cwa | @acme/cwa-shop's prefix "CwaShop" must not start with Cwa |
| Two extensions with one prefix | @acme/cwa-shop and @other/shop both use the prefix "Shop" |
| A name without the prefix | /…/cwa/components/Price from @acme/cwa-shop must be named Shop<Name>, as ShopPrice |
| Two extensions defining one name | ShopPlusBadge is defined by both /…/components/ShopPlusBadge (@acme/cwa-shop) and /…/components/ShopPlusBadge (@acme/cwa-shop-plus) |
| An admin page on a built-in path | @acme/cwa-shop's admin page /_cwa/users is a built-in admin page |
| Two extensions with one admin path | /_cwa/shop-orders is registered by both @acme/cwa-shop and @acme/store |
| An admin page role that isn't allowed | @acme/cwa-shop's admin page /_cwa/shop-orders declares the role "admin". Use a ROLE_ name other than ROLE_USER, or leave it out for ROLE_ADMIN. |
| Admin pages without the CWA layer | @acme/cwa-shop's admin pages need the /_cwa page from @cwa/nuxt/layer |
A site file with the same name as an extension's isn't an error: it replaces it (see Replacing an Extension's File).
Next: Admin Pages.