Skip to content

Conversation

@azzzy
Copy link

@azzzy azzzy commented Dec 10, 2025

Problem

The Router's CloudFront function uses simple startsWith() matching for paths, causing incorrect route matching when paths share a common prefix.

For example:

  • Route /travel-plan incorrectly matches /travel-plans
  • Route /api incorrectly matches /api-docs

Solution

Updated the path matching logic to check for proper path segment boundaries. A path now only matches if:

  • The URI exactly equals the path, OR
  • The character after the matched prefix is /, OR
  • The route is the root path /

Changes

  • Modified CF_ROUTER_INJECTION in platform/src/components/aws/router.ts (line ~1541)
  • Added unit tests in platform/test/components/router-path-matching.test.ts

Testing

The test file duplicates the path matching logic rather than importing it because the actual implementation lives inside a CloudFront function string literal. This allows testing the logic in isolation without complex string parsing.

Verified with live CloudFront deployment:

  • /travel-plans correctly does NOT match route /travel-plan
  • /travel-plan/abc correctly matches route /travel-plan
  • All existing routes continue to work as expected

Breaking Changes

None. This is a bug fix that corrects unexpected behavior.

Route paths now only match at proper segment boundaries.
Previously /travel-plan incorrectly matched /travel-plans.

Changes:
- Updated path matching logic to check for exact match or trailing slash
- Added unit tests for path matching edge cases
@vimtor vimtor self-assigned this Jan 9, 2026
@vimtor
Copy link
Collaborator

vimtor commented Jan 11, 2026

thanks for the contribution @azzzy

i'm trying to reproduce this with a nextjs site but i don't see the issue:

CleanShot 2026-01-11 at 17 52 58@2x CleanShot 2026-01-11 at 17 52 45@2x

is there an specific use case where this fails?

@azzzy
Copy link
Author

azzzy commented Jan 13, 2026

@vimtor , are you sure this isn't just Next.js client routing taking over?
If you do a hard refresh on /travel-plans is it working still?

@azzzy
Copy link
Author

azzzy commented Jan 13, 2026

@vimtor , I've created a minimal reproduction repo: https://github.com/azzzy/sst-routing-bug-reproduction
The setup:

  • A Lambda function at /travel-plan
  • A StaticSite (React SPA) on the same domain

To reproduce:
Open the SPA, click on the Travel plans link and once on the /travel-plans route hard refresh (Cmd+Shift+R / Ctrl+Shift+R)
The bug: /travel-plans incorrectly routes to the /travel-plan Lambda handler instead of the React site.

I have it deployed here - https://sst-routing-pr.sourcelab.dev

Copy link
Collaborator

@vimtor vimtor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approved!

thanks for the reproduction @azzzy

proof of it working spa-home spa-travel-plan spa-travel-plans

i also tried different examples like the aws-nextjs one to ensure this doesn't break existing routing:

nextjs-home nextjs-travel-info nextjs-travel-plan nextjs-travel

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