React Router处理OpenSearch建议

时间:2018-11-14 19:45:45

标签: react-router service-worker search-suggestion opensearch

是否可以让React Router处理带有JSON响应的OpenSearch建议查询?

事情看起来像这样:

  • index.html: <link rel="search" type="application/opensearchdescription+xml" href="/opensearch.xml" title="Search">

  • opensearch.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
        <ShortName>Search</ShortName>
        <Description>Search</Description>
        <InputEncoding>UTF-8</InputEncoding>
        <Image height="16" width="16" type="image/x-icon">https://host/favicon.ico</Image>
        <Url type="text/html" method="get" template="https://host/search?q={searchTerms}" />
        <Url type="application/x-suggestions+json"
    template="https://host/suggest?q={searchTerms}">
        <Query role="example" searchTerms="RDF"/>
    </Url>
    

  • index.js

    ReactDOM.render(
      <Provider store={store()}>
        <Router>
          <div>
            <Route exact path="/" component={App} />
            <Route path='/suggest' render={(event) => {
              console.log('Route Suggest', { event });
              return ["fir", ["firefox", "first choice", "mozilla firefox"]];
            }}/>
          </div>
        </Router>
      </Provider>,
      document.getElementById('root')
    );
    

在服务器端,nginx日志中有:

==> /var/log/nginx/access.log <==
[14/Nov/2018:20:09:50 +0100] "GET /suggest?q=foo HTTP/2.0" 200 1167 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:63.0) Gecko/20100101 Firefox/63.0" "-"

但浏览器搜索框中未返回任何内容。而且客户端控制台中没有任何痕迹。

此外,服务工作者的GET /suggest?q=foo事件未处理fetch

React Router可以处理这样的查询并返回JSON响应吗?还是通过与Service Worker的拦截查询?

0 个答案:

没有答案