Dynamic server usage: Route /api/locations couldn't be rendered statically because it accessed `next
Unanswered
Spotted Owl posted this in #help-forum
Spotted OwlOP
I have a NextJS API router handler that uses tRPC and searchParams to accept external request to the web app.
Here is what I started with:
After reading the docs I ended up with this:
The updates resolved the error when building and running the app in production mode locally but I am still getting these errors on Vercel.
Here is what I started with:
import type { NextRequest, NextResponse } from 'next/server';
import { api } from '@/trpc/server';
export const GET = async (req: NextRequest, _res: NextResponse) => {
try {
const { searchParams } = new URL(req.url);
const startDate = searchParams.get('start');
const endDate = searchParams.get('end');
if (!startDate || !endDate) {
throw new Error('Start and end time are required');
}
const coords = await api.nmea.getExpeditionPath.query({
order: 'DESC',
start: startDate ?? '',
end: endDate ?? '',
intervalSeconds: 3600 * 4,
});
return new Response(JSON.stringify(coords));
} catch (e) {
console.error(e);
return new Response(e instanceof Error ? e.message : 'Server Error');
}
};After reading the docs I ended up with this:
import type { NextRequest, NextResponse } from 'next/server';
import { api } from '@/trpc/server';
export const dynamic = 'force-dynamic';
export const GET = async (req: NextRequest, _res: NextResponse) => {
try {
const searchParams = req.nextUrl.searchParams;
const startDate = searchParams.get('start');
const endDate = searchParams.get('end');
if (!startDate || !endDate) {
throw new Error('Start and end time are required');
}
const coords = await api.nmea.getExpeditionPath.query({
order: 'DESC',
start: startDate ?? '',
end: endDate ?? '',
intervalSeconds: 360,
});
return new Response(JSON.stringify(coords));
} catch (e) {
console.error(e);
return new Response(e instanceof Error ? e.message : 'Server Error');
}
};The updates resolved the error when building and running the app in production mode locally but I am still getting these errors on Vercel.