1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SSRの時間計測方法(Nextjs)

Last updated at Posted at 2019-04-17

Nextjsはlibの中にrenderToStringが入っていて純粋にrenderToStringのみの計測ができなそうなので、以下のような感じで妥協するしかなさそう。

const serverTiming = require('server-timing')

const ssrTiming = async ({ req, res, pagePath, queryParams }) => {
  res.startTime('ssr', 'SSR')
  const html = await app.renderToHTML(req, res, pagePath, queryParams)
  res.endTime('ssr')
  res.send(html)
}

app.prepare()
  .then(() => {
    const server = express()
    server.use(serverTiming())

    // SSRの時間を計測したいパス
    server.get('/', (req, res) => {
      return ssrTiming({ req, res, pagePath: '/' }))
    }

    server.get('*', (req, res) => {
      return handle(req, res)
    })

    server.listen(port, (err) => {
      if (err) throw err
      console.log('> Ready on http://localhost:' + port)
    })
  })
  .catch((ex) => {
    console.error(ex.stack)
    process.exit(1)
  })
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?