UNPKG

6.29 kB Markdown View Raw
1---
2title: Progressive Enhancement
3---
4
5# Progressive Enhancement
6
7[MODES: framework]
8
9> Progressive enhancement is a strategy in web design that puts emphasis on web content first, allowing everyone to access the basic content and functionality of a web page, whilst users with additional browser features or faster Internet access receive the enhanced version instead.
10>
11> <cite>- [Wikipedia][wikipedia]</cite>
12
13When using React Router with Server-Side Rendering (the default in framework mode), you can automatically leverage the benefits of progressive enhancement.
14
15## Why Progressive Enhancement Matters
16
17Coined in 2003 by Steven Champeon & Nick Finck, the phrase emerged during a time of varied CSS and JavaScript support across different browsers, with many users actually browsing the web with JavaScript disabled.
18
19Today, we are fortunate to develop for a much more consistent web and where the majority of users have JavaScript enabled.
20
21However, we still believe in the core principles of progressive enhancement in React Router. It leads to fast and resilient apps with simple development workflows.
22
23**Performance**: While it's easy to think that only 5% of your users have slow connections, the reality is that 100% of your users have slow connections 5% of the time.
24
25**Resilience**: Everybody has JavaScript disabled until it's loaded.
26
27**Simplicity**: Building your apps in a progressively enhanced way with React Router is actually simpler than building a traditional SPA.
28
29## Performance
30
31Server rendering allows your app to do more things in parallel than a typical [Single Page App (SPA)][spa], making the initial loading experience and subsequent navigations faster.
32
33Typical SPAs send a blank document and only start doing work when JavaScript has loaded:
34
35```
36HTML |---|
37JavaScript |---------|
38Data |---------------|
39 page rendered 👆
40```
41
42A React Router app can start doing work the moment the request hits the server and stream the response so that the browser can start downloading JavaScript, other assets, and data in parallel:
43
44```
45 👇 first byte
46HTML |---|-----------|
47JavaScript |---------|
48Data |---------------|
49 page rendered 👆
50```
51
52## Resilience and Accessibility
53
54While your users probably don't browse the web with JavaScript disabled, everybody uses the websites without JavaScript before it finishes loading. React Router embraces progressive enhancement by building on top of HTML, allowing you to build your app in a way that works without JavaScript, and then layer on JavaScript to enhance the experience.
55
56The simplest case is a `<Link to="/account">`. These render an `<a href="/account">` tag that works without JavaScript. When JavaScript loads, React Router will intercept clicks and handle the navigation with client side routing. This gives you more control over the UX instead of just spinning favicons in the browser tab--but it works either way.
57
58Now consider a simple add to cart button:
59
60```tsx
61export function AddToCart({ id }) {
62 return (
63 <Form method="post" action="/add-to-cart">
64 <input type="hidden" name="id" value={id} />
65 <button type="submit">Add To Cart</button>
66 </Form>
67 );
68}
69```
70
71Whether JavaScript has loaded or not doesn't matter, this button will add the product to the cart.
72
73When JavaScript loads, React Router will intercept the form submission and handle it client side. This allows you to add your own pending UI, or other client side behavior.
74
75## Simplicity
76
77When you start to rely on basic features of the web like HTML and URLs, you will find that you reach for client side state and state management much less.
78
79Consider the button from before, with no fundamental change to the code, we can pepper in some client side behavior:
80
81```tsx lines=[1,4,7,10-12,14]
82import { useFetcher } from "react-router";
83
84export function AddToCart({ id }) {
85 const fetcher = useFetcher();
86
87 return (
88 <fetcher.Form method="post" action="/add-to-cart">
89 <input name="id" value={id} />
90 <button type="submit">
91 {fetcher.state === "submitting"
92 ? "Adding..."
93 : "Add To Cart"}
94 </button>
95 </fetcher.Form>
96 );
97}
98```
99
100This feature continues to work the very same as it did before when JavaScript is loading, but once JavaScript loads:
101
102- `useFetcher` no longer causes a navigation like `<Form>` does, so the user can stay on the same page and keep shopping
103- The app code determines the pending UI instead of spinning favicons in the browser
104
105It's not about building it two different ways–once for JavaScript and once without–it's about building it in iterations. Start with the simplest version of the feature and ship it; then iterate to an enhanced user experience.
106
107Not only will the user get a progressively enhanced experience, but the app developer gets to "progressively enhance" the UI without changing the fundamental design of the feature.
108
109Another example where progressive enhancement leads to simplicity is with the URL. When you start with a URL, you don't need to worry about client side state management. You can just use the URL as the source of truth for the UI.
110
111```tsx
112export function SearchBox() {
113 return (
114 <Form method="get" action="/search">
115 <input type="search" name="query" />
116 <SearchIcon />
117 </Form>
118 );
119}
120```
121
122This component doesn't need any state management. It just renders a form that submits to `/search`. When JavaScript loads, React Router will intercept the form submission and handle it client side. Here's the next iteration:
123
124```tsx lines=[1,4-6,11]
125import { useNavigation } from "react-router";
126
127export function SearchBox() {
128 const navigation = useNavigation();
129 const isSearching =
130 navigation.location.pathname === "/search";
131
132 return (
133 <Form method="get" action="/search">
134 <input type="search" name="query" />
135 {isSearching ? <Spinner /> : <SearchIcon />}
136 </Form>
137 );
138}
139```
140
141No fundamental change in architecture, simply a progressive enhancement for both the user and the code.
142
143See also: [State Management][state_management]
144
145[wikipedia]: https://en.wikipedia.org/wiki/Progressive_enhancement
146[spa]: ../how-to/spa
147[state_management]: ./state-management
148
\No newline at end of file