📖 Scenario: You are creating a simple website using Deno Fresh framework. The website will have multiple pages with a shared layout that includes a header and footer.
🎯 Goal: Build a Deno Fresh project with two routes: a home page and an about page. Both pages should use a shared layout component that displays a header and footer.
📋 What You'll Learn
Create a route file for the home page at routes/index.tsx with a simple welcome message.
Create a route file for the about page at routes/about.tsx with an about message.
Create a shared layout component in components/Layout.tsx that includes a header and footer.
Use the shared layout component in both route files to wrap their content.
💡 Why This Matters
🌍 Real World
Websites often have multiple pages sharing common headers, footers, or sidebars. Using layouts helps keep the design consistent and code organized.
💼 Career
Understanding routing and layouts is essential for frontend developers working with modern frameworks like Deno Fresh, Next.js, or React Router.
Progress0 / 4 steps
1
Create the home page route
Create a file routes/index.tsx with a default exported function component called HomePage that returns a <main> element containing the text "Welcome to the Home Page".
Deno
Hint
Use a React-style function component that returns JSX with a <main> tag and the exact text.
2
Create the about page route
Create a file routes/about.tsx with a default exported function component called AboutPage that returns a <main> element containing the text "About Us".
Deno
Hint
Create a new function component named AboutPage that returns a <main> tag with the exact text.
3
Create the shared layout component
Create a file components/Layout.tsx with a default exported function component called Layout that accepts a children prop and returns a <div> containing a <header> with text "Site Header", the children, and a <footer> with text "Site Footer".
Deno
Hint
Use a function component with a children prop and wrap it between header and footer elements.
4
Use the layout component in routes
Modify routes/index.tsx and routes/about.tsx to import the Layout component from components/Layout.tsx and wrap their returned <main> content inside <Layout> tags.
Deno
Hint
Import Layout and wrap the <main> content inside <Layout> tags in both route components.
Practice
(1/5)
1. What is the main purpose of routes in a Deno web application?
easy
A. To store user data in a database
B. To style the application with CSS
C. To connect URLs to specific content or pages
D. To handle server hardware settings
Solution
Step 1: Understand what routes do
Routes link a URL path to the content or page that should show when that URL is visited.
Step 2: Compare options to this definition
Only To connect URLs to specific content or pages describes connecting URLs to content, which matches the purpose of routes.
Final Answer:
To connect URLs to specific content or pages -> Option C
Quick Check:
Routes = URL to content connection [OK]
Hint: Routes map URLs to pages or content [OK]
Common Mistakes:
Confusing routes with styling or database tasks
Thinking routes manage server hardware
Mixing routes with layout responsibilities
2. Which of the following is the correct way to define a route handler in a Deno Oak application?
easy
A. router.route('/home', function() { return 'Home'; });
B. router.get('/home', (ctx) => { ctx.response.body = 'Home'; });
C. router.add('/home', (req, res) => res.send('Home'));
D. router.handle('/home', () => { print('Home'); });
Solution
Step 1: Recall Oak router syntax
In Oak, routes are defined using methods like router.get(path, handler).
Step 2: Check each option's syntax
router.get('/home', (ctx) => { ctx.response.body = 'Home'; }); uses router.get with a context parameter and sets ctx.response.body, which is correct Oak syntax.
B. Middleware wrapping layout runs after routes, so layout is applied too late
C. ctx.response.body is not set in route handler
D. Middleware wrapping layout runs after routes, so layout is not applied
Solution
Step 1: Analyze middleware order
router.routes() and router.allowedMethods() are registered before the layout middleware.
Step 2: Recall route handler behavior in Oak
When router.routes() matches a route, it executes the handler. The handler sets the response body but does not call next(), terminating the middleware chain at that point.
Step 3: Determine layout application
Subsequent middleware, including the layout wrapper, are never reached for matched routes. Thus, the layout is not applied.
Final Answer:
Middleware wrapping layout runs after routes, so layout is not applied -> Option D
Quick Check:
Middleware after router.routes() skipped on match [OK]
Hint: Layout middleware must precede router.routes() [OK]
Common Mistakes:
Placing layout middleware after routes
Forgetting to set ctx.response.body in route
Misunderstanding middleware execution order
5. You want to create a Deno Oak app with a layout that adds a header and footer to all pages, but only for routes under '/app'. Which approach correctly applies the layout only to those routes?
hard
A. Use a middleware that checks if ctx.request.url.pathname starts with '/app' and wraps response body with layout only then
B. Apply the layout middleware globally to all routes without condition
C. Add the header and footer HTML directly inside each route handler under '/app'
D. Use router.allowedMethods() to apply layout only to '/app' routes
Solution
Step 1: Understand the requirement
The layout should wrap only routes under '/app', not all routes.
Step 2: Evaluate each option
Use a middleware that checks if ctx.request.url.pathname starts with '/app' and wraps response body with layout only then uses middleware that checks the URL path and applies layout conditionally, which is correct and efficient.
Step 3: Why other options fail
Apply the layout middleware globally to all routes without condition applies layout to all routes, ignoring the condition. Add the header and footer HTML directly inside each route handler under '/app' duplicates layout code in every route, which is not DRY. Use router.allowedMethods() to apply layout only to '/app' routes misuses router.allowedMethods(), which only handles HTTP method responses.
Final Answer:
Use a middleware that checks if ctx.request.url.pathname starts with '/app' and wraps response body with layout only then -> Option A