Create a pages metadata module using a SvelteKit remote function (prerender) instead of a plain client-side module with import.meta.glob. This keeps the glob/metadata resolution on the server and prerendered at build time.

Goal

Provide a single module that exports all page metadata, enabling:

  • Type-safe access to page metadata
  • Easy iteration over all pages (for sitemap, feeds, navigation)
  • Consistent metadata structure across the app
  • Server-side only execution (no glob in the client bundle)

Relevant Docs

Approach

Use a prerender remote function since page metadata only changes at build time. The result gets cached and prerendered during the build, so no runtime server is needed for the static site.

Implementation

1. Enable remote functions in svelte.config.js:

export default { kit: { experimental: { remoteFunctions: true } } }

2. Create src/routes/pages.remote.ts:

import { prerender } from '$app/server';

const modules = import.meta.glob('/src/routes/**/+page.svelte', {
  import: 'meta',
  eager: true,
});

export const getPages = prerender(async () => {
  return Object.entries(modules).map(([path, meta]) => {
    const url = path
      .replace('/src/routes', '')
      .replace('/+page.svelte', '')
      .replace(/\([^)]+\)\//g, '') || '/';
    return { url, ...meta };
  });
});

3. Use in components:

<script>
  import { getPages } from './pages.remote';
</script>

{#each await getPages() as page}
  <a href={page.url}>{page.title}</a>
{/each}

Consumers to Update

  • src/lib/index.js - remove the pages export (currently uses import.meta.glob on client)
  • src/lib/page-renderer.svelte.js - use remote function instead of glob
  • src/routes/sitemap/+page.server.js - use remote function
  • src/routes/feed.json/+server.js - use remote function

Config Requirements

  • Enable experimental.remoteFunctions in svelte.config.js
  • Enable compilerOptions.experimental.async for await in templates
  • Remote function files use .remote.ts extension
  • Cannot be placed in src/lib/server/

Centralized metadata

Single source of truth

A static site template built with SvelteKit.

Sveleton, 2025