-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
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.