Typeahead.js 远程工作,但预取不

Typeahead.js remote works, but prefetch doesn't

我在我的网络应用程序中使用 typeahead.js 0.10.5。由于某些奇怪的原因,通过远程工作实时获取建议,而预取已损坏。这里发生了一些不明显和奇怪的事情。根据开发控制台和 Chrome 的网络监视器,它甚至没有对页面加载进行查询。当然,它会在我开始输入时进行查询。

这真的让我很困惑-我在这里做错了什么?

    // Instantiate the Bloodhound suggestion engine
    var tags = new Bloodhound({
      datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
      queryTokenizer: Bloodhound.tokenizers.whitespace,
      remote: {
        url: '/tags/tags/search.json?q=%QUERY',
        filter: function (taglist) {
            // Map the remote source JSON array to a JavaScript object array
            return $.map(taglist, function (tag) {
                console.log(tag);
                return {
                    value: tag.tag
                };
            });
        }
      },
      prefetch: {
        url: '/tags/tags/search.json?q=',
        filter: function (taglist) {
            // Map the remote source JSON array to a JavaScript object array
            return $.map(taglist, function (tag) {
                console.log(tag);
                return {
                    value: tag.tag
                };
            });
        },      
      }
    });

    // Initialize the Bloodhound suggestion engine
    tags.initialize();

    // Instantiate the Typeahead UI
    $('#search-tags').typeahead(null, {
        displayKey: 'value',
        source: tags.ttAdapter(),
        hint: true,
        highlight: true
    });

尝试从浏览器的本地存储中删除条目并重新开始。

Bloodhound object 默认情况下将预取数据缓存在浏览器的本地存储中,为其分配 1 天的 TTL (time-to-live),并且在 TTL 到期之前不会重新验证它。当 Bloodhound object 初始化时,可以更改默认设置 "cache: false" and/or "ttl: 1000"(毫秒)。

Pre-fetching 是相关的,但与缓存略有不同,因为 pre-fetched 数据不受服务器发送的 Cache-Control headers 的约束。它也存在于 LocalStorage 中,而不是浏览器的缓存中(这就是为什么硬重新加载或缓存清除不会导致它成为 re-fetched)。

另一方面,远程获取的文件根据 Cache-Control headers 进行重新验证。因此,如果服务器允许,浏览器可能仍会缓存它们。但是,它们存储在缓存中,而不是在 LocalStorage 中。

LocalStorage 中存在 per-domain space 限制(请参阅 What is the max size of localStorage values?),因此大 pre-fetches 会失败,但我不知道 typeahead 是否正常失败(即,使用数据,即使它不能存储它)。