---
url: /reference/items/Fetch/examples.md
---
# Examples
## Simple
Intercepting clicks on links, displaying a loader and updating the targets' content.
:::code-group
```ts
import { registerComponents } from '@studiometa/js-toolkit';
import { Action, Fetch, Transition } from '@studiometa/ui';
registerComponents(Action, Fetch, Transition);
```
:::
## Fetch from any element
A link or a form gives `Fetch` its destination. Any other element requests the endpoint of the [`src` option](./js-api.md#src) when [`fetch()`](./js-api.md#fetch-destination-string-url) is called. In the following example the panel is a `
`: it combines `Fetch` with the [`InViewOnce`](../InViewOnce/index.md) and [`Action`](../Action/index.md) components, so its content is loaded the first time it scrolls into view, with a bare `Fetch.fetch()` call.
:::code-group
```ts
import { registerComponents } from '@studiometa/js-toolkit';
import { Action, Fetch, InViewOnce } from '@studiometa/ui';
registerComponents(Action, Fetch, InViewOnce);
```
:::
## Form
In the following example, we intercept a form submission, display a loader and use a custom view transition to animate only the updated content once the request is finished.
:::code-group
```css
html.dark {
background-color: #222;
color: #eee;
}
body {
padding: 1rem;
}
@keyframes loader {
0% { transform: rotate(0) }
100% { transform: rotate(720deg) }
}
.animate-loader {
animation: loader 1s ease-in-out infinite;
}
#target strong {
view-transition-name: target;
}
::view-transition-old(target) {
animation: transition-out 0.3s ease-in-out;
}
::view-transition-new(target) {
animation: transition-in 0.3s ease-in-out;
}
@keyframes transition-out {
to {
opacity: 0;
transform: translateY(-100%);
}
}
@keyframes transition-in {
from {
opacity: 0;
transform: translateY(100%);
}
}
```
:::
## Pagination
A set of submit buttons is a paginator with no script of its own: each button sends its own `page` value, and the hidden `orderby` field travels with every submission. The [`params` option](./js-api.md#params) adds the template of the results to every request, so it never reaches the address bar. With the [`history` option](./js-api.md#history), each page is a history entry that back and forward restore. See [forms](./index.md#forms).
:::code-group
```ts
import { registerComponents } from '@studiometa/js-toolkit';
import { Fetch } from '@studiometa/ui';
registerComponents(Fetch);
```
:::
::: tip Back and forward
The preview runs in a frame with no address of its own, so `Fetch` writes no history entry there. On a page, the second button writes `?orderby=title&page=2`, and back requests page 1 again with the same `params`.
:::
## Modes
Modes are configured with the [`data-option-mode` attribute](./js-api.md#mode).
:::code-group
```ts
import { registerComponents } from '@studiometa/js-toolkit';
import { Fetch } from '@studiometa/ui';
registerComponents(Fetch);
```
:::
## Add to cart / Quickbuy
:::code-group
```ts
import { registerComponents } from '@studiometa/js-toolkit';
import { Action, Dialog, Fetch, Figure, Transition } from '@studiometa/ui';
registerComponents(Action, Dialog, Fetch, Figure, Transition);
```
:::
## Error handling
:::code-group
```ts
import { registerComponents } from '@studiometa/js-toolkit';
import { Action, DataBind, Fetch, Transition } from '@studiometa/ui';
registerComponents(Action, DataBind, Fetch, Transition);
```
:::
## Cancelling a request
Use the [`abort` method](./js-api.md#abort-reason-unknown) to cancel a request. In the following example, we use the [`Action` component](../Action/index.md) to cancel any pending request from any mounted `Fetch` component.
:::code-group
```ts
import { registerComponents } from '@studiometa/js-toolkit';
import { Action, DataBind, Fetch, Transition } from '@studiometa/ui';
registerComponents(Action, DataBind, Fetch, Transition);
```
:::
::: tip
In this example, we could use a more specific version of the [`data-option-target` attribute](../Action/js-api.md#target) of the `Action` component to target a single `Fetch` instance.
:::
## Shopify Section Rendering API
Shopify's [Section Rendering API](https://shopify.dev/docs/api/ajax/section-rendering) returns the rendered HTML of specific theme sections as a JSON object: each key is a requested section ID and each value is that section's HTML — or `null` when the section fails to render. Because every section is wrapped in a `
` element both on the page and inside the API response, the `Fetch` component can consume it **without any extra code**: parse the JSON with the [`response` option](./js-api.md#response) and let the default [`[id]` selector](./js-api.md#selector) swap each section in place.
::: tip
The [`FetchShopifySection`](../FetchShopifySection/index.md) component wraps this pattern: it declares the sections through a `sections` option (keeping them out of the `href` for a working no-JS fallback) and ships the JSON extraction below as its default, so you don't repeat it on every element.
:::
Request the sections to update with the comma-separated `sections` parameter (up to five). Set it with the [`params` option](./js-api.md#params), so the `href` stays a working link without JavaScript and the parameter never reaches the address bar:
:::code-group
```html [collection.liquid]
Sort by price
{% comment %} These wrappers, rendered by Shopify, are what Fetch swaps by id. {% endcomment %}
…
…
```
```json [GET ?sections=… response]
{
"main-collection-product-grid": "
…
",
"collection-results-count": "
…
"
}
```
:::
* `filter(Boolean)` drops any section returned as `null` (for example a section absent from the published theme) so the parser only receives valid HTML.
* The JSON keys are ignored: `Fetch` matches each section by the `id` of its `shopify-section-*` wrapper, so there is never a key/id mismatch.
* Keep the default `replace` [`mode`](./js-api.md#mode) (or use `morph`) so each section is swapped in place — `append` and `prepend` insert the new markup *inside* the existing `shopify-section-{id}` wrapper and would duplicate its content. This works together with the [`history`](./js-api.md#history) and [`viewTransition`](./js-api.md#viewtransition) options.
::: tip
Use a `