Skip to content

Conversation

tusharpandey13
Copy link
Contributor

@tusharpandey13 tusharpandey13 commented Mar 7, 2025

Fix route matching when Next.js trailingSlash is enabled (#1938)

Fixes #1938

Problem

When using trailingSlash: true in next.config.js, the SDK's authentication routes (login, logout, etc.) returned 404 errors due to strict path comparisons that didn't account for trailing slashes.

Solution

Normalize pathnames before route matching:

  • Added removeTrailingSlash utility to consistently handle paths
  • Centralized path utilities in pathUtils.ts for better maintainability
  • Updated route handling to compare sanitized paths

Key Changes

  1. Path normalization: Removes trailing slashes from incoming request paths before matching against configured routes
  2. Code organization: Moved path utilities to dedicated module
  3. Backwards compatibility: Maintains existing URL construction behavior while improving route matching reliability

This ensures routes work correctly regardless of Next.js' trailingSlash configuration, without breaking existing implementations.

@tusharpandey13 tusharpandey13 requested a review from a team as a code owner March 7, 2025 16:09
@codecov-commenter
Copy link

codecov-commenter commented Mar 7, 2025

Codecov Report

Attention: Patch coverage is 95.00000% with 1 line in your changes missing coverage. Please review.

Project coverage is 76.27%. Comparing base (47e45ee) to head (6aac597).

Files with missing lines Patch % Lines
src/utils/pathUtils.ts 90.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1948      +/-   ##
==========================================
+ Coverage   76.21%   76.27%   +0.05%     
==========================================
  Files          20       21       +1     
  Lines        1505     1513       +8     
  Branches      238      242       +4     
==========================================
+ Hits         1147     1154       +7     
- Misses        351      352       +1     
  Partials        7        7              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@tusharpandey13 tusharpandey13 changed the title removed trailing slashes from route pathnames Fix route matching when Next.js trailingSlash is enabled Mar 7, 2025
}

export const removeTrailingSlash = (path: string) =>
path.endsWith("/") ? path.slice(0, -1) : path;
Copy link
Contributor

@nandan-bhat nandan-bhat Mar 7, 2025

Choose a reason for hiding this comment

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

Please add test coverage to the above utility methods.

@tusharpandey13 tusharpandey13 merged commit b9944fa into main Mar 10, 2025
12 checks passed
@tusharpandey13 tusharpandey13 deleted the bugfix/routeTrailingSlash branch March 10, 2025 09:14
@tusharpandey13 tusharpandey13 mentioned this pull request Mar 10, 2025
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.

v4: Mounted routes are not reached with trailingSlash: true in next.config.js
3 participants