Skip to content

Commit

Permalink
Fix dynamic server usage in app router docs route (#168)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
blomqma authored May 10, 2024
1 parent 2feaca1 commit d4e5109
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 14 deletions.
6 changes: 0 additions & 6 deletions docs/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions packages/next-rest-framework/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions packages/next-rest-framework/src/app-router/docs-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down

0 comments on commit d4e5109

Please sign in to comment.