-
Notifications
You must be signed in to change notification settings - Fork 3
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
Next.js 13+ app directory support #3
Comments
Hi @chrisjh, Thank you for reaching out! Next.js 13+ directory structure is definitely something we want to support. Thanks again for raising this. I appreciate it. 🙏 |
Is there a simple way to get this to work with Next 13? Would be great! |
I put together a quick implementation for this. I am not sure yet how to differentiate between an API handler and a Route handler, but the code below should work. type NextRouteHandler = (
request: NextRequest,
context: NextRouteHandlerContext
) => Promise<NextResponse> | NextResponse;
type NextRouteHandlerContext = { params: Record<string, string | string[]> };
export type LogtailNextRequest = NextRequest & { log: Logger }
export type LogtailRouteHandler = (
request: LogtailNextRequest,
context: NextRouteHandlerContext
) => Promise<NextResponse> | NextResponse;
export function withLogtailRouteHandler(handler: LogtailRouteHandler): NextRouteHandler {
return async (req, ctx) => {
const report: RequestReport = {
startTime: new Date().getTime(),
path: `${req.nextUrl.pathname}${req.nextUrl.search}`,
method: req.method,
host: req.headers.get('host') || '',
userAgent: req.headers.get('user-agent') || '',
scheme: 'https',
ip: req.ip,
region: config.region,
}
const runtime = (
typeof globalThis.EdgeRuntime === 'undefined' &&
process.env.NEXT_RUNTIME != 'edge'
) ? 'lambda' : 'edge';
const logger = new Logger({}, report, false, runtime);
const logtailRequest = req as LogtailNextRequest;
logtailRequest.log = logger;
try {
const res = await handler(logtailRequest, ctx);
logger.attachResponseStatus(res.status);
await logger.flush();
return res;
} catch (error: any) {
logger.error('Error in Route Handler', { error });
logger.attachResponseStatus(500);
await logger.flush();
throw error;
}
}
} AFAIK, route handlers need to return a Happy to open a PR if this is acceptable :) |
Thanks @marceloclp - nice to see others are interested in a solution here! Can you share an example of how this works with Next.js 13's app directory named route handlers (e.g. Next expects the # /app/api/route.ts
export async function GET(request: Request) {} https://nextjs.org/docs/app/building-your-application/routing/route-handlers#convention |
It should be fine to use arrow functions instead: export const GET = withLogtailRouteHandler((req) => {
return NextResponse.json({})
})
export const PUT = async () => {} An alternative could be: export withLogtailRouteHandler(
function GET() {}
) {}
export withLogtailRouteHandler(
function PUT() {}
) {} But AFAIK it virtually doesn't matter if you use arrow functions as Next does not make use of |
Ah, clever! Makes sense, will try it out in our app today. |
Thank you for the interim solution. Looking forward to having it out of the box, cheers! |
@curusarn after 1,5 years, it would be nice to finally see the implementation in the lib. This should be a quick thing to do right? |
nice code. But trying to use it in my test route gives me a build error.
Cant get a handle on this. Most likely a Promise thing? Hard to read.... UPDATE: this fixed it:
|
Are there any plans to add Next.js 13 app directory support to this package?
There's a few things that don't quite work the same between pages and app directory related to structured logging and API routes:
The new app route handler in Next.js 13 has specific named exports (GET, POST, PUT, DELETE). The current version of logtail-nextjs wraps the pages api handler with
withLogtail(handler)
which no longer works with the new named exports. Additionally, Next changed the types toNextResponse
andNextRequest
fromNextApiResponse
andNextApiRequest
.The text was updated successfully, but these errors were encountered: