Skip to content

Commit

Permalink
Implement pre-fetching strategies for improved performance
Browse files Browse the repository at this point in the history
- Add Webpack magic comments for component pre-fetching
- Create PreFetchLink component for interaction-based pre-fetching
- Update App.js to utilize new pre-fetching methods
  • Loading branch information
Violet-Bora-Lee committed Aug 16, 2024
1 parent 7dac277 commit f838d69
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import React, { Suspense, lazy } from 'react';
import { BrowserRouter as Router, Route, Link, Routes } from 'react-router-dom';

const Home = lazy(() => import('./components/Home'));
const Dashboard = lazy(() => import('./components/Dashboard'));
const Home = lazy(() => import(/* webpackPrefetch: true */ './components/Home'));
const Dashboard = lazy(() => import(/* webpackPrefetch: true */ './components/Dashboard'));
const Profile = lazy(() => import('./components/Profile'));
const Settings = lazy(() => import('./components/Settings'));
const AdvancedFeatures = lazy(() => import('./components/AdvancedFeatures'));
Expand All @@ -15,11 +15,41 @@ function App() {
<div>
<nav>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/dashboard">Dashboard</Link></li>
<li><Link to="/profile">Profile</Link></li>
<li><Link to="/settings">Settings</Link></li>
<li><Link to="/advanced">Advanced Features</Link></li>
<li>
<PreFetchLink
to="/"
prefetch={() => import('./components/Home')}>
Home
</PreFetchLink>
</li>
<li>
<PreFetchLink
to="/dashboard"
prefetch={() => import('./components/Dashboard')}>
Dashboard
</PreFetchLink>
</li>
<li>
<PreFetchLink
to="/profile"
prefetch={() => import('./components/Profile')}>
Profile
</PreFetchLink>
</li>
<li>
<PreFetchLink
to="/settings"
prefetch={() => import('./components/Settings')}>
Settings
</PreFetchLink>
</li>
<li>
<PreFetchLink
to="/advanced" p
refetch={() => import('./components/AdvancedFeatures')}>
Advanced Features
</PreFetchLink>
</li>
</ul>
</nav>

Expand Down
19 changes: 19 additions & 0 deletions src/components/PreFetchLink.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// components/PreFetchLink.js
import React from 'react';
import { Link } from 'react-router-dom';

const PreFetchLink = ({ to, children, prefetch }) => {
const prefetchComponent = () => {
if (prefetch) {
prefetch();
}
};

return (
<Link to={to} onMouseEnter={prefetchComponent} onFocus={prefetchComponent}>
{children}
</Link>
);
};

export default PreFetchLink;

0 comments on commit f838d69

Please sign in to comment.