Bird
Raised Fist0
Svelteframework~8 mins

Project structure in Svelte - Performance & Optimization

Choose your learning style10 modes available
Performance: Project structure
MEDIUM IMPACT
Project structure affects how quickly the browser can load and render the app by influencing code splitting and lazy loading.
Organizing Svelte components and routes for optimal loading
Svelte
src/
  App.svelte
  components/
    Header.svelte
    Footer.svelte
  routes/
    +page.svelte (Home)
    about/
      +page.svelte
    contact/
      +page.svelte

// Use SvelteKit file-based routing with dynamic imports for lazy loading routes
Routes are code-split and loaded only when visited, reducing initial bundle size and speeding up first paint.
šŸ“ˆ Performance GainReduces initial bundle size by 50-70%, improves LCP by 200-300ms
Organizing Svelte components and routes for optimal loading
Svelte
src/
  App.svelte
  components/
    Header.svelte
    Footer.svelte
  routes/
    Home.svelte
    About.svelte
    Contact.svelte

// All components imported eagerly in App.svelte
All components and routes are bundled and loaded upfront, increasing initial bundle size and slowing page load.
šŸ“‰ Performance CostBlocks rendering for 300-500ms on initial load due to large bundle size
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Monolithic project with eager importsLow (few nodes initially)Low (few layout changes)High (large JS bundle delays paint)[X] Bad
Modular project with lazy-loaded routesLow (few nodes initially)Low (few layout changes)Low (small JS bundle, faster paint)[OK] Good
Rendering Pipeline
A well-structured Svelte project enables the bundler to split code into smaller chunks. The browser loads only needed chunks initially, reducing parsing and execution time.
→Network Loading
→JavaScript Parsing
→Style Calculation
→Layout
āš ļø BottleneckNetwork Loading and JavaScript Parsing due to large bundles
Core Web Vital Affected
LCP
Project structure affects how quickly the browser can load and render the app by influencing code splitting and lazy loading.
Optimization Tips
1Split routes into separate files to enable lazy loading.
2Avoid importing all components eagerly in the main app file.
3Use SvelteKit file-based routing conventions for automatic code splitting.
Performance Quiz - 3 Questions
Test your performance knowledge
How does a flat project structure with all components imported eagerly affect Svelte app performance?
AHas no effect on performance
BReduces bundle size and speeds up page load
CIncreases initial bundle size and slows page load
DImproves runtime interaction speed
DevTools: Network and Performance
How to check: Open DevTools > Network tab, reload page, check JS bundle sizes and load times. Then open Performance tab, record page load, and check scripting and loading times.
What to look for: Look for smaller initial JS bundles and shorter scripting times indicating faster parsing and execution.

Practice

(1/5)
1. In a Svelte project, where should you place reusable components that can be used across multiple pages?
easy
A. static/components
B. src/routes
C. src/lib
D. public/assets

Solution

  1. Step 1: Understand folder purpose

    src/lib is designed for reusable components and utilities.
  2. Step 2: Compare with other folders

    src/routes holds pages, public/assets is for static files, and static/components is not a standard folder.
  3. Final Answer:

    src/lib -> Option C
  4. Quick Check:

    Reusable components = src/lib [OK]
Hint: Reusable parts go in src/lib folder [OK]
Common Mistakes:
  • Putting reusable components in src/routes
  • Using public folder for components
  • Creating non-standard folders like static/components
2. Which of the following is the correct folder to place page files in a SvelteKit project?
easy
A. src/pages
B. src/lib
C. public/pages
D. src/routes

Solution

  1. Step 1: Identify the folder for pages

    In SvelteKit, src/routes is the folder where page components live.
  2. Step 2: Check other options

    src/lib is for reusable components, public/pages and src/pages are not standard folders for pages.
  3. Final Answer:

    src/routes -> Option D
  4. Quick Check:

    Pages go in src/routes [OK]
Hint: Pages always go in src/routes in SvelteKit [OK]
Common Mistakes:
  • Confusing src/lib with src/routes
  • Placing pages in public folder
  • Using non-standard src/pages folder
3. Given the following SvelteKit project structure:
project/
ā”œā”€ public/
│  └─ logo.png
ā”œā”€ src/
│  ā”œā”€ lib/
│  │  └─ Button.svelte
│  └─ routes/
│     └─ index.svelte

