Last active
June 11, 2021 22:24
-
-
Save tomatrow/73bb4560efb9bc9536c4c80ce54ff2b0 to your computer and use it in GitHub Desktop.
Kind of does does rewrites for SvelteKit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script lang="ts" context="module"> | |
import { rewriteLoad } from "./RewriteLoader.svelte" | |
import { load as loadCareers } from "./careers.svelte" | |
import { load as loadAbout } from "./about.svelte" | |
import { load as loadContact } from "./contact.svelte" | |
enum PageRoute { | |
careers, | |
about, | |
contact | |
} | |
export const load = rewriteLoad([ | |
{ id: PageRoute.careers, load: loadCareers }, | |
{ id: PageRoute.about, load: loadAbout }, | |
{ id: PageRoute.contact, load: loadContact } | |
]) | |
</script> | |
<script lang="ts"> | |
import { RewriteLoader } from "$lib/components" | |
export let id: PageRoute | |
export let props: any | |
function resolve(id: PageRoute) { | |
switch (id) { | |
case PageRoute.careers: | |
return import("./careers.svelte") | |
case PageRoute.about: | |
return import("./about.svelte") | |
case PageRoute.contact: | |
return import("./contact.svelte") | |
default: | |
throw new Error(`Unknown id ${id}`) | |
} | |
} | |
</script> | |
<RewriteLoader {resolve} {id} {props} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script context="module" lang="ts"> | |
import type { Load, LoadInput, LoadOutput } from "@sveltejs/kit" | |
export interface Route { | |
id: any | |
load: Load | |
} | |
export function rewriteLoad(routes: Route[]): Load { | |
return async input => { | |
let id: any | |
let output: LoadOutput | |
for (const route of routes) { | |
output = await route.load(input) | |
if (output) { | |
id = route.id | |
break | |
} | |
} | |
if (!id) return | |
return { | |
...output, | |
props: { | |
id, | |
props: output.props | |
} | |
} | |
} | |
} | |
</script> | |
<script lang="ts"> | |
import type { SvelteComponent } from "svelte" | |
export let id: any | |
export let props: any | |
export let resolve: (id: any) => Promise<{ default: SvelteComponent }> | |
</script> | |
{#await resolve(id) then module} | |
<svelte:component this={module?.default} {...props ?? {}} /> | |
{/await} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment