|
9 | 9 |
|
10 | 10 | import { |
11 | 11 | HeadContent, |
| 12 | + Link, |
12 | 13 | Outlet, |
13 | 14 | RouterProvider, |
14 | 15 | createBrowserHistory, |
@@ -116,6 +117,96 @@ describe('ssr scripts', () => { |
116 | 117 | '<script src="script.js"></script><script src="script3.js"></script>', |
117 | 118 | ) |
118 | 119 | }) |
| 120 | + |
| 121 | + test('keeps manifest stylesheet links mounted when navigating with Link', async () => { |
| 122 | + const history = createBrowserHistory() |
| 123 | + |
| 124 | + try { |
| 125 | + const rootRoute = createRootRoute({ |
| 126 | + component: () => { |
| 127 | + return ( |
| 128 | + <> |
| 129 | + <HeadContent /> |
| 130 | + <Outlet /> |
| 131 | + </> |
| 132 | + ) |
| 133 | + }, |
| 134 | + }) |
| 135 | + |
| 136 | + const indexRoute = createRoute({ |
| 137 | + path: '/', |
| 138 | + getParentRoute: () => rootRoute, |
| 139 | + component: () => <Link to="/about">Go to about page</Link>, |
| 140 | + }) |
| 141 | + |
| 142 | + const aboutRoute = createRoute({ |
| 143 | + path: '/about', |
| 144 | + getParentRoute: () => rootRoute, |
| 145 | + component: () => <div>About</div>, |
| 146 | + }) |
| 147 | + |
| 148 | + const router = createRouter({ |
| 149 | + history, |
| 150 | + routeTree: rootRoute.addChildren([indexRoute, aboutRoute]), |
| 151 | + }) |
| 152 | + |
| 153 | + router.ssr = { |
| 154 | + manifest: { |
| 155 | + routes: { |
| 156 | + [rootRoute.id]: { |
| 157 | + assets: [ |
| 158 | + { |
| 159 | + tag: 'link', |
| 160 | + attrs: { |
| 161 | + rel: 'stylesheet', |
| 162 | + href: '/main.css', |
| 163 | + }, |
| 164 | + }, |
| 165 | + ], |
| 166 | + }, |
| 167 | + }, |
| 168 | + }, |
| 169 | + } as any |
| 170 | + |
| 171 | + await router.load() |
| 172 | + |
| 173 | + render(() => <RouterProvider router={router} />) |
| 174 | + |
| 175 | + const getStylesheetLink = () => |
| 176 | + Array.from( |
| 177 | + document.head.querySelectorAll('link[rel="stylesheet"]'), |
| 178 | + ).find((link) => link.getAttribute('href') === '/main.css') |
| 179 | + |
| 180 | + await waitFor(() => { |
| 181 | + expect(getStylesheetLink()).toBeInstanceOf(HTMLLinkElement) |
| 182 | + }) |
| 183 | + |
| 184 | + const initialLink = getStylesheetLink() |
| 185 | + expect(initialLink).toBeInstanceOf(HTMLLinkElement) |
| 186 | + |
| 187 | + fireEvent.click(screen.getByRole('link', { name: 'Go to about page' })) |
| 188 | + |
| 189 | + await waitFor(() => { |
| 190 | + expect(router.state.location.pathname).toBe('/about') |
| 191 | + }) |
| 192 | + |
| 193 | + expect(getStylesheetLink()).toBe(initialLink) |
| 194 | + expect( |
| 195 | + Array.from( |
| 196 | + document.head.querySelectorAll('link[rel="stylesheet"]'), |
| 197 | + ).filter((link) => link.getAttribute('href') === '/main.css'), |
| 198 | + ).toHaveLength(1) |
| 199 | + } finally { |
| 200 | + history.destroy() |
| 201 | + document.head |
| 202 | + .querySelectorAll('link[rel="stylesheet"]') |
| 203 | + .forEach((link) => { |
| 204 | + if (link.getAttribute('href') === '/main.css') { |
| 205 | + link.remove() |
| 206 | + } |
| 207 | + }) |
| 208 | + } |
| 209 | + }) |
119 | 210 | }) |
120 | 211 |
|
121 | 212 | describe('ssr HeadContent', () => { |
|
0 commit comments