无法在服务器上进行这种动态搜索(当用户输入时)。这需要在 UI 上处理为per docs
对于初始页面加载,getInitialProps 将仅在服务器上执行。
getInitialProps 只会在客户端导航到
通过 Link 组件或使用路由 API 实现不同的路由。
将在服务器上预呈现的唯一搜索是重新加载该页面的搜索。
例子
<form action="/search-enpoint">
<input name="search" type="text" placeholder="Search" />
<button>Find</button>
</form>
此表单将提交至/search-enpoint?search=qwe,您可以在custom server 中获取该表单
// server.js
const { createServer } = require('http');
const { parse } = require('url');
const next = require('next');
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
createServer((req, res) => {
const parsedUrl = parse(req.url, true);
const { pathname, query } = parsedUrl;
if (pathname === '/search-enpoint') {
app.render(req, res, '/index', query);
} else {
handle(req, res, parsedUrl);
}
}).listen(3000, err => {
if (err) throw err;
console.log('> Ready on http://localhost:3000');
});
});
但同样,这个搜索不是动态的。
希望这会有所帮助。