Skip to content
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

createLazyFileRoute function doesn't have validateSearch property #1209

Closed
skaionedev opened this issue Feb 22, 2024 · 4 comments
Closed

createLazyFileRoute function doesn't have validateSearch property #1209

skaionedev opened this issue Feb 22, 2024 · 4 comments

Comments

@skaionedev
Copy link

skaionedev commented Feb 22, 2024

Describe the bug

I can't use validateSearch property for lazy file routes as it's for regular file routes
Screenshot 2024-02-22 at 16 39 03

Steps to Reproduce the Bug or Issue

  1. create a fresh vite + react app with router
  2. create lazy file route (e.g. about.lazy.tsx)

Expected behavior

I expect lazy file routes have same properties as regular file routes

Platform

  • OS: macOS
  • Browser: Brave
  • Version: 1.62.165

Additional context

No response

@SeanCassiere
Copy link
Member

SeanCassiere commented Feb 23, 2024

The createLazyFileRoute helper is meant to be used for routes that are being code-split. For example src/routes/about.lazy.tsx. This configuration only lets you define the components for a specific Route that are then imported at runtime using code-splitting.

When you need to define the Route configuration (such as configuring the loader, beforeLoad, validateSearch, etc.), it is done in a separate file which would be named as src/routes/about.tsx. In here, you would use the createFileRoute helper to define the Route configuration itself.

import { createFileRoute } from '@tanstack/react-router'
import { z } from 'zod'
export const Route = createFileRoute("/about")({
  validateSearch: (search) => z.object({ foo: z.string().catch("bar") }).parse(search)
});

TLDR; You'll need both about.lazy.tsx AND about.tsx if you want the about route to be code-split whilst utilizing the extensible Route configuration.

Edit: You can see this in action in the Basic (file based) example in the posts.tsx and the posts.lazy.tsx files.

@ShellWen
Copy link

It should be pinned, lots of developers are very confused about this feature.

@SeanCassiere
Copy link
Member

It should be pinned, lots of developers are very confused about this feature.

This has been stated in docs over here, but it's easy to understand why someone could miss it. Tanstack Router covers a lot, and the fact that virtual routes "just work" means people don't see fully read the page on code-splitting and how it works.

IMO: Tanstack Router is definitely not the most beginner friendly solution at the moment, and honestly I don't think it ever will be given the kinds of complex stuff it aims to do.

But we should see alot of the documentation pages getting a cleanup when we begin the push for v2.

@skaionedev
Copy link
Author

@SeanCassiere Thank you 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants