You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**This article was last updated on August 07, 2024, to add sections on Security Considerations and SWR.**
12
+
11
13
## Overview
12
14
13
15
Delivering flawless user experience and structured data retrieval is of the most important when it comes to web development. Next.js, a robust React framework, has become the go-to choice for developers seeking to optimize server-side rendering (SSR) and client-side rendering (CSR) capabilities. One crucial aspect that sets Next.js apart is its comprehensive toolkit for data fetching and server actions.
@@ -291,6 +293,195 @@ In the code above, we define a component `MyComponent` that fetches users from t
291
293
Note that when making requests to API routes from the client-side code, you can use relative URLs like `/api/users` since Next.js automatically routes requests to the appropriate API route.
292
294
Next.js API routes provide a powerful mechanism to handle server actions seamlessly within your application. Whether you need to fetch data, perform data mutations, or handle authentication, API routes enable you to define custom server endpoints with ease.
293
295
296
+
## Bonus: Securing API Routes in Our Next.js App
297
+
298
+
Just wanted to share some key practices in making sure our Next.js application is secure, especially with regard to the server actions and API routes.
299
+
300
+
### Protecting API Endpoints
301
+
302
+
First, we need to ensure that API routes are kept safe from unauthorized access. Here's what we can do:
303
+
304
+
#### JWT Authentication
305
+
306
+
We could authenticate users through JSON Web Tokens (JWT). When a user logs in, we will issue a token for them to send along with their requests.
## Bonus: Using SWR to Fetch Data Efficiently with Next.js
400
+
401
+
This will be the time when I will introduce you to one of the greatest data-fetching tools for our Next.js projects: SWR. Using SWR, we manage data fetching efficiently, ensuring that the UI stays fast and responsive while new data is fetched.
402
+
403
+
### SWR (Stale-While-Revalidate)
404
+
405
+
#### Introduction to SWR
406
+
407
+
SWR is a React Hooks library for remote data fetching. When working with this library, fetching data can be made really simple and assured that it is up-to-date with live data at any time. The acronym SWR stands for "Stale-While-Revalidate": a strategy in which the component exhibits stale data while it continues to revalidate in the background.
408
+
409
+
#### SWR for Data Fetching
410
+
411
+
Below is a simple example of how we might use SWR in our components.
Here the function of `useSWR` is to take a key (the URL to fetch) and a fetcher function: it will automatically handle the fetch, error, and revalidation process.
-**Automatic Caching and Revalidation**: SWR, in the background, caches the data and automatically revalidates it, making the UI fast, live, and always updated.
482
+
-**Simple focus**: Abstract away fetching data; make our code more clean and maintainable.
483
+
-**Error Handling**: SWR has error handling built-in, so it's fairly trivial to deal with data fetching failures.
484
+
294
485
## Conclusion
295
486
296
487
We explored the basics of Next.js, simplified setup process, and advantages. We delved into data fetching, showcasing both client-side and server-side techniques with code examples. Additionally, we explored the concept of server actions using Next.js API routes, enabling custom server endpoints for handling server-side logic and operations.
0 commit comments