Where should you import the Button.svelte component to use it on the homepage?
medium
A. From ./Button.svelte inside src/routes/index.svelte
B. From ../lib/Button.svelte inside src/routes/index.svelte
C. From ../../public/logo.png inside src/routes/index.svelte
D. From public/Button.svelte inside src/routes/index.svelte

Solution

  1. Step 1: Understand folder hierarchy

    src/routes/index.svelte is one level deeper than src/lib/Button.svelte, so import path is ../lib/Button.svelte.
  2. Step 2: Eliminate wrong paths

    ../../public/logo.png is for static assets, not components; ./Button.svelte assumes Button is in routes folder; public/Button.svelte is incorrect location.
  3. Final Answer:

    From ../lib/Button.svelte inside src/routes/index.svelte -> Option B
  4. Quick Check:

    Import reusable components with relative path ../lib [OK]
Hint: Use relative path ../lib to import components from routes [OK]
Common Mistakes:
  • Trying to import components from public folder
  • Using wrong relative path like ./Button.svelte
  • Confusing static assets with components
4. You placed an image file inside src/lib/images and tried to use it in your SvelteKit page, but it doesn't load in the browser. What is the most likely reason?
medium
A. Static assets must be inside the public folder to be served correctly.
B. Images must be imported from src/routes folder only.
C. You need to rename the image file to have a .svelte extension.
D. The image file size is too large to load.

Solution

  1. Step 1: Understand static asset serving

    SvelteKit serves static files only from the public folder, so files in src/lib/images are not accessible directly.
  2. Step 2: Check other options

    Images don't belong in src/routes, renaming to .svelte is incorrect, and file size doesn't prevent loading if path is correct.
  3. Final Answer:

    Static assets must be inside the public folder to be served correctly. -> Option A
  4. Quick Check:

    Static files go in public folder [OK]
Hint: Put images in public folder to load in browser [OK]
Common Mistakes:
  • Placing images inside src/lib or src/routes
  • Renaming images as .svelte files
  • Assuming large file size causes loading failure
5. You want to organize your SvelteKit project so that:
1. All page routes are inside src/routes.
2. Reusable UI components are inside src/lib/components.
3. Static assets like images and fonts are inside public/assets.

Which folder structure below correctly reflects this setup?
hard
A.
project/
ā”œā”€ public/
│  └─ assets/
│     ā”œā”€ images/
│     └─ fonts/
ā”œā”€ src/
│  ā”œā”€ lib/
│  │  └─ components/
│  │     └─ Button.svelte
│  └─ routes/
│     └─ index.svelte
B.
project/
ā”œā”€ src/
│  ā”œā”€ assets/
│  │  ā”œā”€ images/
│  │  └─ fonts/
│  ā”œā”€ components/
│  │  └─ Button.svelte
│  └─ pages/
│     └─ index.svelte
C.
project/
ā”œā”€ public/
│  └─ components/
│     └─ Button.svelte
ā”œā”€ src/
│  ā”œā”€ lib/
│  └─ routes/
│     └─ index.svelte
D.
project/
ā”œā”€ public/
│  └─ assets/
│     └─ Button.svelte
ā”œā”€ src/
│  ā”œā”€ lib/
│  └─ routes/
│     └─ index.svelte

Solution

  1. Step 1: Verify static assets location

    Static assets like images and fonts must be inside public/assets to be served correctly.
  2. Step 2: Check reusable components and routes

    Reusable components go inside src/lib/components, and pages go inside src/routes.
  3. Step 3: Match folder structure

    project/
    ā”œā”€ public/
    │  └─ assets/
    │     ā”œā”€ images/
    │     └─ fonts/
    ā”œā”€ src/
    │  ā”œā”€ lib/
    │  │  └─ components/
    │  │     └─ Button.svelte
    │  └─ routes/
    │     └─ index.svelte
    matches all requirements exactly; other options place assets or components incorrectly.
  4. Final Answer:

    Option A folder structure -> Option A
  5. Quick Check:

    Correct folder setup =
    project/
    ā”œā”€ public/
    │  └─ assets/
    │     ā”œā”€ images/
    │     └─ fonts/
    ā”œā”€ src/
    │  ā”œā”€ lib/
    │  │  └─ components/
    │  │     └─ Button.svelte
    │  └─ routes/
    │     └─ index.svelte
    [OK]
Hint: Static assets in public, components in src/lib, pages in src/routes [OK]
Common Mistakes:
  • Putting static assets inside src folder
  • Placing components in public folder
  • Using non-standard folders like pages instead of routes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard