diff --git a/packages/solid-router/src/Transitioner.tsx b/packages/solid-router/src/Transitioner.tsx index 51112e3bbc5..d1af6069949 100644 --- a/packages/solid-router/src/Transitioner.tsx +++ b/packages/solid-router/src/Transitioner.tsx @@ -66,6 +66,7 @@ export function Transitioner() { // Try to load the initial location Solid.createRenderEffect(() => { + if (router.isServer) return Solid.untrack(() => { if ( (typeof window !== 'undefined' && router.clientSsr) || diff --git a/packages/solid-router/tests/Transitioner.test.tsx b/packages/solid-router/tests/Transitioner.test.tsx new file mode 100644 index 00000000000..5467795e221 --- /dev/null +++ b/packages/solid-router/tests/Transitioner.test.tsx @@ -0,0 +1,70 @@ +import { describe, expect, it, vi } from 'vitest' +import { render, waitFor } from '@solidjs/testing-library' +import { + createMemoryHistory, + createRootRoute, + createRoute, + createRouter, +} from '../src' +import { RouterProvider } from '../src/RouterProvider' + +describe('Transitioner', () => { + it('should call router.load() when Transitioner mounts on the client', async () => { + const rootRoute = createRootRoute() + const indexRoute = createRoute({ + getParentRoute: () => rootRoute, + path: '/', + component: () =>
Index
, + }) + + const routeTree = rootRoute.addChildren([indexRoute]) + const router = createRouter({ + routeTree, + history: createMemoryHistory({ + initialEntries: ['/'], + }), + }) + + // Mock router.load() to verify it gets called + const loadSpy = vi.spyOn(router, 'load').mockResolvedValue(undefined) + + render(() => ) + + // Wait for the createRenderEffect to run and call router.load() + await waitFor(() => { + expect(loadSpy).toHaveBeenCalledTimes(1) + }) + + loadSpy.mockRestore() + }) + + it('should not call router.load() when on the server', async () => { + const rootRoute = createRootRoute() + const indexRoute = createRoute({ + getParentRoute: () => rootRoute, + path: '/', + component: () =>
Index
, + }) + + const routeTree = rootRoute.addChildren([indexRoute]) + const router = createRouter({ + routeTree, + history: createMemoryHistory({ + initialEntries: ['/'], + }), + isServer: true, + }) + + // Mock router.load() to verify it gets called + const loadSpy = vi.spyOn(router, 'load').mockResolvedValue(undefined) + + render(() => ) + + // Wait for the createRenderEffect to run and call router.load() + await waitFor(() => { + expect(loadSpy).toHaveBeenCalledTimes(0) + }) + + loadSpy.mockRestore() + }) +}) diff --git a/packages/solid-router/tests/router.test.tsx b/packages/solid-router/tests/router.test.tsx index 244007334ef..5d452085622 100644 --- a/packages/solid-router/tests/router.test.tsx +++ b/packages/solid-router/tests/router.test.tsx @@ -1574,7 +1574,7 @@ describe('does not strip search params if search validation fails', () => { expect(window.location.search).toBe('?root=hello&index=world') }) - it('root is missing', async () => { + it('root is missing', () => { window.history.replaceState(null, 'root', '/?index=world') const router = getRouter() render(() => ) @@ -1582,7 +1582,7 @@ describe('does not strip search params if search validation fails', () => { expect(window.location.search).toBe('?index=world') }) - it('index is missing', async () => { + it('index is missing', () => { window.history.replaceState(null, 'root', '/?root=hello') const router = getRouter()