From 6825ae1feb0b0fb9b09b8dfd72f660e58e61793d Mon Sep 17 00:00:00 2001 From: Markus Blomqvist Date: Fri, 10 May 2024 13:24:19 +0300 Subject: [PATCH] Fix dynamic server usage in app router docs route Currently in the app router docs route we read the request headers directly from the original request object. This is problematic because it leads to a dynamic server usage error unless the docs endpoint is explicitly set to `force-dynamic` caching strategy. By reading the headers from a cloned request object the docs endpoint can be used with the default caching strategy, fixing this issue. --- docs/docs/getting-started.md | 6 ------ packages/next-rest-framework/README.md | 6 ------ packages/next-rest-framework/src/app-router/docs-route.ts | 7 +++++-- 3 files changed, 5 insertions(+), 14 deletions(-) diff --git a/docs/docs/getting-started.md b/docs/docs/getting-started.md index 7fa18b7..1d9ca55 100644 --- a/docs/docs/getting-started.md +++ b/docs/docs/getting-started.md @@ -159,12 +159,6 @@ export const { GET, POST } = route({ The `TypedNextResponse` ensures that the response status codes and content-type headers are type-checked against the defined outputs. You can still use the regular `NextResponse` if you prefer to have less type-safety. -When using the default `nodejs` runtime with app router routes (`docsRoute` or `route`), you may encounter the [Dynamic server usage](https://nextjs.org/docs/messages/dynamic-server-error) Next.js error when running `next build`. In that case you should force the route to be dynamically rendered with the [dynamic](https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic) option: - -```typescript -export const dynamic = 'force-dynamic'; -``` - ##### [Pages router API route](#pages-router-api-route): ```typescript diff --git a/packages/next-rest-framework/README.md b/packages/next-rest-framework/README.md index a2244b1..4f1f4f2 100644 --- a/packages/next-rest-framework/README.md +++ b/packages/next-rest-framework/README.md @@ -251,12 +251,6 @@ export const { GET, POST } = route({ The `TypedNextResponse` ensures that the response status codes and content-type headers are type-checked against the defined outputs. You can still use the regular `NextResponse` if you prefer to have less type-safety. -When using the default `nodejs` runtime with app router routes (`docsRoute` or `route`), you may encounter the [Dynamic server usage](https://nextjs.org/docs/messages/dynamic-server-error) Next.js error when running `next build`. In that case you should force the route to be dynamically rendered with the [dynamic](https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic) option: - -```typescript -export const dynamic = 'force-dynamic'; -``` - ##### [Pages router API route](#pages-router-api-route): ```typescript diff --git a/packages/next-rest-framework/src/app-router/docs-route.ts b/packages/next-rest-framework/src/app-router/docs-route.ts index 32d001c..88f2cab 100644 --- a/packages/next-rest-framework/src/app-router/docs-route.ts +++ b/packages/next-rest-framework/src/app-router/docs-route.ts @@ -10,9 +10,12 @@ import { export const docsRoute = (_config?: NextRestFrameworkConfig) => { const config = getConfig(_config); - const handler = async (req: NextRequest, _context: { params: BaseQuery }) => { + const handler = async ( + _req: NextRequest, + _context: { params: BaseQuery } + ) => { try { - const host = req.headers.get('host') ?? ''; + const host = _req.clone().headers.get('host') ?? ''; const html = getHtmlForDocs({ config, host }); return new NextResponse(html, {