proper usage of page store
This commit is contained in:
		
							
								
								
									
										1
									
								
								src/global.d.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								src/global.d.ts
									
									
									
									
										vendored
									
									
								
							| @@ -1 +0,0 @@ | |||||||
| /// <reference types="@sveltejs/kit" /> |  | ||||||
| @@ -1,26 +1,23 @@ | |||||||
| <script> | <script context="module"> | ||||||
|     import { page } from '$app/stores'; |     import { page } from '$app/stores'; | ||||||
|     export let href; |  | ||||||
|     let pageHost; |  | ||||||
|     page.subscribe(p => pageHost = p.host); // apparently this works? |  | ||||||
|  |  | ||||||
|     function isLocal(url) { |  | ||||||
|         if (url.startsWith('/')) { |  | ||||||
|             return true; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|  |     function host(url) { | ||||||
|         try { |         try { | ||||||
|             let parsed = new URL(url); |             let u = new URL(url); | ||||||
|             return parsed.host === pageHost; |             return u.host; | ||||||
|         } |         } | ||||||
|         catch { |         catch { | ||||||
|             return false; |             return null; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| </script> | </script> | ||||||
|  |  | ||||||
|  | <script> | ||||||
|  |     export let href; | ||||||
|  | </script> | ||||||
|  |  | ||||||
| <!-- we pass href explicitly so a11y doesn't scream at us --> | <!-- we pass href explicitly so a11y doesn't scream at us --> | ||||||
| {#if isLocal(href)} | {#if href.startsWith('/') || host(href) === $page.host} | ||||||
|     <a sveltekit:prefetch {href} {...$$props}> |     <a sveltekit:prefetch {href} {...$$props}> | ||||||
|         <slot></slot> |         <slot></slot> | ||||||
|     </a> |     </a> | ||||||
|   | |||||||
| @@ -5,10 +5,13 @@ | |||||||
|  |  | ||||||
| <script> | <script> | ||||||
|     export let title, date; |     export let title, date; | ||||||
|  |     let pDate = new Date(date); | ||||||
|  |  | ||||||
| </script> | </script> | ||||||
|  |  | ||||||
|  |  | ||||||
| <div id="post"> | <div id="post"> | ||||||
|     <h1>{title}</h1> |     <h1>{title}</h1> | ||||||
|  |     <p><em>Composed: {pDate.toLocaleDateString('en-US')}</em></p> | ||||||
|     <slot></slot> |     <slot></slot> | ||||||
| </div> | </div> | ||||||
|   | |||||||
| @@ -33,7 +33,7 @@ | |||||||
|     <nav id="nav-main"> |     <nav id="nav-main"> | ||||||
|         <a sveltekit:prefetch href="/">Home</a> |         <a sveltekit:prefetch href="/">Home</a> | ||||||
|         <a sveltekit:prefetch href="/posts">Posts</a> |         <a sveltekit:prefetch href="/posts">Posts</a> | ||||||
|         <a sveltekit:prefetch href="#">About</a> |         <a sveltekit:prefetch href="/">About</a> | ||||||
|     </nav> |     </nav> | ||||||
| </div> | </div> | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user