📖 Scenario: You are building a simple website with navigation links. To make the site faster, you want to prefetch the data for pages when users hover over the links. This way, the next page loads quickly.
🎯 Goal: Create a Remix component with navigation links that use prefetch="intent" to prefetch page data when the user shows intent to visit the link.
📋 What You'll Learn
Create a Remix component with at least two navigation links
Use the Link component from Remix
Add prefetch="intent" attribute to each Link
Ensure the links navigate to /about and /contact routes
💡 Why This Matters
🌍 Real World
Prefetching links is a common technique in web apps to make navigation feel faster and smoother by loading data before the user clicks.
💼 Career
Understanding prefetching in Remix helps you build performant React apps and improves user experience, a valuable skill for frontend developers.
Progress0 / 4 steps
1
Create navigation links
Create a Remix component called Navigation that returns a nav element containing two Link components from @remix-run/react. The first Link should have to="/about" and display the text About. The second Link should have to="/contact" and display the text Contact.
Remix
Hint
Use the Link component from Remix and set the to attribute exactly as /about and /contact.
2
Add prefetch attribute
Add the attribute prefetch="intent" to both Link components inside the Navigation component to enable prefetching when the user shows intent to visit the link.
Remix
Hint
Add prefetch="intent" inside the opening tag of each Link.
3
Add aria-labels for accessibility
Add aria-label attributes to both Link components. For the About link, use aria-label="Go to About page". For the Contact link, use aria-label="Go to Contact page".
Remix
Hint
Add aria-label inside each Link tag with the exact text provided.
4
Add simple styling with flex layout
Add a style attribute to the nav element with the value {{ display: 'flex', gap: '1rem' }} to space the links horizontally with some gap.
Remix
Hint
Add the style attribute exactly as {{ display: 'flex', gap: '1rem' }} to the nav element.
Practice
(1/5)
1. What does the prefetch attribute do when used on a Remix Link component?
easy
A. It automatically redirects the user to the linked page on page load.
B. It disables the link until the page is fully loaded.
C. It changes the link's color when hovered.
D. It loads the linked page's data before the user clicks the link to speed up navigation.
Solution
Step 1: Understand the purpose of prefetching in Remix
Prefetching is used to load data or resources ahead of time to make navigation faster.
Step 2: Identify what the prefetch attribute does on a Link
It triggers loading of the linked page's data before the user clicks, improving speed.
Final Answer:
It loads the linked page's data before the user clicks the link to speed up navigation. -> Option D
Quick Check:
Prefetching = Load data early [OK]
Hint: Prefetch loads data early to speed up clicks [OK]
Common Mistakes:
Thinking prefetch disables links
Confusing prefetch with styling effects
Assuming prefetch causes automatic redirects
2. Which of the following is the correct way to prefetch a link on hover using Remix's Link component?
easy
A. <Link to='/about' prefetch='intent'>About</Link>
B. <Link to='/about' prefetch='hover'>About</Link>
C. <Link to='/about' prefetch='render'>About</Link>
D. <Link to='/about' prefetch='click'>About</Link>
Solution
Step 1: Recall valid prefetch values in Remix
Remix supports 'intent' (hover/focus) and 'render' (immediate) for prefetch.
Step 2: Match the value for hover prefetching
'intent' triggers prefetch on hover or focus, so it is correct.
Final Answer:
<Link to='/about' prefetch='intent'>About</Link> -> Option A
Quick Check:
Hover prefetch = 'intent' [OK]
Hint: 'intent' means hover/focus prefetch [OK]
Common Mistakes:
Using 'hover' instead of 'intent' as prefetch value
Confusing 'render' with hover prefetch
Using invalid values like 'click'
3. Given the code snippet below, what will happen when the page loads?
A. The prefetch value 'hovering' is invalid; it should be 'intent'.
B. The 'to' attribute must be 'href' instead.
C. The Link component cannot use prefetch attribute.
D. The link text 'Profile' must be inside a <span>.
Solution
Step 1: Check valid prefetch attribute values
Remix accepts 'intent' or 'render' as valid prefetch values, not 'hovering'.
Step 2: Identify the incorrect value usage
The code uses 'hovering', which is invalid and causes no prefetch.
Final Answer:
The prefetch value 'hovering' is invalid; it should be 'intent'. -> Option A
Quick Check:
Valid prefetch values = 'intent' or 'render' [OK]
Hint: Use only 'intent' or 'render' for prefetch [OK]
Common Mistakes:
Using non-existent prefetch values
Confusing 'to' with 'href' in Remix Link
Thinking prefetch attribute is unsupported
5. You want to optimize your Remix app so that the home page prefetches the 'settings' page data only when the user is likely to click it (hover or focus), but the 'help' page data should load immediately on page render. Which is the correct way to set prefetch attributes on these links?
hard
A. <Link to='/settings' prefetch='render'>Settings</Link> <Link to='/help' prefetch='intent'>Help</Link>
B. <Link to='/settings' prefetch='hover'>Settings</Link> <Link to='/help' prefetch='render'>Help</Link>
C. <Link to='/settings' prefetch='intent'>Settings</Link> <Link to='/help' prefetch='render'>Help</Link>
D. <Link to='/settings' prefetch='click'>Settings</Link> <Link to='/help' prefetch='render'>Help</Link>
Solution
Step 1: Match prefetch types to desired behavior
'intent' prefetches on hover/focus, 'render' prefetches immediately on page load.
Step 2: Assign correct prefetch values to each link
Settings should use 'intent' and Help should use 'render' as per requirements.
Final Answer:
<Link to='/settings' prefetch='intent'>Settings</Link> <Link to='/help' prefetch='render'>Help</Link> -> Option C