SvelteKit

repository·version-3·Indexed 12 days ago

https://github.com/sveltejs/kit

A high-performance framework for building web applications using Svelte. It provides a complete developer experience including file-system based routing, server-side rendering (SSR), and a plugin-based adapter system for deployment to platforms like Vercel, Netlify, Cloudflare, and Node.js. SvelteKit leverages Vite for build optimizations and Hot Module Replacement (HMR).

Tokens
119.2K
Snippets
404
Records
592
Agent score
96%

What's inside SvelteKit

  1. Overview of SvelteKit and available packages

    version-3

    SvelteKit is a framework for streamlined web development. It consists of the core @sveltejs/kit package and several official adapters used to deploy your application to different environments.

    Official packages include:

    • @sveltejs/kit: The core framework.
    • @sveltejs/adapter-auto: Automatically detects the deployment environment.
    • @sveltejs/adapter-cloudflare: For Cloudflare deployments.
    • @sveltejs/adapter-netlify: For Netlify deployments.
    • @sveltejs/adapter-node: For Node.js environments.
    • @sveltejs/adapter-static: For static site generation (SSG).
    • @sveltejs/adapter-vercel: For Vercel deployments.
    • @sveltejs/enhanced-img: For optimized image handling.
    • @sveltejs/package: A utility for packaging libraries.

    Community-maintained adapters are also available via the Svelte Society.

  2. Overview of @sveltejs/enhanced-img

    version-3

    @sveltejs/enhanced-img is a Vite plugin that utilizes a Svelte preprocessor to identify images within your project and transform them during the build process. This allows for automated image optimization and transformation at build-time.

    WARNING: This package is currently experimental. It follows pre-1.0 versioning, meaning breaking changes may be introduced in any minor version release. Use with caution in production environments.

  3. Use @sveltejs/adapter-cloudflare for Cloudflare deployments

    version-3

    Use @sveltejs/adapter-cloudflare to build SvelteKit applications for deployment on Cloudflare. It supports two primary deployment patterns:

    1. Cloudflare Workers: Deploying with static assets support.
    2. Cloudflare Pages: Deploying with Workers integration (Functions).

    For detailed configuration and usage instructions, refer to the official SvelteKit adapter-cloudflare documentation.

    npm i -D @sveltejs/adapter-cloudflare
  4. Use @sveltejs/adapter-node to build a standalone Node server

    version-3
    The @sveltejs/adapter-node is a SvelteKit adapter designed to build SvelteKit applications into a standalone Node.js server. This is useful when you want to deploy your application to a server environment that runs Node.js (such as a VPS, a Docker container, or a managed service like Heroku) rather than using serverless functions.
  5. Understand the SvelteKit project structure

    version-3

    A typical SvelteKit project is organized into several key directories and files. The src directory contains the application logic, static holds unmanaged assets, and tests contains browser-based tests.

    my-project/
    ├ src/
    │ ├ lib/
    │ │ └ [your lib files]
    │ ├ params/
    │ │ └ [your param matchers]
    │ ├ routes/
    │ │ └ [your routes]
    │ ├ service-worker/
    │ │ ├ index.js
    │ │ └ tsconfig.json
    │ ├ app.html
    │ ├ error.html
    │ ├ hooks.client.js
    │ ├ hooks.server.js
    │ └ instrumentation.server.js
    ├ static/
    │ └ [your static assets]
    ├ tests/
    │ └ [your tests]
    ├ package.json
    ├ tsconfig.json
    └ vite.config.js
  6. Use @sveltejs/adapter-vercel to deploy SvelteKit to Vercel

    version-3

    The @sveltejs/adapter-vercel package is a SvelteKit adapter designed to build your application specifically for deployment on the Vercel platform. It configures your SvelteKit app to leverage Vercel's infrastructure, such as Serverless Functions or Edge Functions, depending on your configuration.

    For detailed configuration options (such as specifying the runtime or output mode), refer to the official SvelteKit documentation.

    // Example installation
    npm install -D @sveltejs/adapter-vercel
  7. Use @sveltejs/adapter-static for static site generation or SPAs

    version-3

    @sveltejs/adapter-static is a SvelteKit adapter that prerenders your entire site into a collection of static files.

    It supports two primary modes of operation:

    1. Static Site Generation (SSG): Prerenders the entire site into static HTML files.
    2. Single Page Application (SPA): By specifying a fallback page (which typically renders an empty shell), you can use this adapter to create an SPA.

    Note: If you want to prerender only specific pages without turning the entire site into an SPA, you should use a different adapter in combination with SvelteKit's prerender page option.

    // Example concept: To create an SPA, you would configure a fallback page in your svelte.config.js
    // (Exact configuration syntax depends on the adapter's API, typically via the 'fallback' option)
    export default {
      kit: {
        adapter: adapter({
          fallback: '404.html' // or 'index.html' depending on your hosting provider
        })
      }
    };
  8. Use @sveltejs/adapter-auto for automatic environment detection

    version-3
    The @sveltejs/adapter-auto package is a SvelteKit adapter that automatically detects your deployment environment and selects the appropriate adapter (such as Vercel, Netlify, or Cloudflare Pages) if a compatible one is available. This allows your project to be portable across different hosting providers without manual configuration changes, provided the environment supports one of the built-in auto-adapters.
  9. Core features of SvelteKit

    version-3

    SvelteKit automates modern web development best practices through several core capabilities:

    • Routing: A built-in router that updates the UI when links are clicked.
    • Configurable Rendering: Supports Server-Side Rendering (SSR), Client-Side Rendering (CSR), and Prerendering (at build-time) to handle different parts of your application according to your needs.
    • Build Optimizations: Leverages Vite to ensure only the minimal required code is loaded.
    • Preloading: Ability to preload pages before a user even navigates to them.
    • Offline Support: Integration with Service Workers.
    • Image Optimization: Built-in tools for handling images efficiently.
    • Fast Development: Uses Vite and the Svelte plugin to provide Hot Module Replacement (HMR), reflecting code changes in the browser instantly.
  10. Use @sveltejs/adapter-netlify to deploy SvelteKit to Netlify

    version-3
    The @sveltejs/adapter-netlify package is a SvelteKit adapter designed to build your application specifically for deployment on Netlify. It handles the transformation of your SvelteKit app into a format compatible with Netlify Functions and Netlify's edge runtime.
  11. Handle ESM imports and TypeScript file extensions

    version-3

    When writing library code, you must adhere to Node's ESM algorithm for relative imports:

    • Explicit Extensions: All relative file imports must include the full filename and extension. For example, import { something } from './something/index.js'; is required (even if the file is actually index.js).
    • TypeScript Caveat: When importing .ts files in a TypeScript project, you must use the .js extension in your import statements. This is a requirement of the TypeScript design.
    • Recommended Config: To make this easier, set "moduleResolution": "NodeNext" in your tsconfig.json or jsconfig.json.
    // Correct ESM import
    import { something } from './something/index.js';