Skip to content

fix: prevent explicit undefined values from overwriting visit defaults (#3018)#3019

Open
Vitalcheffe wants to merge 1 commit intoinertiajs:3.xfrom
Vitalcheffe:fix/undefined-visit-options-3018
Open

fix: prevent explicit undefined values from overwriting visit defaults (#3018)#3019
Vitalcheffe wants to merge 1 commit intoinertiajs:3.xfrom
Vitalcheffe:fix/undefined-visit-options-3018

Conversation

@Vitalcheffe
Copy link
Copy Markdown

Problem

When visit options contain explicit undefined values (e.g. { only: undefined }), the spread operator overwrites the default [] with undefined, causing a runtime crash:

Uncaught TypeError: Cannot read properties of undefined (reading 'length')

This happens when options are built dynamically with conditional spreads or wrapper functions that pass through undefined. TypeScript's Partial<Visit> allows this without error.

Reproduction

router.visit('/some-url', { only: undefined })
// => TypeError: Cannot read properties of undefined (reading 'length')

Fix

Added a stripUndefined helper that removes keys with undefined values from options and configuredOptions before they're spread into mergedOptions. This ensures defaults (only: [], except: [], reset: []) are preserved.

This is a minimal, surgical fix — 12 lines added, no behavior changes for correctly-formed options.

Fixes #3018

When visit options contain explicit undefined values (e.g. { only: undefined }),
the spread operator overwrites the default [] with undefined, causing a runtime
crash on .length access. This happens when options are built dynamically with
conditional spreads or wrapper functions that may pass through undefined.

Added stripUndefined helper that removes undefined keys before spreading,
ensuring defaults are preserved. This matches the expected behavior where
router.visit('/url', { only: undefined }) should behave the same as
router.visit('/url', {}).

Fixes inertiajs#3018
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.

Uncaught TypeError: Cannot read properties of undefined (reading 'length') when visit options contain explicit undefined values

1 participant