📖 Scenario: You are building a simple web page using the Fresh framework on Deno. The page shows a greeting message and a button that updates a counter when clicked. To keep the page fast and interactive, you will use Islands architecture to isolate the interactive button as a client-side component.
🎯 Goal: Create a Fresh project with a server-rendered page that includes a client-side interactive counter button using Islands architecture.
📋 What You'll Learn
Create a server-rendered page with a greeting message
Add a client-side interactive counter button as an island
Use Fresh's island component pattern correctly
Ensure the counter increments on button clicks without full page reload
💡 Why This Matters
🌍 Real World
Islands architecture helps build fast web pages by server-rendering static parts and hydrating only small interactive parts on the client. This improves performance and user experience.
💼 Career
Understanding Islands architecture is valuable for frontend developers working with modern frameworks like Fresh, Astro, or Qwik, enabling them to build efficient and scalable web apps.
Progress0 / 4 steps
1
Create the server-rendered page with greeting
Create a file routes/index.tsx with a default exported function component called Home. Inside it, return a <main> element containing an <h1> with the text "Welcome to Fresh Islands!".
Deno
Hint
Use a function component named Home that returns JSX with a <main> and <h1> inside.
2
Create the counter island component
Create a new file islands/Counter.tsx. Export a default function component called Counter. Inside, use React's useState hook to create a count state starting at 0. Return a <button> that shows the text "Count: " followed by the current count. When clicked, the button should increment the count by 1.
Deno
Hint
Use useState from preact/hooks to create a counter state. Return a button that increments the count on click.
3
Import and use the Counter island in the page
In routes/index.tsx, import the Counter component from ../islands/Counter.tsx. Inside the Home component's <main>, below the <h1>, add the <Counter /> component to render the interactive button.
Deno
Hint
Import Counter from ../islands/Counter.tsx and add <Counter /> inside the <main> below the heading.
4
Mark the Counter component as an island
In islands/Counter.tsx, add the line /** @jsxImportSource preact */ at the top to enable JSX. Then export the Counter component as default. This marks it as an island for Fresh to hydrate on the client side.
Deno
Hint
Add /** @jsxImportSource preact */ at the top of islands/Counter.tsx to mark it as an island component.
Practice
(1/5)
1. What is the main purpose of the Islands architecture in Fresh?
easy
A. To split pages into static and interactive parts for better performance
B. To make all parts of the page fully interactive
C. To load the entire page on the client side only
D. To disable JavaScript on the page
Solution
Step 1: Understand Islands architecture concept
Islands architecture divides a web page into static parts and small interactive components called islands.
Step 2: Identify the main benefit
This split improves page speed and maintainability by only hydrating interactive parts.
Final Answer:
To split pages into static and interactive parts for better performance -> Option A