-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Type-safe href (#12994) #13012
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
Merged
Merged
Type-safe href (#12994) #13012
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
"@react-router/dev": patch | ||
"react-router": patch | ||
--- | ||
|
||
Fix typegen for repeated params | ||
|
||
In React Router, path parameters are keyed by their name. | ||
So for a path pattern like `/a/:id/b/:id?/c/:id`, the last `:id` will set the value for `id` in `useParams` and the `params` prop. | ||
For example, `/a/1/b/2/c/3` will result in the value `{ id: 3 }` at runtime. | ||
|
||
Previously, generated types for params incorrectly modeled repeated params with an array. | ||
So `/a/1/b/2/c/3` generated a type like `{ id: [1,2,3] }`. | ||
|
||
To be consistent with runtime behavior, the generated types now correctly model the "last one wins" semantics of path parameters. | ||
So `/a/1/b/2/c/3` now generates a type like `{ id: 3 }`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@react-router/dev": patch | ||
--- | ||
|
||
Fix `ArgError: unknown or unexpected option: --version` when running `react-router --version` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
"@react-router/dev": minor | ||
"react-router": minor | ||
--- | ||
|
||
New type-safe `href` utility that guarantees links point to actual paths in your app | ||
|
||
```tsx | ||
import { href } from "react-router"; | ||
|
||
export default function Component() { | ||
const link = href("/blog/:slug", { slug: "my-first-post" }); | ||
return ( | ||
<main> | ||
<Link to={href("/products/:id", { id: "asdf" })} /> | ||
<NavLink to={href("/:lang?/about", { lang: "en" })} /> | ||
</main> | ||
); | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
import { spawnSync } from "node:child_process"; | ||
import * as path from "node:path"; | ||
|
||
import { expect, test } from "@playwright/test"; | ||
import dedent from "dedent"; | ||
import semver from "semver"; | ||
import fse from "fs-extra"; | ||
|
||
import { createProject } from "./helpers/vite"; | ||
|
||
const nodeBin = process.argv[0]; | ||
const reactRouterBin = "node_modules/@react-router/dev/dist/cli/index.js"; | ||
|
||
const run = (command: string[], options: Parameters<typeof spawnSync>[2]) => | ||
spawnSync(nodeBin, [reactRouterBin, ...command], options); | ||
|
||
const helpText = dedent` | ||
react-router | ||
|
||
Usage: | ||
$ react-router build [projectDir] | ||
$ react-router dev [projectDir] | ||
$ react-router routes [projectDir] | ||
|
||
Options: | ||
--help, -h Print this help message and exit | ||
--version, -v Print the CLI version and exit | ||
--no-color Disable ANSI colors in console output | ||
\`build\` Options: | ||
--assetsInlineLimit Static asset base64 inline threshold in bytes (default: 4096) (number) | ||
--clearScreen Allow/disable clear screen when logging (boolean) | ||
--config, -c Use specified config file (string) | ||
--emptyOutDir Force empty outDir when it's outside of root (boolean) | ||
--logLevel, -l Info | warn | error | silent (string) | ||
--minify Enable/disable minification, or specify minifier to use (default: "esbuild") (boolean | "terser" | "esbuild") | ||
--mode, -m Set env mode (string) | ||
--profile Start built-in Node.js inspector | ||
--sourcemapClient Output source maps for client build (default: false) (boolean | "inline" | "hidden") | ||
--sourcemapServer Output source maps for server build (default: false) (boolean | "inline" | "hidden") | ||
\`dev\` Options: | ||
--clearScreen Allow/disable clear screen when logging (boolean) | ||
--config, -c Use specified config file (string) | ||
--cors Enable CORS (boolean) | ||
--force Force the optimizer to ignore the cache and re-bundle (boolean) | ||
--host Specify hostname (string) | ||
--logLevel, -l Info | warn | error | silent (string) | ||
--mode, -m Set env mode (string) | ||
--open Open browser on startup (boolean | string) | ||
--port Specify port (number) | ||
--profile Start built-in Node.js inspector | ||
--strictPort Exit if specified port is already in use (boolean) | ||
\`routes\` Options: | ||
--config, -c Use specified Vite config file (string) | ||
--json Print the routes as JSON | ||
\`reveal\` Options: | ||
--config, -c Use specified Vite config file (string) | ||
--no-typescript Generate plain JavaScript files | ||
\`typegen\` Options: | ||
--watch Automatically regenerate types whenever route config (\`routes.ts\`) or route modules change | ||
|
||
Build your project: | ||
|
||
$ react-router build | ||
|
||
Run your project locally in development: | ||
|
||
$ react-router dev | ||
|
||
Show all routes in your app: | ||
|
||
$ react-router routes | ||
$ react-router routes my-app | ||
$ react-router routes --json | ||
$ react-router routes --config vite.react-router.config.ts | ||
|
||
Reveal the used entry point: | ||
|
||
$ react-router reveal entry.client | ||
$ react-router reveal entry.server | ||
$ react-router reveal entry.client --no-typescript | ||
$ react-router reveal entry.server --no-typescript | ||
$ react-router reveal entry.server --config vite.react-router.config.ts | ||
|
||
Generate types for route modules: | ||
|
||
$ react-router typegen | ||
$ react-router typegen --watch | ||
`; | ||
|
||
test.describe("cli", () => { | ||
test("--help", async () => { | ||
const cwd = await createProject(); | ||
const { stdout, stderr, status } = run(["--help"], { | ||
cwd, | ||
env: { | ||
NO_COLOR: "1", | ||
}, | ||
}); | ||
expect(stdout.toString().trim()).toBe(helpText); | ||
expect(stderr.toString()).toBe(""); | ||
expect(status).toBe(0); | ||
}); | ||
|
||
test("--version", async () => { | ||
const cwd = await createProject(); | ||
let { stdout, stderr, status } = run(["--version"], { cwd }); | ||
expect(semver.valid(stdout.toString().trim())).not.toBeNull(); | ||
expect(stderr.toString()).toBe(""); | ||
expect(status).toBe(0); | ||
}); | ||
|
||
test("routes", async () => { | ||
const cwd = await createProject(); | ||
let { stdout, stderr, status } = run(["routes"], { cwd }); | ||
expect(stdout.toString().trim()).toBe(dedent` | ||
<Routes> | ||
<Route file="root.tsx"> | ||
<Route index file="routes/_index.tsx" /> | ||
</Route> | ||
</Routes> | ||
`); | ||
expect(stderr.toString()).toBe(""); | ||
expect(status).toBe(0); | ||
}); | ||
|
||
test.describe("reveal", async () => { | ||
test("generates entry.{server,client}.tsx in the app directory", async () => { | ||
const cwd = await createProject(); | ||
let entryClientFile = path.join(cwd, "app", "entry.client.tsx"); | ||
let entryServerFile = path.join(cwd, "app", "entry.server.tsx"); | ||
|
||
expect(fse.existsSync(entryServerFile)).toBeFalsy(); | ||
expect(fse.existsSync(entryClientFile)).toBeFalsy(); | ||
|
||
run(["reveal"], { cwd }); | ||
|
||
expect(fse.existsSync(entryServerFile)).toBeTruthy(); | ||
expect(fse.existsSync(entryClientFile)).toBeTruthy(); | ||
}); | ||
|
||
test("generates specified entries in the app directory", async () => { | ||
const cwd = await createProject(); | ||
|
||
let entryClientFile = path.join(cwd, "app", "entry.client.tsx"); | ||
let entryServerFile = path.join(cwd, "app", "entry.server.tsx"); | ||
|
||
expect(fse.existsSync(entryServerFile)).toBeFalsy(); | ||
expect(fse.existsSync(entryClientFile)).toBeFalsy(); | ||
|
||
run(["reveal", "entry.server"], { cwd }); | ||
expect(fse.existsSync(entryServerFile)).toBeTruthy(); | ||
expect(fse.existsSync(entryClientFile)).toBeFalsy(); | ||
fse.removeSync(entryServerFile); | ||
|
||
run(["reveal", "entry.client"], { cwd }); | ||
expect(fse.existsSync(entryClientFile)).toBeTruthy(); | ||
expect(fse.existsSync(entryServerFile)).toBeFalsy(); | ||
}); | ||
|
||
test("generates entry.{server,client}.jsx in the app directory with --no-typescript", async () => { | ||
const cwd = await createProject(); | ||
let entryClientFile = path.join(cwd, "app", "entry.client.jsx"); | ||
let entryServerFile = path.join(cwd, "app", "entry.server.jsx"); | ||
|
||
expect(fse.existsSync(entryServerFile)).toBeFalsy(); | ||
expect(fse.existsSync(entryClientFile)).toBeFalsy(); | ||
|
||
run(["reveal", "--no-typescript"], { cwd }); | ||
|
||
expect(fse.existsSync(entryServerFile)).toBeTruthy(); | ||
expect(fse.existsSync(entryClientFile)).toBeTruthy(); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😍 Awesome to have tests for this now!