Skip to content

Automatic ID generation does not work well with server rendering #1761

@lemonmade

Description

@lemonmade

Issue summary

In the latest edition of "mistakes Chris made several years ago", it's been bugging me that the approach to doing automatic ID generation in Polaris doesn't work properly for server rendering. I think all cases that need an ID generate one using createUniqueIDFactory. This creates auto-incrementing IDs (they aren't guaranteed globally unique, but they're fine for our purposes). However, they rely on module-global variables, which means that each request a node server gets will increment those IDs, even though the client will always start them back at 0. This creates a mismatch between client and server markup (not a huge deal, but an annoying warning in dev at the very least).

My proposal for addressing this would be to allow passing some sort of idFactory in to Polaris via context. This idFactory could default to being module global (e.g., const IdContext = createContext(idFactory())), which preserves the 0-config behaviour. However, for our apps, we could provide a fresh idFactory for each server render, allowing it to remain consistent with the eventual client markup.

Two things that would be nice to see out of it:

  • Make it a shared package so we could use it in other React components
  • Make sure it can be reset; our server rendering approach relies on rendering the whole app multiple times, so we need the ability to reset the counter between each pass.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions