Yjs currently has an experimental feature: Y.PermanentUserData, which can be used to meet the above requirements.
However, since it is currently in the experimental stage and the author said that it will provide a better implementation in the future, it is recommended to refer to its implementation and introduce it carefully in production.