@@ -223,6 +223,97 @@ describe('ssr scripts', () => {
223223 ) . toHaveLength ( 1 )
224224 } )
225225
226+ test ( 'keeps manifest stylesheet links mounted when preload counts change' , async ( ) => {
227+ const history = createTestBrowserHistory ( )
228+
229+ const rootRoute = createRootRoute ( {
230+ component : ( ) => {
231+ return (
232+ < >
233+ < HeadContent />
234+ < Outlet />
235+ </ >
236+ )
237+ } ,
238+ } )
239+
240+ const aRoute = createRoute ( {
241+ path : '/a' ,
242+ getParentRoute : ( ) => rootRoute ,
243+ component : ( ) => < Link to = "/b" > Go to B</ Link > ,
244+ } )
245+
246+ const bRoute = createRoute ( {
247+ path : '/b' ,
248+ getParentRoute : ( ) => rootRoute ,
249+ component : ( ) => < Link to = "/a" > Go to A</ Link > ,
250+ } )
251+
252+ const router = createRouter ( {
253+ history,
254+ routeTree : rootRoute . addChildren ( [ aRoute , bRoute ] ) ,
255+ } )
256+
257+ router . ssr = {
258+ manifest : {
259+ routes : {
260+ [ rootRoute . id ] : {
261+ preloads : [ '/root.js' ] ,
262+ assets : [
263+ {
264+ tag : 'link' ,
265+ attrs : {
266+ rel : 'stylesheet' ,
267+ href : '/main.css' ,
268+ } ,
269+ } ,
270+ ] ,
271+ } ,
272+ [ aRoute . id ] : {
273+ preloads : [ '/a.js' ] ,
274+ assets : [ ] ,
275+ } ,
276+ [ bRoute . id ] : {
277+ preloads : [ '/b.js' , '/b-child.js' ] ,
278+ assets : [ ] ,
279+ } ,
280+ } ,
281+ } ,
282+ }
283+
284+ await router . navigate ( { to : '/a' } )
285+ await router . load ( )
286+
287+ render ( ( ) => < RouterProvider router = { router } /> )
288+
289+ const getStylesheetLink = ( ) =>
290+ Array . from ( document . head . querySelectorAll ( 'link[rel="stylesheet"]' ) ) . find (
291+ ( link ) => link . getAttribute ( 'href' ) === '/main.css' ,
292+ )
293+
294+ await waitFor ( ( ) => {
295+ expect ( getStylesheetLink ( ) ) . toBeInstanceOf ( HTMLLinkElement )
296+ } )
297+
298+ const initialLink = getStylesheetLink ( )
299+ expect ( initialLink ) . toBeInstanceOf ( HTMLLinkElement )
300+
301+ fireEvent . click ( screen . getByRole ( 'link' , { name : 'Go to B' } ) )
302+
303+ await waitFor ( ( ) => {
304+ expect ( router . state . location . pathname ) . toBe ( '/b' )
305+ } )
306+
307+ await screen . findByRole ( 'link' , { name : 'Go to A' } )
308+
309+ expect ( getStylesheetLink ( ) ) . toBe ( initialLink )
310+ expect (
311+ Array . from (
312+ document . head . querySelectorAll ( 'link[rel="stylesheet"]' ) ,
313+ ) . filter ( ( link ) => link . getAttribute ( 'href' ) === '/main.css' ) ,
314+ ) . toHaveLength ( 1 )
315+ } )
316+
226317 test ( 'applies assetCrossOrigin to manifest assets and preloads' , async ( ) => {
227318 const history = createTestBrowserHistory ( )
228319
0 commit comments