choojs / choo

:steam_locomotive::train: - sturdy 4kb frontend framework
https://choo.io/
MIT License
6.78k stars 595 forks source link

Redirects do not work #684

Closed NetOpWibby closed 5 years ago

NetOpWibby commented 5 years ago

Expected behavior

emit("replaceState", "/") redirects a user to the specified URL.

Actual behavior

Nothing happens. Even the official site has this issue.

Steps to reproduce behavior

Put emit("replaceState", "/") inside a route and be sad that you don't get redirected.


I'm using fastify for the server so I'll try intercepting choo to make redirects happen via fastify instead. I'd rather do it programmatically in choo though.

NetOpWibby commented 5 years ago

This is the way I am doing redirects now: https://github.com/lbryio/lbry.tech/blob/master/app/modules/redirect-404.js

goto-bus-stop commented 5 years ago

Yea this is a problem we've had for a while. You can wrap the emit call inside a setTimeout(() => {}, 0) to work around it for now

NetOpWibby commented 5 years ago

So the first instance of the redirect is here: https://github.com/lbryio/lbry.tech/blob/master/app/views/redirect.js#L51

// relevant portion
if (!fs.existsSync(`./documents/${path}.md`))
  return redirect404(state, emit); // when I add emit here, it does get passed to my redirect-404.js file   
// ...but replacing the HTML in that file with the code below results in nothing
setTimeout(() => {
  emit("replaceState", redirectUrl);
}, 0);