Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API resolved without sending a response with intermediate logger using req.log.with #119

Open
nikosantis opened this issue Apr 12, 2023 · 1 comment

Comments

@nikosantis
Copy link

I am using Next.js 13.3.0 along with version 0.17.0 of the next-axiom library to handle API routing in my application. I have created an intermediate logger using req.log.with to customize the logs in my API endpoint handlers. However, I am experiencing an issue where responses are not sent correctly and warnings are generated in the console.

The issue seems to be related to the use of req.log.with, as when I use it, responses are not sent correctly and requests are left hanging, resulting in the following warning in the console:

"API resolved without sending a response for /api/[endpoint], this may result in stalled requests."

This issue only occurs when I use req.log.with, as if I only use req.log.info for logging, no warnings are generated and responses are sent correctly.

I have found a workaround by adding the following config object to my API route:

export const config = {
  api: {
    externalResolver: true,
  },
}

This seems to resolve the issue, but I'm not sure if it's the proper implementation or not, as there is no information available in the documentation of next-axiom.

Here are the versions I am using:

Next.js: 13.3.0
next-axiom: 0.17.0
Steps to reproduce the issue:

Create an intermediate logger using req.log.with in an API endpoint handler in Next.js.
Make a request to that endpoint from a client.
Observe that the response is not sent correctly and warnings are generated in the console.

eg:

import type { NextApiResponse } from 'next'
import { AxiomAPIRequest, withAxiom } from 'next-axiom'

async function handler(req: AxiomAPIRequest, res: NextApiResponse) {
  const logger = req.log.with({
    timestamp: new Date(Date.now()).toISOString(),
    env: process.env.NODE_ENV,
    node_version: process.version,
  })
  logger.info('test', {
    data: { url: req.url, method: req.method },
  })
  res.status(200).json({ message: 'Message sent' })
}

export default withAxiom(handler)

I was trying to create a function createLogger:

eg:

import { Logger } from 'next-axiom'
import pckJson from 'package.json'

function createLogger(logger: Logger) {
  const log = logger.with({
    timestamp: new Date(Date.now()).toISOString(),
    env: process.env.NODE_ENV,
    node_version: process.version,
    project_version: pckJson.version,
    next_version: pckJson.dependencies.next,
  })
  return log
}

export { createLogger }
@bahlo
Copy link
Member

bahlo commented Jun 19, 2023

Hey @nikosantis, sorry for getting back so late -- from a quick look I'm surprised that with would introduce these problems, I'll try to reproduce using your code, thanks for bringing this issue to our attention 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants