Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react-router): stabilizing the encoding of path parameters #1490

Merged
merged 5 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ gpt/db.json

vite.config.js.timestamp-*
vite.config.ts.timestamp-*
.idea
.idea
*.vitest-temp.json
5 changes: 5 additions & 0 deletions packages/react-router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,11 @@ export class Router<
})
}

// encodeURI all params so the generated href is valid and stable
Object.keys(nextParams).forEach((key) => {
nextParams[key] = encodeURI(nextParams[key])
})

pathname = interpolatePath({
path: pathname,
params: nextParams ?? {},
Expand Down
118 changes: 118 additions & 0 deletions packages/react-router/tests/router.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { describe, it, expect } from 'vitest'

import {
createRootRoute,
createRoute,
createRouter,
createMemoryHistory,
type RouterHistory,
} from '../src'

function createTestRouter(initialHistory?: RouterHistory) {
const history =
initialHistory ?? createMemoryHistory({ initialEntries: ['/'] })

const rootRoute = createRootRoute({})
const indexRoute = createRoute({ getParentRoute: () => rootRoute, path: '/' })
const postsRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/posts',
})
const postIdRoute = createRoute({
getParentRoute: () => postsRoute,
path: '/$slug',
})
const routeTree = rootRoute.addChildren([
indexRoute,
postsRoute.addChildren([postIdRoute]),
])
const router = createRouter({ routeTree, history })

return { router, routes: { indexRoute, postsRoute, postIdRoute } }
}

describe('path params for /posts/$slug', () => {
it('state.location.pathname / href, should have the params.slug value of "tanner"', async () => {
const { router } = createTestRouter(
createMemoryHistory({ initialEntries: ['/posts/tanner'] }),
)

await router.load()

expect(router.state.location.pathname).toBe('/posts/tanner')
})

it('state.location.pathname / href, should have the params.slug value of "🚀"', async () => {
const { router } = createTestRouter(
createMemoryHistory({ initialEntries: ['/posts/🚀'] }),
)

await router.load()

expect(router.state.location.pathname).toBe('/posts/🚀')
})

it('state.location.pathname / href, should have the params.slug value of "%F0%9F%9A%80"', async () => {
const { router } = createTestRouter(
createMemoryHistory({ initialEntries: ['/posts/%F0%9F%9A%80'] }),
)

await router.load()

expect(router.state.location.pathname).toBe('/posts/%F0%9F%9A%80')
})

it('params.slug for the matched route, should be "tanner"', async () => {
const { router, routes } = createTestRouter(
createMemoryHistory({ initialEntries: ['/posts/tanner'] }),
)

await router.load()

const match = router.state.matches.find(
(r) => r.routeId === routes.postIdRoute.id,
)

if (!match) {
throw new Error('No match found')
}

expect((match.params as unknown as any).slug).toBe('tanner')
})

it('params.slug for the matched route, should be "🚀"', async () => {
const { router, routes } = createTestRouter(
createMemoryHistory({ initialEntries: ['/posts/🚀'] }),
)

await router.load()

const match = router.state.matches.find(
(r) => r.routeId === routes.postIdRoute.id,
)

if (!match) {
throw new Error('No match found')
}

expect((match.params as unknown as any).slug).toBe('🚀')
})

it('params.slug for the matched route, should be "🚀" instead of it being "%F0%9F%9A%80"', async () => {
const { router, routes } = createTestRouter(
createMemoryHistory({ initialEntries: ['/posts/%F0%9F%9A%80'] }),
)

await router.load()

const match = router.state.matches.find(
(r) => r.routeId === routes.postIdRoute.id,
)

if (!match) {
throw new Error('No match found')
}

expect((match.params as unknown as any).slug).toBe('🚀')
})
})
Loading