📖 Scenario: You are building a simple web page that shows a list of favorite fruits. You want the page to work well even if JavaScript is disabled, but also enhance the experience when JavaScript is available.
🎯 Goal: Create a Svelte component that displays a list of fruits using basic HTML first, then progressively enhance it by adding a filter input that lets users search fruits dynamically.
📋 What You'll Learn
Create a list of fruits in the component
Add a text input to filter fruits by name
Use Svelte reactive statements to update the filtered list
Ensure the list is visible and usable without JavaScript
💡 Why This Matters
🌍 Real World
Many websites need to work well even if users disable JavaScript or have slow connections. Progressive enhancement ensures basic content is always accessible and improves the experience when JavaScript is available.
💼 Career
Understanding progressive enhancement is important for front-end developers to build accessible, robust web applications that work for all users.
Progress0 / 4 steps
1
Create the initial fruit list
Create a Svelte component with a variable called fruits that is an array containing these exact strings: "Apple", "Banana", "Cherry", "Date", and "Elderberry". Then, add an unordered list <ul> in the markup that uses {#each fruits as fruit} to display each fruit inside a <li>.
Svelte
Hint
Start by declaring the fruits array inside a <script> tag. Then use Svelte's {#each} block to loop through the array and display each fruit in a list item.
2
Add a filter input variable
Add a variable called filter initialized to an empty string "" inside the <script> tag. Then add a text input <input type="text" bind:value={filter} placeholder="Filter fruits" /> above the list in the markup.
Svelte
Hint
Declare the filter variable in the script. Then add an input element with bind:value={filter} so it updates as the user types.
3
Filter the fruit list reactively
Add a reactive statement $: filteredFruits = fruits.filter(fruit => fruit.toLowerCase().includes(filter.toLowerCase())) inside the <script> tag. Then update the {#each} block to loop over filteredFruits instead of fruits.
Svelte
Hint
Use a reactive statement starting with $: to create filteredFruits based on the current filter. Then loop over filteredFruits in the markup.
4
Add a <noscript> message for no JavaScript
Add a <noscript> tag below the input that contains a paragraph <p> with the exact text: JavaScript is disabled. You see the full fruit list.. This message will show only if JavaScript is disabled, enhancing accessibility.
Svelte
Hint
Use the <noscript> tag to show a message only when JavaScript is off. Put a paragraph inside with the exact text.
Practice
(1/5)
1. What is the main goal of progressive enhancement in Svelte applications?
easy
A. To load all data at once for faster performance
B. To use only JavaScript for all app functionality
C. To avoid using semantic HTML elements
D. To make sure the app works with basic features before adding advanced ones
Solution
Step 1: Understand the concept of progressive enhancement
Progressive enhancement means starting with a simple, working version of the app that everyone can use.
Step 2: Identify the goal in Svelte apps
In Svelte, this means building basic content first, then adding interactive features on top.
Final Answer:
To make sure the app works with basic features before adding advanced ones -> Option D
Quick Check:
Progressive enhancement = basic first, then advanced [OK]
Hint: Think: start simple, then add features [OK]
Common Mistakes:
Assuming JavaScript must be used for everything
Ignoring basic content for advanced features
Skipping semantic HTML importance
2. Which of the following is the correct way to provide fallback content for users without JavaScript in a Svelte app?
easy
A.
Enable JavaScript to see this app.
B.
C.
No JS support
D. Enable JS
Solution
Step 1: Identify the HTML element for no-JavaScript fallback
The tag is designed to show content only when JavaScript is disabled.
Step 2: Check the options for correct usage
Enable JavaScript to see this app.
uses correctly with fallback text. Others misuse tags or won't show fallback properly.
Final Answer:
<noscript><p>Enable JavaScript to see this app.</p></noscript> -> Option A
Quick Check:
Fallback for no JS = <noscript> [OK]
Hint: Use <noscript> for no-JS fallback content [OK]
Common Mistakes:
Using <script> to detect JS absence
Hiding fallback with CSS only
Using <template> which doesn't render
3. Given this Svelte snippet for progressive enhancement: