Skip to content

fix: worklets bug#243

Open
Juliusohblen wants to merge 1 commit into
mainfrom
feat/fix-worklets-bug
Open

fix: worklets bug#243
Juliusohblen wants to merge 1 commit into
mainfrom
feat/fix-worklets-bug

Conversation

@Juliusohblen

Copy link
Copy Markdown
Contributor

Summary

Problem: React Native Reanimated worklets were failing in the Next.js
browser environment because worklets require a special JavaScript
runtime that only exists in React Native, not in web browsers.

Solution: Created a custom Reanimated mock and configured webpack to use
it for web builds.

Changes Made

  1. Added dependency (apps/docs/package.json)
    - Installed react-native-reanimated@~4.1.3 in the docs app
  2. Created custom mock (apps/docs/src/lib/reanimated-mock.js)
    - Provides no-op implementations of all Reanimated hooks
    (useAnimatedStyle, useDerivedValue, withTiming, etc.)
    - Exports mock layout animations that won't throw errors
    - Uses regular React Native Web components instead of animated ones
    - Crucially: doesn't import from real Reanimated, preventing worklet
    initialization
  3. Updated webpack config (apps/docs/next.config.mjs:95)
    - Added alias: 'react-native-reanimated$': path.resolve(__dirname,
    './src/lib/reanimated-mock.js')
    - Redirects all Reanimated imports to the custom mock on web

Result

  • Worklets errors are eliminated in the browser
  • Components using Reanimated (like Accordion) render without runtime
    errors
  • Animations are gracefully disabled on web (expected behavior)
  • No impact on native React Native builds

@Juliusohblen Juliusohblen requested a review from naodya October 30, 2025 22:33
@vercel

vercel Bot commented Oct 30, 2025

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
react-native-usmds-docs Ready Ready Preview Comment Oct 30, 2025 10:35pm

clamp: NOOP,
};

const interpolateColor = {

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note documentation

Unused variable interpolateColor.

Copilot Autofix

AI 9 months ago

The best way to resolve the issue is to simply remove the unused variable declaration for interpolateColor in this file. This means deleting lines 88–93, which declare and initialize interpolateColor. This avoids wasted memory and improves code readability by removing extraneous code elements. Only lines related to the declaration and assignment of interpolateColor need to be removed; no additional imports or function changes are required.

Suggested changeset 1
apps/docs/src/lib/reanimated-mock.js

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/apps/docs/src/lib/reanimated-mock.js b/apps/docs/src/lib/reanimated-mock.js
--- a/apps/docs/src/lib/reanimated-mock.js
+++ b/apps/docs/src/lib/reanimated-mock.js
@@ -85,12 +85,6 @@
   clamp: NOOP,
 };
 
-const interpolateColor = {
-  Extrapolate: interpolation.Extrapolation,
-  Extrapolation: interpolation.Extrapolation,
-  ColorSpace: { RGB: 0, HSV: 1 },
-  interpolateColor: NOOP,
-};
 
 const Easing = {
   Easing: {
EOF
@@ -85,12 +85,6 @@
clamp: NOOP,
};

const interpolateColor = {
Extrapolate: interpolation.Extrapolation,
Extrapolation: interpolation.Extrapolation,
ColorSpace: { RGB: 0, HSV: 1 },
interpolateColor: NOOP,
};

const Easing = {
Easing: {
Copilot is powered by AI and may make mistakes. Always verify output.
interpolateColor: NOOP,
};

const Easing = {

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note documentation

Unused variable Easing.

Copilot Autofix

AI 9 months ago

To fix the problem, the unused variable Easing should simply be removed. This means deleting the declaration and initialization of Easing entirely (lines 95–115, inclusive). Since there is no evidence in the shown code that Easing is used, this can be done safely without affecting the rest of the file's functionality. No additional imports, methods, or variable definitions are required as part of the fix—just the removal of the block that defines Easing.

Suggested changeset 1
apps/docs/src/lib/reanimated-mock.js

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/apps/docs/src/lib/reanimated-mock.js b/apps/docs/src/lib/reanimated-mock.js
--- a/apps/docs/src/lib/reanimated-mock.js
+++ b/apps/docs/src/lib/reanimated-mock.js
@@ -92,28 +92,8 @@
   interpolateColor: NOOP,
 };
 
-const Easing = {
-  Easing: {
-    linear: ID,
-    ease: ID,
-    quad: ID,
-    cubic: ID,
-    poly: ID,
-    sin: ID,
-    circle: ID,
-    exp: ID,
-    elastic: ID,
-    back: ID,
-    bounce: ID,
-    bezier: () => ({ factory: ID }),
-    bezierFn: ID,
-    steps: ID,
-    in: ID,
-    out: ID,
-    inOut: ID,
-  },
-};
 
+
 const platformFunctions = {
   measure: () => ({ x: 0, y: 0, width: 0, height: 0, pageX: 0, pageY: 0 }),
   scrollTo: NOOP,
EOF
@@ -92,28 +92,8 @@
interpolateColor: NOOP,
};

const Easing = {
Easing: {
linear: ID,
ease: ID,
quad: ID,
cubic: ID,
poly: ID,
sin: ID,
circle: ID,
exp: ID,
elastic: ID,
back: ID,
bounce: ID,
bezier: () => ({ factory: ID }),
bezierFn: ID,
steps: ID,
in: ID,
out: ID,
inOut: ID,
},
};


const platformFunctions = {
measure: () => ({ x: 0, y: 0, width: 0, height: 0, pageX: 0, pageY: 0 }),
scrollTo: NOOP,
Copilot is powered by AI and may make mistakes. Always verify output.
},
};

const platformFunctions = {

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note documentation

Unused variable platformFunctions.

Copilot Autofix

AI 9 months ago

To fix the issue, we should remove the unused variable declaration for platformFunctions in apps/docs/src/lib/reanimated-mock.js. Delete lines 117-120, which declare and initialize the platformFunctions object. There is no indication it is used elsewhere in the file, and removing it should have no effect on functionality.


Suggested changeset 1
apps/docs/src/lib/reanimated-mock.js

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/apps/docs/src/lib/reanimated-mock.js b/apps/docs/src/lib/reanimated-mock.js
--- a/apps/docs/src/lib/reanimated-mock.js
+++ b/apps/docs/src/lib/reanimated-mock.js
@@ -114,11 +114,8 @@
   },
 };
 
-const platformFunctions = {
-  measure: () => ({ x: 0, y: 0, width: 0, height: 0, pageX: 0, pageY: 0 }),
-  scrollTo: NOOP,
-};
 
+
 const Colors = {
   processColor: (color) => color,
 };
EOF
@@ -114,11 +114,8 @@
},
};

const platformFunctions = {
measure: () => ({ x: 0, y: 0, width: 0, height: 0, pageX: 0, pageY: 0 }),
scrollTo: NOOP,
};


const Colors = {
processColor: (color) => color,
};
Copilot is powered by AI and may make mistakes. Always verify output.
scrollTo: NOOP,
};

const Colors = {

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note documentation

Unused variable Colors.

Copilot Autofix

AI 9 months ago

To fix this issue, the unused variable Colors should be removed from the code. Specifically, the entire definition starting from const Colors = { ... } on line 122 through line 124 should be deleted. No additional imports, methods, or variable definitions are required, and no other code depends on Colors as per the information given. This change should be limited strictly to the provided snippet and not alter existing functionality.


Suggested changeset 1
apps/docs/src/lib/reanimated-mock.js

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/apps/docs/src/lib/reanimated-mock.js b/apps/docs/src/lib/reanimated-mock.js
--- a/apps/docs/src/lib/reanimated-mock.js
+++ b/apps/docs/src/lib/reanimated-mock.js
@@ -119,10 +119,8 @@
   scrollTo: NOOP,
 };
 
-const Colors = {
-  processColor: (color) => color,
-};
 
+
 class BaseAnimationMock {
   duration() { return this; }
   delay() { return this; }
EOF
@@ -119,10 +119,8 @@
scrollTo: NOOP,
};

const Colors = {
processColor: (color) => color,
};


class BaseAnimationMock {
duration() { return this; }
delay() { return this; }
Copilot is powered by AI and may make mistakes. Always verify output.

const LayoutAnimationConfig = ({ children }) => children;

const commonTypes = {

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note documentation

Unused variable commonTypes.

Copilot Autofix

AI 9 months ago

To fix the unused variable error, we should remove the definition of commonTypes entirely from the file. The definition lies on lines 257 to 263 inclusive. Simply delete these lines. There is no need to replace uses, update imports, or preserve the object, since it is unused.


Suggested changeset 1
apps/docs/src/lib/reanimated-mock.js

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/apps/docs/src/lib/reanimated-mock.js b/apps/docs/src/lib/reanimated-mock.js
--- a/apps/docs/src/lib/reanimated-mock.js
+++ b/apps/docs/src/lib/reanimated-mock.js
@@ -254,14 +254,8 @@
 
 const LayoutAnimationConfig = ({ children }) => children;
 
-const commonTypes = {
-  SensorType: { ACCELEROMETER: 1, GYROSCOPE: 2, GRAVITY: 3, MAGNETIC_FIELD: 4, ROTATION: 5 },
-  IOSReferenceFrame: { XArbitraryZVertical: 0, XArbitraryCorrectedZVertical: 1, XMagneticNorthZVertical: 2, XTrueNorthZVertical: 3 },
-  InterfaceOrientation: { ROTATION_0: 0, ROTATION_90: 90, ROTATION_180: 180, ROTATION_270: 270 },
-  KeyboardState: { UNKNOWN: 0, OPENING: 1, OPEN: 2, CLOSING: 3, CLOSED: 4 },
-  ReduceMotion: { System: 0, Always: 1, Never: 2 },
-};
 
+
 const jestUtils = {
   withReanimatedTimer: (callback) => callback(),
   advanceAnimationByTime: NOOP,
EOF
@@ -254,14 +254,8 @@

const LayoutAnimationConfig = ({ children }) => children;

const commonTypes = {
SensorType: { ACCELEROMETER: 1, GYROSCOPE: 2, GRAVITY: 3, MAGNETIC_FIELD: 4, ROTATION: 5 },
IOSReferenceFrame: { XArbitraryZVertical: 0, XArbitraryCorrectedZVertical: 1, XMagneticNorthZVertical: 2, XTrueNorthZVertical: 3 },
InterfaceOrientation: { ROTATION_0: 0, ROTATION_90: 90, ROTATION_180: 180, ROTATION_270: 270 },
KeyboardState: { UNKNOWN: 0, OPENING: 1, OPEN: 2, CLOSING: 3, CLOSED: 4 },
ReduceMotion: { System: 0, Always: 1, Never: 2 },
};


const jestUtils = {
withReanimatedTimer: (callback) => callback(),
advanceAnimationByTime: NOOP,
Copilot is powered by AI and may make mistakes. Always verify output.
ReduceMotion: { System: 0, Always: 1, Never: 2 },
};

const jestUtils = {

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note documentation

Unused variable jestUtils.

Copilot Autofix

AI 9 months ago

The best way to fix this problem is to remove the unused variable jestUtils, which consists of its declaration at line 265-271:

265: const jestUtils = {
266:   withReanimatedTimer: (callback) => callback(),
267:   advanceAnimationByTime: NOOP,
268:   advanceAnimationByFrame: NOOP,
269:   setUpTests: NOOP,
270:   getAnimatedStyle: () => ({}),
271: };

No other code depends on this variable within the given snippet. Therefore, delete lines 265-271. No imports, methods, or definitions are needed for the fix.


Suggested changeset 1
apps/docs/src/lib/reanimated-mock.js

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/apps/docs/src/lib/reanimated-mock.js b/apps/docs/src/lib/reanimated-mock.js
--- a/apps/docs/src/lib/reanimated-mock.js
+++ b/apps/docs/src/lib/reanimated-mock.js
@@ -262,14 +262,8 @@
   ReduceMotion: { System: 0, Always: 1, Never: 2 },
 };
 
-const jestUtils = {
-  withReanimatedTimer: (callback) => callback(),
-  advanceAnimationByTime: NOOP,
-  advanceAnimationByFrame: NOOP,
-  setUpTests: NOOP,
-  getAnimatedStyle: () => ({}),
-};
 
+
 const Animated = {
   View: ViewRN,
   Text: TextRN,
EOF
@@ -262,14 +262,8 @@
ReduceMotion: { System: 0, Always: 1, Never: 2 },
};

const jestUtils = {
withReanimatedTimer: (callback) => callback(),
advanceAnimationByTime: NOOP,
advanceAnimationByFrame: NOOP,
setUpTests: NOOP,
getAnimatedStyle: () => ({}),
};


const Animated = {
View: ViewRN,
Text: TextRN,
Copilot is powered by AI and may make mistakes. Always verify output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants