Marketing
Storefront sitemaps for GoCommerce
A Go package that publishes the storefront’s sitemap from the live catalogue — every active product and collection, and every published page when the cms module is installed. It needs one setting: where the storefront lives.
- Marketing module
- 4 settings
- 4 tests
- 5 API operations
- ext/sitemaps
What it does
The module serves a sitemap index at /x/sitemaps/sitemap.xml and the files it points at: products.xml, collections.xml and, with the cms module installed, pages.xml. Every URL carries the day the thing it names last changed.
The engine does not know where its storefront lives, so, as the package doc puts it, the address is a setting — and so are the paths, because storefronts differ on whether a product lives at /products/{slug} or /p/{slug}. A storefront serves /sitemap.xml by proxying or redirecting to the index.
The files are generated on request from the live catalogue and cacheable for an hour. Each carries a stylesheet instruction, so a person who opens one in a browser sees a table of what is listed rather than raw XML; crawlers ignore it.
Configuration
One setting is required: the storefront’s URL. With it in Config the module starts switched on; otherwise switch it on and fill it in on the Plugins screen, which wins where both say something.
| Setting | Environment variable | Required | What it does |
|---|---|---|---|
StorefrontURLStorefront URL | STOREFRONT_URL | Yes | Where the pages live, e.g. https://shop.example. Until it is set the sitemap answers 409 and says so; while the plugin is off, 404. |
ProductPathProduct page path | — | No | The product page’s path with {slug} in it. Defaults to /products/{slug}; set it on the Plugins screen — see the limits. |
CollectionPathCollection page path | — | No | The collection page’s path. Defaults to /collections/{slug}. |
PagePathContent page path | — | No | The content page’s path, used when the cms module is installed. Defaults to /pages/{slug}. |
The module reads its Config struct, not the environment. The variable names are the ones the package’s own example or the reference binary uses; in your own main() you choose where each value comes from. Where a setting has a panel label, the admin’s settings drawer can hold it too, and a value typed there wins over Config.
Setting it up
- Install the moduleImport it and pass sitemaps.New to gocommerce.New, as below — or run the reference binary with -sitemaps, which reads STOREFRONT_URL.
- Set the storefront and pathsThe storefront URL, and the product, collection and page paths if your storefront’s differ from the defaults, on the Plugins screen.
- Point /sitemap.xml at itHave the storefront proxy or redirect /sitemap.xml to /x/sitemaps/sitemap.xml — and /x/sitemaps/ too, since the index links to the files there.
- Check it, then submit itOpen the index in a browser: the stylesheet shows what is listed. Then submit the storefront’s /sitemap.xml to the search engines.
main.go
import (
"os"
"github.com/itswadesh/gocommerce/core"
"github.com/itswadesh/gocommerce/ext/sitemaps"
)
app, err := gocommerce.New(cfg,
sitemaps.New(sitemaps.Config{StorefrontURL: os.Getenv("STOREFRONT_URL")}),
) As the repository’s examples/store/main.go wires it, with its imports; cfg is your gocommerce.Config. The package doc itself has no example. Import path github.com/itswadesh/gocommerce/ext/sitemaps.
How it works
-
An index and three files
sitemap.xml lists products.xml and collections.xml, and pages.xml when the cms module is installed. All five routes are public and documented in the module’s OpenAPI file.
-
Live, not stored
Each file is built on request from active products, active collections and published pages, read 500 at a time, and sent with Cache-Control: public, max-age=3600.
-
Last changed, by the day
Every URL carries lastmod — the day it last changed, in UTC — and a change frequency: weekly for products and collections, monthly for content pages.
-
Readable in a browser
Each file points at a stylesheet the module serves beside it, so the first click after switching it on answers whether it worked.
-
Links from the request’s host
The index’s links use the host the request came to, honouring X-Forwarded-Host and X-Forwarded-Proto, so behind a proxy they carry the storefront’s address.
-
Off means 404
Switched off, every file is a 404. Switched on without a storefront URL, a 409 that names the setting to fill in.
What it does not do
Read these before an order depends on it. No store is known to run GoCommerce in production yet, so these come from the code, not from anyone’s experience.
- 50,000 URLs per fileThe package doc names it: a sitemap file holds 50,000 URLs, and a catalogue past that needs the products file split, which this does not do yet.
- Paths in Config are shadowedThe panel’s path fields have defaults, read before Config, so ProductPath, CollectionPath and PagePath in Config never take effect. Set the paths on the Plugins screen.
- No categoriesProducts, collections and content pages only. Category pages are not listed.
- No images or languagesEach entry is a location, a date and a frequency: no image entries, and no alternates for other languages.
- The proxy is yoursThe files are served by GoCommerce under /x/sitemaps/. The storefront has to proxy or redirect its /sitemap.xml; the module cannot answer at the storefront’s address.
- No change pingsIt tells no search engine that something changed. That is the indexnow module’s job.
FAQ
Questions about the Sitemaps module
Why is the sitemap served by the API and not the storefront?
Because the catalogue lives there. The engine builds the files from live data; the storefront serves /sitemap.xml by proxying or redirecting to /x/sitemaps/sitemap.xml, as the package doc describes.
My product pages are at /p/{slug}. What do I change?
The product page path, on the Plugins screen. Set it there rather than in Config: the panel’s default of /products/{slug} is read first, so a path in Config is never used.
Does it list content pages?
When the cms module is installed. The index then includes pages.xml, with every published page; without the cms module, pages.xml answers 404 and the index leaves it out.
How fresh is it?
As fresh as the catalogue: every file is built on request, and marked cacheable for an hour.
Does it need Svelte Commerce?
No. Any storefront that can proxy or redirect /sitemap.xml can use it; the paths are settings precisely because storefronts differ.
Source
Everything on this page is read from ext/sitemaps in the GoCommerce repository, MIT licensed. When this page and the code disagree, the code is right and this page is out of date.
Try it against a store of your own
The one-command stack gives you GoCommerce’s API and admin on your own machine in minutes. Add this module to it and try it on test orders before a real one depends on it.