Gatsby: 不要在哈希更新上触发onRouteUpdate

创建于 2017-10-02  ·  3评论  ·  资料来源: gatsbyjs/gatsby

我从gatsby-browser.js导出onRouteUpdate gatsby-browser.js像这样:

export const onRouteUpdate = ({ location, action }) => {
    if (
        window.location &&
        typeof window.location.href === 'string' &&
        window.location.href.includes('www.example.com') // make sure we're on live site
    ) {
        window.analytics.page();
    } else {
        console.log('PAGE: ', window.location.href, action, location);
    }
    return;
};

这可以正常工作,但是当页面上有锚链接时,每次单击其中的一个都会触发。

我尝试仅在action === 'PUSH'路由更新上触发page() ,但这会在所有初始页面加载时阻止该事件。

有什么方法可以从此跟踪中删除锚链接事件?

documentation question or discussion

所有3条评论

@brandonmp如果存在#字符,也许可以签入window.location.href吗?

@sebastienfi,他说的是哈希更新(单击锚点时发生的情况)不应触发onRouteUpdate API。 这对我来说似乎很合理。 哈希更新仅应在旨在仅在切换页面时触发API的单个页面内进行。

@brandonmp希望PR在这里过滤掉同一页面中的哈希变化! https://github.com/gatsbyjs/gatsby/blob/84362074df2238e6794bb6a056e0163ac1c0dfdd/packages/gatsby/cache-dir/production-app.js#L112

也许将函数放在另一个文件中并添加一些测试?

不知道何时,但我们不会在更改路线时触发onRouteUpdate

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

rossPatton picture rossPatton  ·  3评论

andykais picture andykais  ·  3评论

dustinhorton picture dustinhorton  ·  3评论

theduke picture theduke  ·  3评论

jimfilippou picture jimfilippou  ·  3评论