SvelteKit: SVG on HTML page, or as standalone SVG file depending on route

2 min read 05-10-2024
SvelteKit: SVG on HTML page, or as standalone SVG file depending on route


Dynamically Serving SVGs in SvelteKit: HTML or Standalone File

Imagine you're building a website with a beautiful logo. You want this logo to appear seamlessly integrated into the HTML on most pages, but on a specific "About" page, you want to offer a high-resolution version as a standalone SVG download. SvelteKit, with its flexibility and route-based architecture, makes this incredibly easy.

Scenario:

Let's say you have an src/lib/components/Logo.svelte component that renders your logo:

<script>
  export let size = 'small';
</script>

<svg width="{size === 'small' ? '50px' : '100px'}" height="{size === 'small' ? '50px' : '100px'}">
  <path d="M...your logo path..." fill="#000" />
</svg>

Problem: How do you serve this logo as an embedded SVG on all pages except the "About" page, where it should be served as a standalone downloadable SVG file?

Solution:

SvelteKit's dynamic routing and file serving capabilities make this a breeze. Let's break down the solution into two parts:

1. Conditional Rendering:

In your Svelte components, we can use Svelte's built-in $page object to determine the current route. This allows us to dynamically render the logo differently based on the page.

<script>
  import { page } from '$app/stores';

  $: currentPage = $page.url.pathname;
</script>

{#if currentPage !== '/about'}
  <Logo size="small" />
{:else}
  <a href="/logo.svg" download="logo.svg">
    <Logo size="large" />
  </a>
{/if}

This code checks if the current route is /about. If not, it renders the Logo component with the small size. Otherwise, it presents a downloadable link to a separate SVG file.

2. Serving the Standalone SVG:

We need to create a file src/routes/logo.svg with the SVG content for the downloadable version. Since it's a standalone file, you might want to include a larger size than the embedded version for better resolution.

<svg width="200px" height="200px">
  <path d="M...your logo path..." fill="#000" />
</svg>

Key Points:

  • Dynamic Routing: SvelteKit's dynamic routing with $page allows us to control the behavior of our components based on the current route.
  • Flexibility: This solution allows for different representations of the logo based on the page, ensuring optimal user experience and usability.
  • SEO: The SVG is directly embedded into the HTML, ensuring accessibility and visibility for search engines.

Additional Value:

This solution is easily scalable. You can use the same approach to serve different versions of your logo for different screen sizes or other factors. Additionally, you can integrate this logic with your design system for a consistent and maintainable approach to handling SVGs across your project.

References:

By employing these techniques, you can create a dynamic and responsive website that seamlessly serves different versions of your SVG content, enhancing user experience and optimizing for SEO.