ArtisanPack UI
DocsPerformanceJavaScript API

JavaScript API

Performance v1.2.0

The package ships a client-side companion published under with multiple subpath exports so applications only pay for the pieces they use. Publish the...

The package ships a client-side companion published under @artisanpack-ui/performance with multiple subpath exports so applications only pay for the pieces they use. Publish the bundle with:

php artisan vendor:publish --tag=artisanpack-performance-js

Subpath exports

Import Purpose
@artisanpack-ui/performance Main entry — boots the RUM collector, cache client, and speculative-rules helpers
@artisanpack-ui/performance/web-vitals Standalone Web Vitals collector (LCP, CLS, FID, INP, TTFB, FCP)
@artisanpack-ui/performance/metrics-chart Chart.js bootstrap consumed by perf-metrics-chart
@artisanpack-ui/performance/speculative-rules Client helper to install a Speculation Rules document at runtime
@artisanpack-ui/performance/react React components + hooks for the dashboard surfaces
@artisanpack-ui/performance/vue Vue components + composables for the dashboard surfaces

Peer dependencies for React and Vue are declared optional — install only the framework you use.

Main entry

import { installPerformance } from '@artisanpack-ui/performance'

installPerformance({
    endpoint: '/api/performance/metrics',
    sampleRate: 100,
})

Wires up the Web Vitals collector, the cache client, and the Speculation Rules helper against sensible defaults. Every option is optional and falls back to config/artisanpack/performance.php values injected via a <meta> tag.

Web Vitals

import { collectWebVitals } from '@artisanpack-ui/performance/web-vitals'

collectWebVitals({
    endpoint: '/api/performance/metrics',
    onMetric: (metric) => console.log(metric),
})

Emits every Web Vital sample to endpoint as JSON ({ metric, value, url, session_id, context }) — matching the RawMetric row shape ingested by MetricsApiController.

Speculative Rules

import { installSpeculationRules } from '@artisanpack-ui/performance/speculative-rules'

installSpeculationRules({
    prefetch: [{ urls: ['/products', '/about'], eagerness: 'moderate' }],
    prerender: [{ urls: ['/checkout'], eagerness: 'conservative' }],
})

Same payload as @speculativeRules emits server-side; useful for SPA transitions that mutate the speculation set after the initial navigation.

AI features

The PerformanceClient returned by getPerformanceClient() gains two AI methods in 1.1.0. Both accept a plain input object and return the shaped agent output.

import { getPerformanceClient, PerformanceAiError } from '@artisanpack-ui/performance'

const client = getPerformanceClient({ baseUrl: '/api/performance' })

try {
    const insight = await client.suggestQueryInsight({
        query: 'SELECT * FROM articles WHERE slug = ?',
        explain: 'ALL, filesort',
        time_ms: 210,
    })

    const suggestion = await client.suggestOptimization({
        range: { from: '2026-07-01', to: '2026-07-07' },
        metrics: metricsBatch,
        context: { business_priority: 'checkout > blog' },
    })
} catch (error) {
    if (error instanceof PerformanceAiError) {
        console.warn(`AI feature ${error.featureKey} failed (${error.status}): ${error.message}`)
    } else {
        throw error
    }
}

PerformanceAiError extends Error with a status (HTTP status code) and featureKey (the feature key involved). Malformed 2xx bodies — empty responses, non-JSON payloads, or responses missing a data key — are converted into PerformanceAiErrors too, so callers never see an uncaught TypeError from a downstream .data deref.

See api/ai for the full input / output schemas and guides/ai-features for a walkthrough.

React

import {
    PerformanceDashboard,
    MetricsChart,
    QueryInsightPanel,
    OptimizationSuggestionPanel,
    useWebVitals,
} from '@artisanpack-ui/performance/react'

function Vitals() {
    const vitals = useWebVitals()
    return <MetricsChart metric="LCP" data={vitals.LCP} />
}

Dashboard components read from the same admin API endpoints (/api/performance/admin/*) that the Livewire components use.

Vue

<script setup>
import {
    PerformanceDashboard,
    MetricsChart,
    QueryInsightPanel,
    OptimizationSuggestionPanel,
    useWebVitals,
} from '@artisanpack-ui/performance/vue'

const vitals = useWebVitals()
</script>

<template>
    <MetricsChart metric="LCP" :data="vitals.LCP" />
</template>

The composable returns reactive refs so templates re-render as new samples arrive.

REST endpoints

The React/Vue components wrap these endpoints — useful if you're building your own UI:

Method + path Purpose
POST /api/performance/metrics Ingest a Web Vitals sample
GET /api/performance/admin/dashboard Dashboard summary payload
GET /api/performance/admin/chart Time-series chart data for a metric
GET /api/performance/admin/queries Slow-query and N+1 rows
GET /api/performance/admin/cache Cache statistics
POST /api/performance/ai/query-insight Run the performance.query_insight agent
POST /api/performance/ai/optimization-suggestion Run the performance.optimization_suggestion agent
POST /api/performance/admin/cache Cache mutation actions (purge, warm)
GET /api/performance/admin/recommendations Ranked recommendations

Every admin endpoint requires the same authorization your Livewire dashboard route uses — the package does not register a gate itself.

Stay in the Loop

Monthly tips, tutorials and package updates — plus a free Quick Start cheat sheet.

Subscribe
Edit this page on GitHub