Pricing Blog

Best Practices for SSR User Preferences?

  • lunar_rei-1393605314007531595

    Rei

    1 day ago

    Hello everyone 😊
    I'm hoping to get some architectural guidance on the best way to approach state management for users and visitors.

    I'm relatively new to web development, so I would very much appreciate the help. 🙏
    My goal is to create a system for managing user preferences (theme, layout, etc.) that works seamlessly for both guests and authenticated users, with two key principles:

    1. No UI Flash: The initial page load must be server-side rendered with the user's correct preferences.
    2. Optimistic UI: When a setting is changed, the UI should update instantly.

    My initial approach for authenticated users was based entirely on formulas and variables within my app_wrapper:

    - Formulas were "exposed in context" to pass down styles and settings.
    - These formulas used a defaultTo(variable, auth_me_data) pattern. This allowed for an optimistic UI (updating the variable) while having the server-rendered auth_me_data as the default, preventing a flash for logged-in users.
    - Workflows, also "exposed in context", handled updating the variables and syncing changes to the database in the background.

    I soon realized this architecture had a major limitation: it didn't solve the UI flash for visitors, as they have no auth/me data for the server to render and variables aren't initialized at that point.

    To create one unified, streamlined system, I decided to pivot to a "Cookie-First" Approach for SSR, since cookies can be read by the server for all users. The core logic remains the same, but the cookie becomes the primary source of truth for the initial render.

    However, I've run into a roadblock trying to implement it. 😅
    1393605314552664094-Screenshot_2025-07-12_074832.png
  • lunar_rei-1393605863654162523

    Rei

    1 day ago

    My logic for syncing an authenticated user's preferences from the database to the cookie is failing:

    - The login API endpoint correctly returns an authToken and the user's display_prefs object.
    - I use the "Set session cookies" action to securely store the authToken. This works perfectly.
    - In the onSuccess event of that action, I use the built-in "Set Cookie" action, passing the Encode JSON result of my display_prefs object as the value.

    The Problem: The Set Cookie action fails silently. I've isolated this with a simple test button; the action's Success event fires every time, but the cookie never appears in the browser's developer tools.

    The AI assistant suggests a possible cause: my app is served over HTTPS, and modern browsers will often silently reject cookies if they are not marked with the Secure flag. Unsure if it's related but I do indeed have the Toddle extension enabled and I'm using Google Chrome browser. Also, I did try different naming conventions for the cookie though I'm sure that doesn't matter.

    1. Is the ai assistant correct? Is the missing Secure flag the most likely reason the browser is rejecting the cookie on an HTTPS connection?
    2. If so, is there a way to force the built-in Set Cookie action to add this flag, or is a custom JavaScript action the only solution?
    3. Are there potential GDPR implications I should be aware of with this cookie-based approach? (My plan was to have this preference cookie auto-generated on a user's first visit and classify it as "essential" for site functionality.)
    4. Looking at my overall approach, is this a sound and robust way to handle SSR preferences, or would you suggest an alternative, simpler pattern for managing this in Nordcraft?

    Again, I'm new to these concepts and I'm trying to follow best practices to lay a solid foundation for my learning and my projects. I truly appreciate any guidance you can offer. Thank you! 🙏 ☺️