optimizely / react-sdk

React SDK for Optimizely Feature Experimentation and Optimizely Full Stack (legacy)
https://docs.developers.optimizely.com/experimentation/v4.0.0-full-stack/docs/javascript-react-sdk
Apache License 2.0
89 stars 36 forks source link

[BUG] useDecision with autoUpdate:false runs into error "Unable to evaluate feature "..." because User ID is not set." #234

Closed fabb closed 7 months ago

fabb commented 9 months ago

Is there an existing issue for this?

SDK Version

3.0.0

Current Behavior

I have an OptimizelyProvider in a 'use client' component that is SSRed. It receives the user directly on the initial render.

In another 'use client' component that is SSRed, I use useDecision with autoUpdate:false.

The output of useDecision is an error: "Unable to evaluate feature "..." because User ID is not set."

Expected Behavior

Since the OptimizelyProvider receives the user directly during SSR, I would expect that useDecision already contains the correct decision for that user, and the according variant can be server rendered.

Steps To Reproduce

export const CustomOptimizelyProvider = (props) => {
    const [optimizely] = useState(() =>
        createInstance({
            datafile: props.optimizelyDatafile, // this is fetched synchronously in a server component, so we already have it
        }),
    )

    return <OptimizelyProvider optimizely={optimizely} isServerSide={true} user={props.optimizelyUser} children={props.children} />
}
const Page = () => {
    const [decision] = useDecision('beast-test')
    return <div>decision: {decision}<div>
}

Might be related to https://github.com/optimizely/react-sdk/issues/185

React Framework

next.js 14.0.4

Browsers impacted

all

Link

No response

Logs

No response

Severity

Blocking development

Workaround/Solution

none found yet except for autoUpdate:true which I want to avoid since the SSR result should already be the correct variant.

Recent Change

I think it was not an issue in the previous major version, but I only tried it in another project, because the current project is new.

Conflicts

No response

mikechu-optimizely commented 9 months ago

@fabb , Thanks for the early report. I need to add e2e SSR test. Grrrr. That's on me.

I created a work ticket (FSSDK-9975) for a short turnaround.

alexparish commented 9 months ago

@mikechu-optimizely Sorry to nudge, but I don't suppose you have an ETA on this fix? It's a blocker to upgrading to v3, which itself seems to be required for the library to work with the Next.js App Router.

mikechu-optimizely commented 9 months ago

Hey. Thanks for the nudge.

This ticket got bumped for a severity 1 to keep the sprint points level, but grrr it got put into Planning instead of just bumped forward a sprint.

I've pulled it into next week's sprint start.

fabb commented 7 months ago

@mikechu-optimizely how is progress going?

mikechu-optimizely commented 7 months ago

Hi @fabb. I'm working to get #255 approved.

mikechu-optimizely commented 7 months ago

Hi again @fabb Can you review v3.1.0 in your implementation?

fabb commented 7 months ago

@mikechu-optimizely thank you, it works now correctly!

mikechu-optimizely commented 7 months ago

Woot w00t. 🎉 Thanks for the collab.