Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4
fix(init): scaffold ssr.noExternal for React Router v8 projects#374
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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,5 @@ | ||
| --- | ||
| "clerk": patch | ||
| --- | ||
| Fix `clerk init` for React Router v8 projects by adding `@clerk/react-router` to `ssr.noExternal` in the Vite config, preventing dev-mode SSR from failing with "useNavigate() may be used only in the context of a <Router>". |
115 changes: 115 additions & 0 deletions
115 packages/cli-core/src/commands/init/frameworks/react-router.test.ts
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
87 changes: 86 additions & 1 deletion
87 packages/cli-core/src/commands/init/frameworks/react-router.ts
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 |
|---|---|---|
| @@ -382,6 +382,83 @@ function scaffoldConfig(ctx: ProjectContext): Promise<FileAction | null> { | ||
| }); | ||
| } | ||
| type ViteScaffoldResult = { | ||
| action: FileAction | null; | ||
| /** True when the noExternal entry is needed but couldn't be applied — user must wire manually. */ | ||
| needsManualViteWire: boolean; | ||
| }; | ||
| /** | ||
| * Add `ssr.noExternal: ["@clerk/react-router"]` to the vite config. | ||
| * React Router v8 ships development/production conditional exports; `react-router dev` | ||
| * externalizes @clerk/react-router for SSR, so it resolves react-router's production | ||
| * build while app code gets the development build — two Router contexts, and every | ||
| * SSR render throws "useNavigate() may be used only in the context of a <Router>". | ||
| * https://github.com/remix-run/react-router/issues/15232 | ||
| */ | ||
| function addClerkNoExternal(content: string): string { | ||
| try { | ||
| const mod = parseModule(content); | ||
| const defaultExport = mod.exports.default; | ||
| if (!defaultExport || typeof defaultExport !== "object") return content; | ||
| // `export default defineConfig({...})` proxies the call expression; property writes on it | ||
| // throw, so operate on the call's first argument (the config object) instead. | ||
| const config = defaultExport.$type === "function-call" ? defaultExport.$args[0] : defaultExport; | ||
| if (!config || typeof config !== "object") return content; | ||
| if (!config.ssr) config.ssr = {}; | ||
| if (config.ssr.noExternal === undefined) config.ssr.noExternal = []; | ||
| // boolean/string/regex noExternal: leave it and let the caller emit the manual instruction | ||
| if (!Array.isArray(config.ssr.noExternal)) return content; | ||
| if (!config.ssr.noExternal.includes("@clerk/react-router")) { | ||
| config.ssr.noExternal.push("@clerk/react-router"); | ||
| } | ||
| return mod.generate().code; | ||
| } catch { | ||
| return content; | ||
| } | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| async function scaffoldViteConfig(ctx: ProjectContext): Promise<ViteScaffoldResult> { | ||
| // The dual-instance issue only exists in v8's conditional exports; v7 doesn't need this. | ||
| if (shouldEnableV8MiddlewareFlag(ctx)) return { action: null, needsManualViteWire: false }; | ||
| const configPath = await findFirstFile(ctx.cwd, [ | ||
| "vite.config.ts", | ||
| "vite.config.js", | ||
| "vite.config.mts", | ||
| "vite.config.mjs", | ||
| ]); | ||
| if (!configPath) return { action: null, needsManualViteWire: true }; | ||
| const content = await Bun.file(join(ctx.cwd, configPath)).text(); | ||
| if (content.includes("@clerk/react-router")) { | ||
| return { | ||
| action: { | ||
| type: "skip", | ||
| path: configPath, | ||
| skipReason: "Already references @clerk/react-router in vite config", | ||
| }, | ||
| needsManualViteWire: false, | ||
| }; | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| const updated = addClerkNoExternal(content); | ||
| if (updated === content) return { action: null, needsManualViteWire: true }; | ||
| return { | ||
| action: { | ||
| path: configPath, | ||
| type: "modify", | ||
| content: updated, | ||
| description: "Add @clerk/react-router to ssr.noExternal (React Router v8 dev SSR fix)", | ||
| }, | ||
| needsManualViteWire: false, | ||
| }; | ||
| } | ||
| export const reactRouter: FrameworkScaffold = { | ||
| name: "React Router", | ||
| dep: "react-router", | ||
| @@ -390,8 +467,9 @@ export const reactRouter: FrameworkScaffold = { | ||
| matches: (ctx) => ctx.framework.dep === "react-router", | ||
| async scaffold(ctx: ProjectContext): Promise<ScaffoldPlan> { | ||
| const [configAction, rootResult, localePrefix, envAction] = await Promise.all([ | ||
| const [configAction, viteResult, rootResult, localePrefix, envAction] = await Promise.all([ | ||
| scaffoldConfig(ctx), | ||
| scaffoldViteConfig(ctx), | ||
| scaffoldRoot(ctx), | ||
| detectLocalePrefix(ctx.cwd), | ||
| scaffoldEnvVars(ctx, SIGN_ROUTE_ENV_VARS.vite), | ||
| @@ -404,6 +482,7 @@ export const reactRouter: FrameworkScaffold = { | ||
| const rootAction = rootResult.action; | ||
| const actions = [ | ||
| configAction, | ||
| viteResult.action, | ||
| rootAction, | ||
| ...authActions, | ||
| routesResult.action, | ||
| @@ -424,6 +503,12 @@ export const reactRouter: FrameworkScaffold = { | ||
| ); | ||
| } | ||
| if (viteResult.needsManualViteWire) { | ||
| postInstructions.push( | ||
| "React Router v8 needs `ssr: { noExternal: ['@clerk/react-router'] }` in your vite config — without it, dev SSR fails with \"useNavigate() may be used only in the context of a <Router>\". See: https://github.com/remix-run/react-router/issues/15232", | ||
| ); | ||
| } | ||
| if (rootAction?.type === "modify" && rootResult.needsManualLoaderMerge) { | ||
| postInstructions.push( | ||
| "Update your existing app/root.tsx loader to import and call rootAuthLoader(args), then pass that loaderData to <ClerkProvider>.", | ||
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.
Uh oh!
There was an error while loading. Please reload this page.