Skip to content

Commit 4fe6fe1

Browse files
fix react-router shared route css persistence on nav
1 parent 8caa202 commit 4fe6fe1

12 files changed

Lines changed: 194 additions & 1 deletion

File tree

‎.changeset/tough-bears-tease.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/react-router': patch
3+
---
4+
5+
Preserve shared route stylesheet links across client navigation by rendering route CSS assets with React stylesheet precedence.

‎e2e/react-start/rsc/tests/rsc-css-modules.spec.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,33 @@ test.describe('RSC CSS Modules Tests', () => {
122122
)
123123
expect(backgroundColor).toBe('rgb(224, 242, 254)')
124124
})
125+
126+
test('RSC CSS module styles persist across sibling client-side navigation', async ({
127+
page,
128+
}) => {
129+
await page.goto('/rsc-css-modules')
130+
await page.waitForURL('/rsc-css-modules')
131+
132+
const cssModulesContent = page.getByTestId('rsc-css-modules-content')
133+
await expect(cssModulesContent).toBeVisible()
134+
135+
const initialBackgroundColor = await cssModulesContent.evaluate(
136+
(el) => getComputedStyle(el).backgroundColor,
137+
)
138+
expect(initialBackgroundColor).toBe('rgb(224, 242, 254)')
139+
140+
await page.getByTestId('nav-global-css').click()
141+
await page.waitForURL('/rsc-global-css')
142+
await expect(page.getByTestId('rsc-global-css-content')).toBeVisible()
143+
144+
await page.getByTestId('nav-css-modules').click()
145+
await page.waitForURL('/rsc-css-modules')
146+
147+
await expect(cssModulesContent).toBeVisible()
148+
149+
const finalBackgroundColor = await cssModulesContent.evaluate(
150+
(el) => getComputedStyle(el).backgroundColor,
151+
)
152+
expect(finalBackgroundColor).toBe('rgb(224, 242, 254)')
153+
})
125154
})

‎e2e/react-start/start-manifest/src/components/AppShell.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import styles from '~/styles/root-shell.module.css'
33

44
const ROUTES = linkOptions([
55
{ to: '/', label: 'home' },
6+
{ to: '/a', label: '/a' },
7+
{ to: '/b', label: '/b' },
68
{ to: '/r1', label: '/r1' },
79
{ to: '/r2', label: '/r2' },
810
{ to: '/shared-a', label: '/shared-a' },
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import styles from '~/styles/shared-card.module.css'
2+
3+
export function SharedCard({ label }: { label: string }) {
4+
return (
5+
<div className={styles.card} data-testid="shared-card">
6+
Shared card {label}
7+
</div>
8+
)
9+
}

‎e2e/react-start/start-manifest/src/routeTree.gen.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import { Route as R4RouteImport } from './routes/r4'
1818
import { Route as R3RouteImport } from './routes/r3'
1919
import { Route as R2RouteImport } from './routes/r2'
2020
import { Route as R1RouteImport } from './routes/r1'
21+
import { Route as BRouteImport } from './routes/b'
22+
import { Route as ARouteImport } from './routes/a'
2123
import { Route as IndexRouteImport } from './routes/index'
2224

2325
const SharedCRoute = SharedCRouteImport.update({
@@ -65,6 +67,16 @@ const R1Route = R1RouteImport.update({
6567
path: '/r1',
6668
getParentRoute: () => rootRouteImport,
6769
} as any)
70+
const BRoute = BRouteImport.update({
71+
id: '/b',
72+
path: '/b',
73+
getParentRoute: () => rootRouteImport,
74+
} as any)
75+
const ARoute = ARouteImport.update({
76+
id: '/a',
77+
path: '/a',
78+
getParentRoute: () => rootRouteImport,
79+
} as any)
6880
const IndexRoute = IndexRouteImport.update({
6981
id: '/',
7082
path: '/',
@@ -73,6 +85,8 @@ const IndexRoute = IndexRouteImport.update({
7385

7486
export interface FileRoutesByFullPath {
7587
'/': typeof IndexRoute
88+
'/a': typeof ARoute
89+
'/b': typeof BRoute
7690
'/r1': typeof R1Route
7791
'/r2': typeof R2Route
7892
'/r3': typeof R3Route
@@ -85,6 +99,8 @@ export interface FileRoutesByFullPath {
8599
}
86100
export interface FileRoutesByTo {
87101
'/': typeof IndexRoute
102+
'/a': typeof ARoute
103+
'/b': typeof BRoute
88104
'/r1': typeof R1Route
89105
'/r2': typeof R2Route
90106
'/r3': typeof R3Route
@@ -98,6 +114,8 @@ export interface FileRoutesByTo {
98114
export interface FileRoutesById {
99115
__root__: typeof rootRouteImport
100116
'/': typeof IndexRoute
117+
'/a': typeof ARoute
118+
'/b': typeof BRoute
101119
'/r1': typeof R1Route
102120
'/r2': typeof R2Route
103121
'/r3': typeof R3Route
@@ -112,6 +130,8 @@ export interface FileRouteTypes {
112130
fileRoutesByFullPath: FileRoutesByFullPath
113131
fullPaths:
114132
| '/'
133+
| '/a'
134+
| '/b'
115135
| '/r1'
116136
| '/r2'
117137
| '/r3'
@@ -124,6 +144,8 @@ export interface FileRouteTypes {
124144
fileRoutesByTo: FileRoutesByTo
125145
to:
126146
| '/'
147+
| '/a'
148+
| '/b'
127149
| '/r1'
128150
| '/r2'
129151
| '/r3'
@@ -136,6 +158,8 @@ export interface FileRouteTypes {
136158
id:
137159
| '__root__'
138160
| '/'
161+
| '/a'
162+
| '/b'
139163
| '/r1'
140164
| '/r2'
141165
| '/r3'
@@ -149,6 +173,8 @@ export interface FileRouteTypes {
149173
}
150174
export interface RootRouteChildren {
151175
IndexRoute: typeof IndexRoute
176+
ARoute: typeof ARoute
177+
BRoute: typeof BRoute
152178
R1Route: typeof R1Route
153179
R2Route: typeof R2Route
154180
R3Route: typeof R3Route
@@ -225,6 +251,20 @@ declare module '@tanstack/react-router' {
225251
preLoaderRoute: typeof R1RouteImport
226252
parentRoute: typeof rootRouteImport
227253
}
254+
'/b': {
255+
id: '/b'
256+
path: '/b'
257+
fullPath: '/b'
258+
preLoaderRoute: typeof BRouteImport
259+
parentRoute: typeof rootRouteImport
260+
}
261+
'/a': {
262+
id: '/a'
263+
path: '/a'
264+
fullPath: '/a'
265+
preLoaderRoute: typeof ARouteImport
266+
parentRoute: typeof rootRouteImport
267+
}
228268
'/': {
229269
id: '/'
230270
path: '/'
@@ -237,6 +277,8 @@ declare module '@tanstack/react-router' {
237277

238278
const rootRouteChildren: RootRouteChildren = {
239279
IndexRoute: IndexRoute,
280+
ARoute: ARoute,
281+
BRoute: BRoute,
240282
R1Route: R1Route,
241283
R2Route: R2Route,
242284
R3Route: R3Route,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { createFileRoute } from '@tanstack/react-router'
2+
import { SharedCard } from '~/components/SharedCard'
3+
import styles from '~/styles/page-a.module.css'
4+
5+
export const Route = createFileRoute('/a')({
6+
component: RouteComponent,
7+
})
8+
9+
function RouteComponent() {
10+
return (
11+
<section className={styles.page} data-testid="page-a">
12+
<h1 className={styles.title}>Route /a</h1>
13+
<p>Route /a keeps the shared card styled.</p>
14+
<SharedCard label="from /a" />
15+
</section>
16+
)
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { createFileRoute } from '@tanstack/react-router'
2+
import { SharedCard } from '~/components/SharedCard'
3+
import styles from '~/styles/page-b.module.css'
4+
5+
export const Route = createFileRoute('/b')({
6+
component: RouteComponent,
7+
})
8+
9+
function RouteComponent() {
10+
return (
11+
<section className={styles.page} data-testid="page-b">
12+
<h1 className={styles.title}>Route /b</h1>
13+
<p>Route /b should keep the shared card stylesheet after nav.</p>
14+
<SharedCard label="from /b" />
15+
</section>
16+
)
17+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.page {
2+
padding: 1rem;
3+
min-height: 12rem;
4+
background: rgb(240, 253, 244);
5+
border: 4px solid rgb(22, 163, 74);
6+
}
7+
8+
.title {
9+
color: rgb(21, 128, 61);
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.page {
2+
padding: 1rem;
3+
min-height: 12rem;
4+
background: rgb(239, 246, 255);
5+
border: 4px solid rgb(37, 99, 235);
6+
}
7+
8+
.title {
9+
color: rgb(29, 78, 216);
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.card {
2+
margin-top: 1rem;
3+
padding: 1.5rem;
4+
border-radius: 0.75rem;
5+
background: rgb(252, 231, 243);
6+
border: 2px solid rgb(190, 24, 93);
7+
color: rgb(157, 23, 77);
8+
}

0 commit comments

Comments
 (0)