Run app code on a page
Run app or snippet code on every page of a kind with the SDK event page::view, so it works after client-side navigation.
Use this when a store app or a snippet adds something to one kind of page, such as a widget on every product page.
Goal: the code runs on each product page the customer opens, in a React theme and a classic theme alike.
On a classic theme every page is a full load, so a snippet can check the page once:
// Runs once. In the React theme that is only the page the customer landed on.
if (Salla.url.is_page('product.single')) mountWidget();In the React theme, links navigate in the browser without reloading. That snippet runs once, on the first page, and the engine renders later pages fresh. A widget it added is replaced, and a product page the customer opens later never gets one.
Listen for page::view
The Twilight SDK dispatches page::view with the page the customer is looking at: once when it has started, then after every navigation in the browser (it follows the engine's route::changed). A classic theme loads each page, so there it fires once per page too. The same code works in both:
Salla.event.on('page::view', (page) => {
if (page.slug === 'product.single') mountWidget(page.id);
else removeWidget();
});The handler gets the page: slug, id, title, url, and breadcrumbs when the page has them. A 404 sends an empty object, so check slug.
- It is sticky. A handler added after the first
page::view, such as a snippet that loads late, is called straight away with the page on screen, then on every later one. - Leaving is yours to handle. There is no separate event for it: the next
page::viewis the signal, asremoveWidget()above shows. - Opening another product is a new page view. Changing filters or sorting on the same page is not.
Move a check that runs once
A check that runs once, at load or inside Salla.onReady, runs on the page the customer landed on and never again. The React theme does not reload the page for it. Guards that return early behave the same way:
// Runs once. Landing on the home page and opening the cart never runs it.
Salla.onReady(() => {
if (!Salla.url.is_page('cart')) return;
runMyCartCode();
});Put the same check in a page::view handler. It runs on every cart page the customer opens, in both kinds of theme:
Salla.event.on('page::view', (page) => {
if (page.slug !== 'cart') return;
runMyCartCode();
});The same goes for code that reads the address (window.location.pathname, location.href, document.URL) or the page config (Salla.config.get('page.slug')) once to decide where it is: read page.slug and page.id from the handler instead.
Where to go next
- Salla SDK and events, for
route::changed, the eventpage::viewfollows. - Add content to a slot, for a place in the page that survives navigation.