Skip to content
This repository has been archived by the owner on Apr 12, 2020. It is now read-only.

How to handle redirects with parameters? #6

Closed
n1313 opened this issue May 5, 2019 · 3 comments
Closed

How to handle redirects with parameters? #6

n1313 opened this issue May 5, 2019 · 3 comments

Comments

@n1313
Copy link
Contributor

n1313 commented May 5, 2019

This router configuration

<Router>
  <Route path="/" redirect={`/date/${today}`} />
  <Route path="/date/:date" component={DayPage} />
</Router>

gives me Error: svero expects <Route redirect="/date/2019-05-05"> to send to an existing route. /date/2019-05-05 does not exists.. Is there a way to do this kind of redirect inside the router?

@kazzkiq
Copy link
Owner

kazzkiq commented May 6, 2019

No. As of today redirect only accepts non-dynamic existing route paths.

In your scenario, the best option would be something like that:

<Router>
  <Route path="/" component={IndexPage} />
  <Route path="/date/:date" component={DayPage} />
</Router>

And in your IndexPage.svelte:

<script>
  import { onMount } from 'svelte';

  const today = // Your code here...

  onMount(() => {
    history.pushState({}, '', `/date/${today}`);
    const popEvent = new Event('popstate');
    window.dispatchEvent(popEvent);
  });
</script>

@n1313
Copy link
Contributor Author

n1313 commented May 7, 2019

Thanks!

@n1313 n1313 closed this as completed May 7, 2019
@kazzkiq
Copy link
Owner

kazzkiq commented May 7, 2019

Just a reminder that v0.3.0+ now features the navigateTo() method, so the code would be even smaller:

<script>
  import { onMount } from 'svelte';
  import { navigateTo } from 'svero';

  const today = // Your code here...

  onMount(() => {
    navigateTo(`/date/${today}`);
  });
</script>

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

No branches or pull requests

2 participants