Giter Site home page Giter Site logo

Comments (9)

Brendonovich avatar Brendonovich commented on August 30, 2024 1

Throwing the redirect also works, but the important part is calling loadData outside of the load function, since thrown/returned redirects will be ignored when called from load.
https://stackblitz.com/edit/github-fk8exj-mcht9p?file=src%2Froutes%2Findex.tsx

from solid-start.

brenelz avatar brenelz commented on August 30, 2024 1

I feel this is intended behavior at the moment. Thinking we can probably close this issue?

from solid-start.

brenelz avatar brenelz commented on August 30, 2024

So a couple things you need to do. You need to return instead of throw the redirect. Secondly you need to wrap in the cache function. Also you need to call the function in createAsync()

const loadData = cache(() => {
  'use server'
  return redirect('/about')
}, "load-data");

export const route = {
  load: () => loadData()
}

export default function Home() {
  const data = createAsync(() => loadData());
}

from solid-start.

maksimsemenov avatar maksimsemenov commented on August 30, 2024

What if I don't want to cache the request? For example, if I want to validate some auth-link with some hash parameter, and then want to redirect user. I don't think I want to cache the response in this case.

from solid-start.

Brendonovich avatar Brendonovich commented on August 30, 2024

Use action, it's for pretty much everything that isn't a data fetch

from solid-start.

maksimsemenov avatar maksimsemenov commented on August 30, 2024

Can I use action on page load? The use-case I'm trying to solve is:

  1. I send email to the user with a unique link
  2. User clicks the link and is taken to the link validation page
  3. I validate link on the server
  4. If validation is successful, I redirect the user
  5. If validation is not successful, I want to show some error state with some next action, so user is not lost.

I also, don't want to cash that response, so if link is clicked twice, it is no longer valid

from solid-start.

Brendonovich avatar Brendonovich commented on August 30, 2024

Yeah that should work fine

from solid-start.

maksimsemenov avatar maksimsemenov commented on August 30, 2024

So should it be like this:

const loadData = action(() => {
  'use server'
  return redirect('/about')
});

export const route = {
  load: () => loadData()
}

export default function Home() {
  const data = createAsync(() => loadData());
}

from solid-start.

ryansolid avatar ryansolid commented on August 30, 2024

Yeah this is by design since the load function is more of like a lifecycle hook and the server function is just an async function. One could catch the errored response themselves and then call the client side navigation APIs with it but I don't know if that is what you are going for. The name cache is probably misleading because on the server it only lasts the lifetime of the request. It is possible it would just work. We're reviewing some of the naming.

I think an action is fine, but I think cache more fits the semantics because it is getting is validated on a hash. But either probably is fine here.

from solid-start.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.