Performance: CSS imports per route
This affects the page load speed by controlling how much CSS is loaded initially and when navigating between routes.
import './global.css'; export function links() { return [{ rel: 'stylesheet', href: '/global.css' }]; } // In routeA.jsx import './routeA.css'; export function links() { return [{ rel: 'stylesheet', href: '/routeA.css' }]; } // In routeB.jsx import './routeB.css'; export function links() { return [{ rel: 'stylesheet', href: '/routeB.css' }]; }
import './global.css'; import './routeA.css'; import './routeB.css'; export function links() { return [ { rel: 'stylesheet', href: '/global.css' }, { rel: 'stylesheet', href: '/routeA.css' }, { rel: 'stylesheet', href: '/routeB.css' } ]; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Global CSS import for all routes | Minimal | 1 per route load | High due to large CSS | [X] Bad |
| Route-specific CSS imports | Minimal | 1 per route load | Low due to smaller CSS | [OK] Good |
links export in route files to load CSS only for that route.links is preferredlinks function that returns CSS links from the route file -> Option Alinks export [OK]links export in route files for CSS [OK]links function?links export syntaxlinks as a function returning an array of objects with rel and href.import styles from '~/styles/page.css';
export const links = () => [
{ rel: 'stylesheet', href: styles }
];styles gives the path string for the CSS file.links usagelinks function returns an array with the CSS file href set to styles, so it loads that CSS file.export function links() {
return {
rel: 'stylesheet',
href: '/styles.css'
};
}linkslinks to return an array of link objects, not a single object.links function should return an array, not an object -> Option Dlinks returns array = The links function should return an array, not an object [OK]links [OK]base.css and theme.css, only on a specific Remix route. How should you write the links function to achieve this?linkslinks function must return an array of objects, each with rel and href.links [OK]Pass the test and this pattern moves unknown