Make URL params available to client apps via context

This commit is contained in:
Andrew Kingston 2021-02-10 15:49:23 +00:00
parent 4493a0896f
commit 3ffe00fe2f
2 changed files with 16 additions and 17 deletions

View File

@ -1,5 +1,5 @@
<script> <script>
import { getContext, setContext } from "svelte" import { getContext } from "svelte"
import Router from "svelte-spa-router" import Router from "svelte-spa-router"
import { routeStore } from "../store" import { routeStore } from "../store"
import Screen from "./Screen.svelte" import Screen from "./Screen.svelte"
@ -10,12 +10,10 @@
// Only wrap this as an array to take advantage of svelte keying, // Only wrap this as an array to take advantage of svelte keying,
// to ensure the svelte-spa-router is fully remounted when route config // to ensure the svelte-spa-router is fully remounted when route config
// changes // changes
$: configs = [ $: config = {
{ routes: getRouterConfig($routeStore.routes),
routes: getRouterConfig($routeStore.routes), id: $routeStore.routeSessionId,
id: $routeStore.routeSessionId, }
},
]
const getRouterConfig = routes => { const getRouterConfig = routes => {
let config = {} let config = {}
@ -33,11 +31,11 @@
} }
</script> </script>
{#each configs as config (config.id)} {#key config.id}
<div use:styleable={$component.styles}> <div use:styleable={$component.styles}>
<Router on:routeLoading={onRouteLoading} routes={config.routes} /> <Router on:routeLoading={onRouteLoading} routes={config.routes} />
</div> </div>
{/each} {/key}
<style> <style>
div { div {

View File

@ -2,6 +2,7 @@
import { fade } from "svelte/transition" import { fade } from "svelte/transition"
import { screenStore, routeStore } from "../store" import { screenStore, routeStore } from "../store"
import Component from "./Component.svelte" import Component from "./Component.svelte"
import Provider from "./Provider.svelte"
// Keep route params up to date // Keep route params up to date
export let params = {} export let params = {}
@ -12,16 +13,16 @@
// Redirect to home layout if no matching route // Redirect to home layout if no matching route
$: screenDefinition == null && routeStore.actions.navigate("/") $: screenDefinition == null && routeStore.actions.navigate("/")
// Make a screen array so we can use keying to properly re-render each screen
$: screens = screenDefinition ? [screenDefinition] : []
</script> </script>
{#each screens as screen (screen._id)} <!-- Ensure to fully remount when screen changes -->
<div in:fade> {#key screenDefinition?._id}
<Component definition={screen} /> <Provider key="url" data={params}>
</div> <div in:fade>
{/each} <Component definition={screenDefinition} />
</div>
</Provider>
{/key}
<style> <style>
div { div {