Skip to content

[Content]: redirect in query confusion #1384

@zhengkyl

Description

@zhengkyl

📚 Subject area/topic

Auth and data fetching

📋 Page(s) affected (or suggested, for new content)

https://docs.solidjs.com/solid-start/advanced/auth
https://docs.solidjs.com/solid-start/advanced/return-responses
https://docs.solidjs.com/solid-start/guides/data-fetching
https://docs.solidjs.com/solid-router/reference/response-helpers/redirect

📋 Description of content that is out-of-date or incorrect

There is a common mistake people run into when created an authenticated query. They have a query that redirects after the initial streamed response and get a confusing Cannot set headers after they are sent to the client error. Since this only happens with streaming, it doesn't occur when navigating to a page after initial page load, which makes it more confusing.
solidjs/solid-start#1979
solidjs/solid-start#2025
solidjs/solid-start#2023
solidjs/solid-start#1692
https://www.answeroverflow.com/m/1256901524974604308

Even the docs make this mistake, so it's not obvious.

// https://docs.solidjs.com/solid-start/advanced/auth
const getPrivatePosts = query(async function() {
  "use server"
  const user = await getUser()
  if(!user) {
    throw redirect("/login");
  }
  return db.getPosts({ userId: user.id, private: true })
})

export default function Page() {
  const posts = createAsync(() => getPrivatePosts());
}
  1. { deferStream: true } should be added to the example(s).
  2. Streaming is not well understood. Many people (including me) didn't even know it's happening.
  3. Solid Router's redirect() behaves very differently and feels like black magic. Server vs client redirect, query vs action. throwing vs returning. Right now the only way to know is looking at source code.

🖥️ Reproduction in StackBlitz (if reporting incorrect content or code samples)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions