daxus icon indicating copy to clipboard operation
daxus copied to clipboard

SSR support

Open jason89521 opened this issue 1 year ago • 1 comments

My current idea is to hydrate based on different accessors. The API might look like this:

const postAdapter = createPaginationAdapter({});
const postModel = createModel(initialModel);
const getPostById = postModel.defineAccessor('normal', {
    // configuration
});

function Page({ post }) {
    useHydrate(getPostById(post.id), () => {
        postModel.mutate(model => {
            postAdapter.upsertOne(model, post)
        })
    })
}

The useHydrate function will record the provided accessor and execute the function if the current accessor hasn't been recorded yet. When the accessor changes, it removes the previously recorded one and records the new one. In the example above, this ensures that when post changes, we can update the model in real-time.

However, this approach has a drawback. Developers need to place useHydrate at the top level of the component tree to manage accessors centrally and avoid duplicate accessors being passed. Additionally, attention needs to be paid to which models need to be hydrated on which pages to prevent data from being missed.

jason89521 avatar May 23 '23 10:05 jason89521

Should provide a way to create new state in server to prevent from mutating the global model's state directly in server. It would cause state pollution.

jason89521 avatar Jun 30 '23 15:06 jason89521