【问题标题】:browser-sync for SPA: how to sync document fragments: #/contactSPA 的浏览器同步:如何同步文档片段:#/contact
【发布时间】:2015-09-04 14:00:04
【问题描述】:

我正在开发一个 SPA(单页应用程序)并使用 grunt-browsers-sync。 所有浏览器同步功能似乎都有效:CSS 注入、滚动和表单同步。

这是一个 SPA,所以没有导航到其他页面。导航是通过文档片段中的路由完成的(我为此使用了 SammyJs 库)

mysite.com/#/home
mysite.com/#/contact
mysite.com/#/...

似乎 BrowserSync 不同步文档片段。 我认为这是因为文档片段由浏览器处理,而不是 BrowserSync 服务器/代理请求。

有什么方法可以让场景发挥作用吗?

PS:我在导航时有一个 javascript 回调,我可以使用它在开发时将新 url 发送到 BrowserSync(如果 BrowserSync 支持类似的东西)

【问题讨论】:

    标签: single-page-application


    【解决方案1】:

    我还尝试将浏览器同步用于单页主干应用程序。

    路由更改基本上是在单击锚点时触发的。不幸的是,bro​​wser-sync 不能很好地处理具有 stopPropagation 的事件,因此,在其他浏览器中没有触发点击,并且路由已同步。

    从那以后,我已经分叉并修复了这个问题以及其他问题,即 mouseup、mousedown、keyup、keydown 和 contenteditable div 的事件同步。

    pull-request 仍处于等待状态,因此您可以同时使用来自https://github.com/nitinsurana/browser-sync-client 的 browser-sync-client

    您需要进行如下配置才能使修复生效。请注意浏览器同步中不存在的 capture、contenteditable、mouseup 和其他配置选项

    var bs     = require("browser-sync").create();
    var client = require("./");
    
    client["plugin:name"] = "client:script";
    
    bs.use(client);
    
    bs.init({
        //server: {
        //    baseDir: ["test/fixtures"]
        //},
        proxy: 'http://localhost:8080',
        open: false,
        minify: false,
        snippetOptions: {
            rule: {
                //match: /SHNAE/,
                match: /<\/head>/i,
                fn: function (snippet) {
                    return snippet + "\n</head>";
                }
            }
        },
        clientEvents: [
            "scroll",
            "input:text",
            "input:toggles",
            "input:keydown",
            "input:keypress",
            "form:submit",
            "form:reset",
            "click",
            "contenteditable:input",
            "mouseup",
            "mousedown",
            "select:change"
        ],
        ghostMode: {
            clicks: true,
            scroll: true,
            forms: {
                submit: true,
                inputs: true,
                toggles: true,
                keypress: true,
                keydown: true,
                contenteditable: true,
                change: true
            },
            mouseup: true,
            mousedown: true
        },
        capture:true
    });
    

    【讨论】:

      猜你喜欢
      • 2016-09-15
      • 2021-04-04
      • 2017-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-02
      • 2015-04-15
      相关资源
      最近更新 更多