|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <title>ItemsJS Snapshot Smoke Test</title> |
| 6 | + <style> |
| 7 | + body { font-family: sans-serif; padding: 16px; } |
| 8 | + pre { background: #f6f8fa; padding: 12px; overflow: auto; } |
| 9 | + </style> |
| 10 | +</head> |
| 11 | + <body> |
| 12 | + <h1>ItemsJS Snapshot Smoke Test</h1> |
| 13 | + <p style="max-width: 640px;"> |
| 14 | + A green message below means the snapshot was created and stored in localStorage (first load) |
| 15 | + or successfully loaded from it (subsequent refreshes). If it stays on “Loading…” or you see a |
| 16 | + red message, check the browser console for errors. |
| 17 | + </p> |
| 18 | + <p id="status">Loading…</p> |
| 19 | + <div id="msg" style="margin: 8px 0; font-weight: bold;"></div> |
| 20 | + <pre id="output"></pre> |
| 21 | + |
| 22 | + <script type="module"> |
| 23 | + import itemsjs from '../dist/index.module.js'; |
| 24 | + |
| 25 | + const config = { |
| 26 | + searchableFields: ['name', 'category', 'actors', 'name'], |
| 27 | + aggregations: { |
| 28 | + tags: { title: 'Tags', conjunction: true }, |
| 29 | + actors: { title: 'Actors', conjunction: true }, |
| 30 | + year: { title: 'Year', conjunction: true }, |
| 31 | + in_cinema: { title: 'Is played in Cinema', conjunction: true }, |
| 32 | + category: { title: 'Category', conjunction: true }, |
| 33 | + }, |
| 34 | + }; |
| 35 | + |
| 36 | + const log = (obj) => { |
| 37 | + document.getElementById('output').textContent += `${obj}\n`; |
| 38 | + }; |
| 39 | + |
| 40 | + const setStatus = (msg) => { |
| 41 | + document.getElementById('status').textContent = msg; |
| 42 | + }; |
| 43 | + |
| 44 | + const setMsg = (msg, ok = true) => { |
| 45 | + const el = document.getElementById('msg'); |
| 46 | + el.style.color = ok ? 'green' : 'red'; |
| 47 | + el.textContent = msg; |
| 48 | + }; |
| 49 | + |
| 50 | + async function loadData() { |
| 51 | + const res = await fetch('../tests/fixtures/items.json'); |
| 52 | + if (!res.ok) throw new Error(`Failed to load data: ${res.status}`); |
| 53 | + return res.json(); |
| 54 | + } |
| 55 | + |
| 56 | + async function run() { |
| 57 | + try { |
| 58 | + const data = await loadData(); |
| 59 | + |
| 60 | + // Try to load snapshot from localStorage if present |
| 61 | + const stored = localStorage.getItem('itemsjs-snapshot'); |
| 62 | + const snap = stored ? JSON.parse(stored) : null; |
| 63 | + |
| 64 | + const engine = itemsjs(data, { |
| 65 | + ...config, |
| 66 | + fulltextSnapshot: snap?.fulltext, |
| 67 | + facetsSnapshot: snap?.facets, |
| 68 | + }); |
| 69 | + |
| 70 | + if (!snap) { |
| 71 | + setStatus('Built fresh, creating snapshot…'); |
| 72 | + const newSnap = engine.serializeAll(); |
| 73 | + localStorage.setItem('itemsjs-snapshot', JSON.stringify(newSnap)); |
| 74 | + setMsg('Snapshot created and stored in localStorage.', true); |
| 75 | + } else { |
| 76 | + setStatus('Loaded from snapshot'); |
| 77 | + setMsg('Loaded from snapshot in localStorage.', true); |
| 78 | + } |
| 79 | + |
| 80 | + const result = engine.search({ |
| 81 | + query: 'comedy', |
| 82 | + filters: { tags: ['a'] }, |
| 83 | + }); |
| 84 | + |
| 85 | + log(JSON.stringify({ |
| 86 | + status: document.getElementById('status').textContent, |
| 87 | + message: document.getElementById('msg').textContent, |
| 88 | + total: result.pagination.total, |
| 89 | + items: result.data.items.map((v) => v.name), |
| 90 | + aggregations: result.data.aggregations.tags.buckets.slice(0, 3), |
| 91 | + }, null, 2)); |
| 92 | + } catch (e) { |
| 93 | + setStatus('Error'); |
| 94 | + setMsg(e.message || 'Error', false); |
| 95 | + log(e); |
| 96 | + console.error(e); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + run(); |
| 101 | + </script> |
| 102 | +</body> |
| 103 | +</html> |
0 commit comments