Reader small image

You're reading from  Full Stack Web Development with Remix

Product typeBook
Published inNov 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781801075299
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Andre Landgraf
Andre Landgraf
author image
Andre Landgraf

Andre is a full stack developer from Germany. He graduated with an MS in Information Systems from the Technical University of Munich and was also awarded an MS in Computer Science from Sofia University in Palo Alto. Andre currently lives in Cupertino, California, and he works as a Software Engineer at LinkedIn. Andre loves learning, writing, and speaking about all things web. In his free time, he tutors aspiring developers and builds for the web.
Read more about Andre Landgraf

Right arrow

Advanced Session Management

Session management is crucial to building good user experiences. Persisting session data can increase the user’s experience and productivity by remembering user settings, selections, and preferences.

We learned how to manage user sessions in Chapter 8, Session Management. In this chapter, we will work on advanced session management patterns.

This chapter is split into two sections:

  • Managing visitor sessions
  • Implementing pagination

First, we will implement visitor sessions and use Remix’s cookie helper to redirect the user to the right page after login or signup. Next, we will learn how to add pagination with Remix and Prisma. We will practice pagination by applying it to the expense and invoice lists in BeeRich.

After reading this chapter, you will know how to use cookies to persist arbitrary session data in Remix. You will also understand the difference between Remix’s session cookie and cookie helpers. Additionally...

Technical requirements

You can find the code for this chapter here: https://github.com/PacktPublishing/Full-Stack-Web-Development-with-Remix/tree/main/15-advanced-session-management. No extra setup is required for this chapter.

Managing visitor sessions

In Chapter 8, Session Management, we used Remix’s session cookie helpers to implement a login and signup flow. In this section, we will use Remix’s cookie helper to persist additional session data.

You might remember from Chapter 8, Session Management, that cookies are added on the server to an HTTP response using the Set-Cookie header. Once received, the browser attaches the cookie to all subsequent HTTP requests using the Cookie header.

In Remix, we can access incoming HTTP requests in our loader and action functions. In our loaders and actions, we can use Remix’s cookie helper functions to parse the cookie data from the request headers and use it to improve the user experience.

In BeeRich, we already utilize a cookie to handle the authentication of our users. However, there are plenty of other use cases for cookies.

Consider the following advanced use case: we aim to offer visitors a taste of our app’s functionality...

Implementing pagination

Pagination is an important pattern when working with large and user-generated lists of objects. Pagination divides the content into separate pages and thereby limits the number of objects that must be loaded for a given page. Pagination aims to reduce load times and improve performance.

In this section, we will implement pagination in BeeRich for expenses and invoices:

  1. First, open the dashboard.expenses.tsx route module and define a constant for the page size:
    const PAGE_SIZE = 10;

    The page size defines the number of expenses we will show at once in the expenses overview list. To see more expenses, the user has to navigate to the next page.

  2. Update the loader function in dashboard.expenses.tsx and access a new search parameter named page:
    const userId = await requireUserId(request);const url = new URL(request.url);const searchString = url.searchParams.get('q');const pageNumberString = url.searchParams.get('page');const pageNumber...

Summary

In this chapter, you learned about advanced session management patterns with Remix and wrapped up your work on BeeRich.

Remix offers a createCookie helper function for working with cookie data. The function returns a cookie abstraction for parsing and serializing cookie data to and from request headers.

After reading this chapter, you know how to use createCookie to store and access arbitrary user session data in a cookie. You practiced working with cookies by adding a visitor cookie to the login and signup flow in BeeRich that persists the URL the visitor wants to access.

You also learned how to implement a simple pagination feature with Remix and Prisma. Pagination is a pattern that can improve performance and avoid long loading times when working with data lists. Utilizing pagination limits the amount of data that needs to be fetched for each page load.

In the next chapter, we will learn more about deploying Remix applications on the edge.

Further reading

You can find more information about working with search parameters via MDN Web Docs: https://developer.mozilla.org/en-US/docs/Web/API/URL/searchParams.

You can reference the Remix documentation to learn more about the createCookie helper function: https://remix.run/docs/en/2/utils/cookies.

You can read more about pagination with Prisma in the Prisma documentation: https://www.prisma.io/docs/concepts/components/prisma-client/pagination.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Full Stack Web Development with Remix
Published in: Nov 2023Publisher: PacktISBN-13: 9781801075299
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime

Author (1)

author image
Andre Landgraf

Andre is a full stack developer from Germany. He graduated with an MS in Information Systems from the Technical University of Munich and was also awarded an MS in Computer Science from Sofia University in Palo Alto. Andre currently lives in Cupertino, California, and he works as a Software Engineer at LinkedIn. Andre loves learning, writing, and speaking about all things web. In his free time, he tutors aspiring developers and builds for the web.
Read more about Andre Landgraf