Feedback

Fetchable

Requests a URL from the browser and shows what it answers: as formatted JSON, as a chart, or through markup of your own. A spinner shows while the request runs and an error message if it fails. Use it for data that should not delay the page, such as a report or a preview loaded after the first render.

Blade

A spinner first; when the request answers, the JSON response is shown formatted and colored, as tk:pretty-print-json shows it. A response with an error status, or a network failure, shows a red message with the reason (Not Found, for instance) in its place.

Your own markup #

The slot is shown once the response arrives, with data holding the parsed JSON for Alpine expressions inside it. It is not rendered before that, so data is always there when the markup reads it. Wrap the slot in one element: only its first root element is rendered.

Blade

A chart from an endpoint #

The response is the chart's options in the library's own format, drawn by tk:chart with that library: apexcharts, chartjs, echarts or frappe-charts. Attributes prefixed with chart: go to the chart.

Blade

Loading on demand #

With :auto="false" nothing is requested, and "Nothing to show" is displayed, until fetch() is called. Inside the tag, fetch(), reload() and update() can be called directly from Alpine expressions, as in the next example.

Blade

Reloading #

update() requests the URL again and swaps the content when the answer arrives, keeping the current one on screen meanwhile. reload() does the same but shows the spinner while waiting. fetch(url, options) requests another URL, which becomes the one the next calls use. The three return a promise that settles when the answer is read (await reload()); a request that fails, an invalid URL included, shows the error state.

Blade

Data without a request #

Without a url, data is shown as it is, here a Livewire property.

Blade

Error state #

A response with an error status, or a network failure, shows a red message with the reason in place of the content.

Blade

Silent #

silent loads without the spinner and without the started event; the previous content stays until the new one is ready.

Blade

Events #

started, completed and failed fire on the tag. Here completed stores the time in Alpine and failed calls a Livewire action.

Last check:
Outages reported: 0
Blade

Customization #

Classes go to the wrapper; prefixed attributes (loading:, json:, error:, empty:, chart:) reach the parts inside.

Blade

Reference

tk:fetchable

Prop Type Default Description
url string null The URL to request.
auto bool true Requests the URL as soon as the page loads. false waits for fetch().
options array [] Options for fetch(): method, headers, body... plus responseType, the method used to read the response (json by default, text, blob...; empty for the raw Response).
data mixed null Data to show without a request, when there is no url.
chart string null Draws the response as a chart with this library: apexcharts, chartjs, echarts or frappe-charts.
silent bool false Loads without the spinner and without the started event. Until the first answer arrives, "Nothing to show" stays on screen.

Slots

Slot Description
default Shown when the response arrives, in place of the JSON or the chart. data holds the response. Only its first root element is rendered.

Events

Event Detail When
started A request started, except with update() or silent.
completed The response arrived and was read.
failed The request failed, or answered with an error status.

Prefixes

Prefix Goes to
loading: The spinner.
error: The error message.
empty: The "Nothing to show" tk:empty, with :auto="false".
json: The formatted JSON.
chart: The chart, with chart.

Events

Events are dispatched on the tk:fetchable element and do not bubble: listen on the tag itself.

With Alpine:

blade

Calling a Livewire action:

blade

Customization

Classes and attributes on the tag go to its wrapper <div>. To reach the parts inside, prefix the attribute (listed in the Reference above).

blade

Accessibility

The spinner and the error message are role="status" live regions, so the start and the failure of the request are announced. What the slot renders is your own markup.

Behavior notes

  • Requests other than GET, HEAD and OPTIONS to the same origin carry the CSRF token (X-XSRF-TOKEN from the cookie, or X-CSRF-TOKEN from the csrf-token meta tag), so a POST to a Laravel route works without extra setup.
  • A new request cancels the one still running, so only the last answer is shown.
  • headers given to fetch() are merged with the ones from options; the other options replace them for this and the next requests.
  • The wrapper has wire:ignore: a Livewire update does not touch what was loaded.