Type-safe route interception for SvelteKit.
Inspired by Next.js Intercepting Routes: https://nextjs.org/docs/app/api-reference/file-conventions/intercepting-routes
This library solves one pattern.
You have a real route like /photos/[id], but when users click from inside the app, you want to show
that route in a modal. The URL still updates to the canonical route. If users refresh that URL or open
it directly, they still get the normal page route.
Route params are inferred from route templates like /photos/[id] and /docs/[...slug].
pnpm add @wiscaksono/sveltekit-route-intercept
import { defineConfig } from 'vite';
import { sveltekit } from '@sveltejs/kit/vite';
import { sveltekitRouteIntercept } from '@wiscaksono/sveltekit-route-intercept/vite';
export default defineConfig({
plugins: [
sveltekitRouteIntercept({
interceptors: [{ key: 'photo', route: '/photos/[id]' }]
}),
sveltekit()
]
});
This generates:
src/intercept.generated.d.tssrc/lib/interceptors.generated.ts<script lang="ts">
import Modal from '$lib/Modal.svelte';
import { interceptors } from '$lib/interceptors.generated';
type PhotoPayload = { photo: { id: string; title: string } };
const isPhotoPayload = (value: unknown): value is PhotoPayload => {
return typeof value === 'object' && value !== null && 'photo' in value;
};
const photo = interceptors.photoTyped<PhotoPayload>({
parsePayload: isPhotoPayload
});
const selected = $derived(photo.selected());
</script>
<a use:photo.link={{ params: { id: 'aurora-lake' } }} href={photo.href({ id: 'aurora-lake' })}>
Open photo
</a>
{#if selected}
<Modal title={selected.photo.title} onclose={() => photo.close()}>
<!-- render route content -->
</Modal>
{/if}
interceptors.photo() uses JSON-safe payload validation.interceptors.photoTyped<TPayload>({ parsePayload }) requires a type guard and gives strict payload typing._blank, download, modified clicks, and external links are bypassed.goto.defineInterceptcreateInterceptorcreateKitInterceptorcreateInterceptLinkActionThis monorepo includes one package:
@wiscaksono/sveltekit-route-intercept (runtime + SvelteKit APIs + Vite plugin subpath)