Skip to content

fix(solid-router): loaders being called twice during SSR #4574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/solid-router/src/Transitioner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand Down
70 changes: 70 additions & 0 deletions packages/solid-router/tests/Transitioner.test.tsx
Original file line number Diff line number Diff line change
@@ -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: () => <div>Index</div>,
})

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(() => <RouterProvider router={router} />)

// 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: () => <div>Index</div>,
})

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(() => <RouterProvider router={router} />)

// Wait for the createRenderEffect to run and call router.load()
await waitFor(() => {
expect(loadSpy).toHaveBeenCalledTimes(0)
})

loadSpy.mockRestore()
})
})
4 changes: 2 additions & 2 deletions packages/solid-router/tests/router.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1574,15 +1574,15 @@ 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(() => <RouterProvider router={router} />)

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()

Expand Down