Skip to content

Commit d6af29e

Browse files
feat: add default setting for ssr in router (#2477)
1 parent 69f02b3 commit d6af29e

15 files changed

Lines changed: 30 additions & 15 deletions

packages/react-router/src/route.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,7 @@ export class Route<
882882
private _path!: TPath
883883
private _fullPath!: TFullPath
884884
private _to!: TrimPathRight<TFullPath>
885+
private _ssr!: boolean
885886

886887
public get to() {
887888
/* invariant(
@@ -916,6 +917,10 @@ export class Route<
916917
return this._fullPath
917918
}
918919

920+
public get ssr() {
921+
return this._ssr
922+
}
923+
919924
// Optional
920925
children?: TChildren
921926
originalIndex?: number
@@ -984,7 +989,7 @@ export class Route<
984989
loaderDeps: TLoaderDeps
985990
}
986991

987-
init = (opts: { originalIndex: number }): void => {
992+
init = (opts: { originalIndex: number; defaultSsr?: boolean }): void => {
988993
this.originalIndex = opts.originalIndex
989994

990995
const options = this.options as
@@ -1051,6 +1056,7 @@ export class Route<
10511056
// this.customId = customId as TCustomId
10521057
this._fullPath = fullPath as TFullPath
10531058
this._to = fullPath as TrimPathRight<TFullPath>
1059+
this._ssr = options?.ssr ?? opts.defaultSsr ?? true
10541060
}
10551061

10561062
addChildren<

packages/react-router/src/router.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@ export interface RouterOptions<
451451
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RouterOptionsType#isserver property)
452452
*/
453453
isServer?: boolean
454+
455+
defaultSsr?: boolean
454456
}
455457

456458
export interface RouterErrorSerializer<TSerializedError> {
@@ -773,13 +775,19 @@ export class Router<
773775

774776
const notFoundRoute = this.options.notFoundRoute
775777
if (notFoundRoute) {
776-
notFoundRoute.init({ originalIndex: 99999999999 })
778+
notFoundRoute.init({
779+
originalIndex: 99999999999,
780+
defaultSsr: this.options.defaultSsr,
781+
})
777782
;(this.routesById as any)[notFoundRoute.id] = notFoundRoute
778783
}
779784

780785
const recurseRoutes = (childRoutes: Array<AnyRoute>) => {
781786
childRoutes.forEach((childRoute, i) => {
782-
childRoute.init({ originalIndex: i })
787+
childRoute.init({
788+
originalIndex: i,
789+
defaultSsr: this.options.defaultSsr,
790+
})
783791

784792
const existingRoute = (this.routesById as any)[childRoute.id]
785793

packages/router-plugin/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
],
2626
"scripts": {
2727
"clean": "rimraf ./dist && rimraf ./coverage",
28+
"clean:snapshots": "rimraf **/*snapshot* --glob",
2829
"test": "pnpm test:eslint && pnpm test:types && pnpm test:build && pnpm test:unit",
2930
"test:unit": "vitest",
3031
"test:unit:dev": "vitest --watch",

packages/router-plugin/src/core/code-splitter/compilers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export function compileCodeSplitReferenceRoute(opts: ParseAstOptions) {
148148
}
149149

150150
prop.value = template.expression(
151-
`lazyRouteComponent($$splitComponentImporter, 'component', () => Route.options.ssr)`,
151+
`lazyRouteComponent($$splitComponentImporter, 'component', () => Route.ssr)`,
152152
)()
153153

154154
programPath.pushContainer('body', [

packages/router-plugin/tests/code-splitter/snapshots/destructured-react-memo-imported-component.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ const $$splitComponentImporter = () => import('tsr-split:destructured-react-memo
44
import { lazyRouteComponent } from '@tanstack/react-router';
55
import { createFileRoute } from '@tanstack/react-router';
66
export const Route = createFileRoute('/')({
7-
component: lazyRouteComponent($$splitComponentImporter, 'component', () => Route.options.ssr),
7+
component: lazyRouteComponent($$splitComponentImporter, 'component', () => Route.ssr),
88
loader: lazyFn($$splitLoaderImporter, 'loader')
99
});

packages/router-plugin/tests/code-splitter/snapshots/function-declaration.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ import { lazyFn } from '@tanstack/react-router';
55
import { createFileRoute } from '@tanstack/react-router';
66
export const Route = createFileRoute('/posts')({
77
loader: lazyFn($$splitLoaderImporter, 'loader'),
8-
component: lazyRouteComponent($$splitComponentImporter, 'component', () => Route.options.ssr)
8+
component: lazyRouteComponent($$splitComponentImporter, 'component', () => Route.ssr)
99
});

packages/router-plugin/tests/code-splitter/snapshots/imported-default-component-destructured-loader.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ const $$splitComponentImporter = () => import('tsr-split:imported-default-compon
44
import { lazyRouteComponent } from '@tanstack/react-router';
55
import { createFileRoute } from '@tanstack/react-router';
66
export const Route = createFileRoute('/')({
7-
component: lazyRouteComponent($$splitComponentImporter, 'component', () => Route.options.ssr),
7+
component: lazyRouteComponent($$splitComponentImporter, 'component', () => Route.ssr),
88
loader: lazyFn($$splitLoaderImporter, 'loader')
99
});

packages/router-plugin/tests/code-splitter/snapshots/imported-default-component.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ const $$splitComponentImporter = () => import('tsr-split:imported-default-compon
22
import { lazyRouteComponent } from '@tanstack/react-router';
33
import { createFileRoute } from '@tanstack/react-router';
44
export const Route = createFileRoute('/')({
5-
component: lazyRouteComponent($$splitComponentImporter, 'component', () => Route.options.ssr)
5+
component: lazyRouteComponent($$splitComponentImporter, 'component', () => Route.ssr)
66
});

packages/router-plugin/tests/code-splitter/snapshots/imported.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ const $$splitComponentImporter = () => import('tsr-split:imported.tsx?tsr-split'
44
import { lazyRouteComponent } from '@tanstack/react-router';
55
import { createFileRoute } from '@tanstack/react-router';
66
export const Route = createFileRoute('/')({
7-
component: lazyRouteComponent($$splitComponentImporter, 'component', () => Route.options.ssr),
7+
component: lazyRouteComponent($$splitComponentImporter, 'component', () => Route.ssr),
88
loader: lazyFn($$splitLoaderImporter, 'loader')
99
});

packages/router-plugin/tests/code-splitter/snapshots/inline.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const $$splitComponentImporter = () => import('tsr-split:inline.tsx?tsr-split');
22
import { lazyRouteComponent } from '@tanstack/react-router';
33
import { createFileRoute } from '@tanstack/react-router';
44
export const Route = createFileRoute('/')({
5-
component: lazyRouteComponent($$splitComponentImporter, 'component', () => Route.options.ssr)
5+
component: lazyRouteComponent($$splitComponentImporter, 'component', () => Route.ssr)
66
});
77
Route.addChildren([]);
88
export const test = 'test';

0 commit comments

Comments
 (0)