Sapper: Routes should unsubscribe from the page store before $page.params are updated

Created on 26 Apr 2019  路  2Comments  路  Source: sveltejs/sapper

Expected Behavior

A route autosubscribing to $page.params will be unsubscribed before $page.params is changed to a new route

Actual Behavior

The autosubscription is being triggered late exactly once, i.e. if I navigate from channels/[slug].svelte to games/[slug].svelte, the Channel route's subscription will be triggered exactly once with the slug param for the Game route

bug

Most helpful comment

I'm facing the same issue, I also tried to unsubscribe in onDestroy hook but it's trigger too late. Have you find a workaround ? Actualy, I compare route path to prevent function :

const path = $page.path;

const pageUnsubscribe = page.subscribe((p) => {
    if (p.path !== path) {
      return;
    }
    const min = new Date(p.query.date);
    const max = new Date(p.query.date);
    min.setUTCHours(0, 0, 0, 0);
    max.setUTCHours(23, 59, 59, 999);

    store.search(min, max);
});

But i'm not a big fan :smile:

All 2 comments

I'm facing the same issue, I also tried to unsubscribe in onDestroy hook but it's trigger too late. Have you find a workaround ? Actualy, I compare route path to prevent function :

const path = $page.path;

const pageUnsubscribe = page.subscribe((p) => {
    if (p.path !== path) {
      return;
    }
    const min = new Date(p.query.date);
    const max = new Date(p.query.date);
    min.setUTCHours(0, 0, 0, 0);
    max.setUTCHours(23, 59, 59, 999);

    store.search(min, max);
});

But i'm not a big fan :smile:

This has been fixed in 0.27.13, thank you @pchynoweth!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Rich-Harris picture Rich-Harris  路  3Comments

matt3224 picture matt3224  路  4Comments

BMorearty picture BMorearty  路  3Comments

milosdjakovic picture milosdjakovic  路  3Comments

benmccann picture benmccann  路  3Comments