Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 36x 1x 1x 36x 39x 39x 39x 39x 34x 34x 1x 1x 34x 2x 2x 31x 31x 31x 31x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 34x 1x 1x 29x 34x 2x 2x 2x 34x 39x 39x 39x 39x 14x 13x 14x 13x 13x 13x 13x 13x 14x 39x 39x 39x 39x 16x 16x 3x 3x 13x 16x 13x 13x 13x 13x 12x 16x 1x 16x 13x 13x 13x 3x 3x 13x 16x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 39x 39x 39x 39x 18x 18x 39x 39x 39x 39x 6x 6x 39x 39x 39x 39x 20x 20x 20x 20x 20x 6x 6x 20x 20x 20x 20x 20x 18x 18x 18x 18x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 2x 2x 2x 20x 39x 39x 39x 39x 1x 1x 1x 1x 1x 1x 1x 39x 39x 39x 39x 1x 1x 1x 1x 1x 39x 39x 39x 39x 39x 39x 39x 39x 2x 2x 39x 39x 39x 39x 4x 4x 39x 39x 39x 39x 1x 1x 39x 39x 39x 39x 5x 5x 39x 39x 39x 39x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 39x 39x 39x 39x 26x 26x 26x 26x 26x 16x 11x 11x 26x 26x 26x 39x 39x 39x 39x 29x 29x 168x 168x 168x 168x 29x 29x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 1x 1x | /** * Enhanced Routing System * * Provides advanced routing features: * - Route prefetching strategies * - Page transitions * - Code splitting per route * - Advanced scroll behavior */ /** * Create an enhanced router with advanced features * * @param {Object} options - Configuration options * @param {Object} [options.prefetch] - Prefetching configuration * @param {Object} [options.transitions] - Page transition configuration * @param {Object} [options.codeSplitting] - Code splitting configuration * @param {Object} [options.scrollBehavior] - Scroll behavior configuration * @returns {Object} Enhanced router instance */ export function createRouter(options = {}) { const opts = { mode: options.mode || 'history', base: options.base || '/', ...options }; // Ensure nested defaults are preserved opts.prefetch = { enabled: false, strategy: 'hover', delay: 100, maxConcurrent: 3, priority: { critical: 100, high: 50, normal: 0, low: -50 }, ...(options.prefetch || {}) }; opts.transitions = { enabled: false, default: { enter: 'fade-in', leave: 'fade-out', duration: 300 }, routes: {}, onStart: null, onComplete: null, ...(options.transitions || {}) }; opts.codeSplitting = { enabled: false, strategy: 'route', chunkNaming: '[name]-[hash]', preload: [], onLoad: null, ...(options.codeSplitting || {}) }; opts.scrollBehavior = { enabled: true, behavior: 'smooth', position: 'top', delay: 0, savePosition: true, custom: null, ...(options.scrollBehavior || {}) }; // Router state const state = { routes: new Map(), currentRoute: null, history: [], prefetchQueue: [], prefetching: new Set(), loadedChunks: new Map(), savedPositions: new Map(), transitionState: null }; // Statistics const stats = { navigations: 0, prefetches: 0, transitionsCompleted: 0, chunksLoaded: 0, scrollRestores: 0 }; /** * Register a route */ function addRoute(path, config) { state.routes.set(path, { path, component: config.component, meta: config.meta || {}, beforeEnter: config.beforeEnter, beforeLeave: config.beforeLeave, priority: config.priority || opts.prefetch.priority.normal, transition: config.transition || opts.transitions.default, lazy: typeof config.component === 'function', loaded: !config.component || typeof config.component !== 'function', chunk: null }); // Preload if configured if (opts.codeSplitting.enabled && Array.isArray(opts.codeSplitting.preload) && opts.codeSplitting.preload.includes(path)) { loadRoute(path); } } /** * Load a route component */ async function loadRoute(path) { const route = state.routes.get(path); if (!route) { throw new Error(`Route not found: ${path}`); } if (route.loaded) { return route.component; } try { const startTime = performance.now(); // Load the component const component = await route.component(); route.component = component; route.loaded = true; route.chunk = opts.codeSplitting.chunkNaming .replace('[name]', path.replace(/\//g, '-')) .replace('[hash]', generateHash(path)); const loadTime = performance.now() - startTime; state.loadedChunks.set(path, { path, loadTime, timestamp: Date.now() }); stats.chunksLoaded++; if (opts.codeSplitting.onLoad) { opts.codeSplitting.onLoad(path, component, loadTime); } return component; } catch (error) { route.loaded = false; throw new Error(`Failed to load route ${path}: ${error.message}`); } } /** * Prefetch a route */ async function prefetchRoute(path, priority = opts.prefetch.priority.normal) { if (!opts.prefetch.enabled) return; const route = state.routes.get(path); if (!route || route.loaded || state.prefetching.has(path)) { return; } // Add to queue with priority state.prefetchQueue.push({ path, priority }); state.prefetchQueue.sort((a, b) => b.priority - a.priority); // Process queue processPrefetchQueue(); } /** * Process prefetch queue */ async function processPrefetchQueue() { // Limit concurrent prefetches if (state.prefetching.size >= opts.prefetch.maxConcurrent) { return; } const item = state.prefetchQueue.shift(); if (!item) return; const { path } = item; state.prefetching.add(path); try { await loadRoute(path); stats.prefetches++; } catch (error) { console.warn(`Prefetch failed for ${path}:`, error); } finally { state.prefetching.delete(path); // Process next item in queue if (state.prefetchQueue.length > 0) { processPrefetchQueue(); } } } /** * Setup prefetch strategy */ function setupPrefetchStrategy(element, path) { if (!opts.prefetch.enabled || !element) return; if (opts.prefetch.strategy === 'hover') { let timeoutId; element.addEventListener('mouseenter', () => { timeoutId = setTimeout(() => { prefetchRoute(path); }, opts.prefetch.delay); }); element.addEventListener('mouseleave', () => { clearTimeout(timeoutId); }); } else if (opts.prefetch.strategy === 'visible') { const observer = new IntersectionObserver((entries) => { entries.forEach(entry => { if (entry.isIntersecting) { prefetchRoute(path); observer.unobserve(element); } }); }); observer.observe(element); } else if (opts.prefetch.strategy === 'idle') { if ('requestIdleCallback' in window) { requestIdleCallback(() => prefetchRoute(path)); } else { setTimeout(() => prefetchRoute(path), 1); } } } /** * Execute page transition */ async function executeTransition(from, to) { if (!opts.transitions.enabled) return; const transition = state.routes.get(to)?.transition || opts.transitions.default; state.transitionState = { from, to, phase: 'start' }; if (opts.transitions.onStart) { opts.transitions.onStart(from, to); } // Leave transition state.transitionState.phase = 'leave'; await applyTransition(transition.leave, transition.duration / 2); // Enter transition state.transitionState.phase = 'enter'; await applyTransition(transition.enter, transition.duration / 2); state.transitionState.phase = 'complete'; stats.transitionsCompleted++; if (opts.transitions.onComplete) { opts.transitions.onComplete(from, to); } state.transitionState = null; } /** * Apply transition animation */ function applyTransition(animationName, duration) { return new Promise(resolve => { const element = document.querySelector('[data-router-view]'); if (!element) { resolve(); return; } element.style.animation = `${animationName} ${duration}ms`; setTimeout(() => { element.style.animation = ''; resolve(); }, duration); }); } /** * Handle scroll behavior */ function handleScroll(to, from, savedPosition) { if (!opts.scrollBehavior.enabled) return; // Custom scroll behavior if (opts.scrollBehavior.custom) { const position = opts.scrollBehavior.custom(to, from, savedPosition); scrollToPosition(position); return; } // Default scroll behavior let position; if (savedPosition && opts.scrollBehavior.savePosition) { // Restore saved position position = savedPosition; stats.scrollRestores++; } else if (to.hash) { // Scroll to hash const element = document.querySelector(to.hash); if (element) { position = { el: element, behavior: opts.scrollBehavior.behavior }; } } else if (opts.scrollBehavior.position === 'top') { // Scroll to top position = { x: 0, y: 0 }; } else if (opts.scrollBehavior.position === 'saved' && savedPosition) { position = savedPosition; } if (position) { setTimeout(() => { scrollToPosition(position); }, opts.scrollBehavior.delay); } } /** * Scroll to position */ function scrollToPosition(position) { if (typeof window === 'undefined') return; if (position.el) { position.el.scrollIntoView({ behavior: position.behavior || opts.scrollBehavior.behavior }); } else { window.scrollTo({ left: position.x || 0, top: position.y || 0, behavior: position.behavior || opts.scrollBehavior.behavior }); } } /** * Save current scroll position */ function saveScrollPosition(path) { if (!opts.scrollBehavior.savePosition || typeof window === 'undefined') return; state.savedPositions.set(path, { x: window.scrollX, y: window.scrollY }); } /** * Navigate to a route */ async function push(path, options = {}) { stats.navigations++; const from = state.currentRoute; const to = { path, ...options }; // Save scroll position if (from) { saveScrollPosition(from.path); } try { // Execute transition (leave phase) if (opts.transitions.enabled) { await executeTransition(from?.path, path); } // Load route component const component = await loadRoute(path); // Update current route state.currentRoute = { path, component, meta: state.routes.get(path)?.meta || {}, ...options }; // Add to history state.history.push({ path, timestamp: Date.now() }); // Handle scroll const savedPosition = state.savedPositions.get(path); handleScroll(to, from, savedPosition); return true; } catch (error) { console.error('Navigation failed:', error); return false; } } /** * Replace current route */ async function replace(path, options = {}) { const result = await push(path, options); if (result && state.history.length > 1) { // Remove the previous history entry state.history.splice(state.history.length - 2, 1); } return result; } /** * Go back in history */ function back() { if (state.history.length > 1) { const previous = state.history[state.history.length - 2]; push(previous.path); } } /** * Go forward (if supported by browser) */ function forward() { if (typeof window !== 'undefined') { window.history.forward(); } } /** * Prefetch multiple routes */ function prefetchRoutes(paths, priority) { paths.forEach(path => prefetchRoute(path, priority)); } /** * Get route by path */ function getRoute(path) { return state.routes.get(path); } /** * Get all routes */ function getRoutes() { return Array.from(state.routes.values()); } /** * Get current route */ function getCurrentRoute() { return state.currentRoute; } /** * Get router statistics */ function getStats() { return { ...stats, routesRegistered: state.routes.size, prefetchQueueSize: state.prefetchQueue.length, activePrefetches: state.prefetching.size, loadedChunks: state.loadedChunks.size, savedPositions: state.savedPositions.size, historyLength: state.history.length }; } /** * Clear all caches */ function clearCaches() { state.prefetchQueue = []; state.prefetching.clear(); state.savedPositions.clear(); // Unload lazy-loaded chunks state.routes.forEach(route => { if (route.lazy && route.loaded) { route.loaded = false; } }); state.loadedChunks.clear(); } /** * Generate hash for chunk naming */ function generateHash(input) { let hash = 0; for (let i = 0; i < input.length; i++) { const char = input.charCodeAt(i); hash = ((hash << 5) - hash) + char; hash = hash & hash; } return Math.abs(hash).toString(36).substring(0, 8); } // Initialize if (opts.prefetch.enabled && opts.prefetch.strategy === 'idle') { // Prefetch all routes on idle if (typeof window !== 'undefined' && 'requestIdleCallback' in window) { requestIdleCallback(() => { state.routes.forEach((route, path) => { if (!route.loaded) { prefetchRoute(path, opts.prefetch.priority.low); } }); }); } } return { addRoute, push, replace, back, forward, prefetchRoute, prefetchRoutes, setupPrefetchStrategy, getRoute, getRoutes, getCurrentRoute, getStats, clearCaches, // Expose state for testing _state: state }; } // Export default instance export const router = createRouter(); |