Skip to content

refactor: extract trailing-slash helpers into url utils - #724

Merged
dcalhoun merged 1 commit into
trunkfrom
refactor/url-trailing-slash-helpers
Sep 25, 2026
Merged

dcalhoun merged 1 commit into
trunkfrom
refactor/url-trailing-slash-helpers

Conversation

@dcalhoun

@dcalhoun dcalhoun commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

What?

Extract utilities for managing URL trailing slashes.

Why?

Address feedback in #617 (comment). Mitigate bugs from repeated logic.

How?

Add stripTrailingSlash and ensureTrailingSlash URL utilities.

Testing Instructions

N/A, no user-facing changes.

Accessibility Testing Instructions

N/A, no user-facing changes.

Screenshots or screencast

N/A, no user-facing changes.


AI-generated details

The same trailing-slash rule was written inline in three places: configureAjax, siteIndexMiddleware, and apiPathModifierMiddleware. Nothing tied them together, so each could gain its own edge-case handling and drift from the others — for instance if one started trimming leading slashes too.

src/utils/url.js now exports stripTrailingSlash and ensureTrailingSlash, and the three call sites use them.

Every caller reads an optional config field, so the helpers absorb an absent value (undefined, null or empty) rather than making each caller guard: stripTrailingSlash returns an empty string and ensureTrailingSlash stays empty rather than becoming a lone /, which would resolve against the page root. A value of / is kept, since that is a meaningful root. Any other non-string still throws, so a wrong type is not quietly passed through.

No behavior change.

Testing

  1. Run the unit suite:
    make test-web-unit
  2. Confirm src/utils/url.test.js covers both helpers, including repeated slashes, absent values, and the non-string case.

@github-actions github-actions Bot added the [Type] Task Issues or PRs that have been broken down into an individual action to take label Sep 23, 2026
@dcalhoun
dcalhoun added this pull request to stack #726 September 23, 2026 19:10
@wpmobilebot

wpmobilebot commented Sep 23, 2026 •

Copy link
Copy Markdown

XCFramework Build

This PR's XCFramework is available for testing. Add the following to your Package.swift:

.package(url: "https://github.com/wordpress-mobile/GutenbergKit", branch: "pr-build/724")

Built from 89f9521

@dcalhoun
dcalhoun force-pushed the refactor/url-trailing-slash-helpers branch 2 times, most recently from eb90759 to 0848c88 Compare September 23, 2026 20:17
@dcalhoun
dcalhoun marked this pull request as ready for review September 24, 2026 10:48
The same trailing-slash rule was written inline in three places, so the
three could drift apart as each gained its own edge-case handling. A
shared pair of helpers keeps them together.

The helpers absorb an absent value, since every caller reads an optional
config field and would otherwise repeat the same guard. A wrong type
still throws rather than passing through silently.

No behavior change.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014V7YC1kW6f5ZFHtMNGcLKs
@dcalhoun
dcalhoun force-pushed the refactor/url-trailing-slash-helpers branch from 0848c88 to 89f9521 Compare September 24, 2026 10:59
@dcalhoun
dcalhoun requested a review from adalpari September 24, 2026 11:17

@adalpari adalpari left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Changes look good! :shipit:

@dcalhoun
dcalhoun merged commit 93ac812 into trunk Sep 25, 2026
24 checks passed
@dcalhoun
dcalhoun deleted the refactor/url-trailing-slash-helpers branch September 25, 2026 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Type] Task Issues or PRs that have been broken down into an individual action to take

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants