Skip to content

Make optional route segments resolvable - #351

Open
sadiqk2 wants to merge 1 commit into
NativePHP:mainfrom
sadiqk2:fix/optional-route-segments
Open

Make optional route segments resolvable#351
sadiqk2 wants to merge 1 commit into
NativePHP:mainfrom
sadiqk2:fix/optional-route-segments

Conversation

@sadiqk2

@sadiqk2 sadiqk2 commented Aug 17, 2026

Copy link
Copy Markdown

The bug

Route::native('/posts/{slug?}', PostScreen::class);

This route cannot match any URI at all — not /posts/hello, not /posts.

NativeRouter::resolve() compiles its pattern with preg_replace('/\{(\w+)\}/', '(?P<$1>[^/]+)', $pattern), and \w does not match ?. The {slug?} placeholder therefore survives into the regex verbatim, so the compiled pattern only ever matches the literal string /posts/{slug?}.

What turns a dead pattern into a device-visible failure: BootPlanner on both Android and iOS matches these patterns when it decides whether to boot native. An app using the documented optional syntax boots into the native runloop and then resolves to no screen — a blank native window instead of the WebView fallback that would otherwise have carried it.

The fix

  • Optional segments are substituted first, and the pattern consumes the leading slash with them (#/\{(\w+)\?\}#(?:/(?P<$1>[^/]+))?), so the segment and its separator disappear together and /posts matches.
  • An omitted segment is dropped from params rather than reported as ''. A screen checking whether it received a parameter should not be told yes for a segment nobody sent. [^/]+ cannot match empty, so no legitimate value is filtered.

Required segments are unaffected.

Verification

New tests/Unit/Edge/NativeRouterOptionalParamsTest.php — 6 cases: segment present, segment omitted, isNativeRoute() agreeing with resolve() for both (the BootPlanner half), required segments still required, an optional segment after a required one, and an optional segment not swallowing a deeper path.

4 of the 6 fail against main. Full suite: 887 passed, and the 13 failures are identical to main's baseline (Android splash-screen and release-build tests, unrelated to this change). vendor/bin/pint --test clean.

`Route::native('/posts/{slug?}', PostScreen::class)` could not match any URI at
all — not `/posts/hello`, not `/posts`.

resolve() builds its regex with `preg_replace('/\{(\w+)\}/', ...)`, and `\w` does
not match `?`. So a `{slug?}` placeholder was left in the pattern verbatim and the
compiled regex could only ever match the literal string `/posts/{slug?}`.

What makes it more than a dead pattern is that BootPlanner on both platforms
matches these routes happily when deciding whether to boot native. An app using
the documented optional syntax therefore launched into the native runloop and then
resolved to no screen — a blank native window rather than a fallback to the
WebView.

Optional segments are now substituted first, consuming their leading slash so the
segment and its separator disappear together, and an omitted one is dropped from
the parameters rather than reported as an empty string — a screen asking whether
it has a parameter should not be told yes for a segment nobody sent. `[^/]+`
cannot match empty, so no legitimate value is filtered out.
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.

1 participant