Skip to content

Commit

Permalink
add pathname normalizer for actions (#64592)
Browse files Browse the repository at this point in the history
This adds a normalizer to handle `.action` invokes (when in minimal
mode). This PR by itself does not do anything of note, but will be
hooked up in a future PR in the [Next.js
builder](https://github.com/vercel/vercel/tree/main/packages/next).

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes NEXT-3131
  • Loading branch information
ztanner committed Apr 17, 2024
1 parent 8d18e0c commit 6f27f94
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/next/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const PRERENDER_REVALIDATE_ONLY_GENERATED_HEADER =

export const RSC_PREFETCH_SUFFIX = '.prefetch.rsc'
export const RSC_SUFFIX = '.rsc'
export const ACTION_SUFFIX = '.action'
export const NEXT_DATA_SUFFIX = '.json'
export const NEXT_META_SUFFIX = '.meta'
export const NEXT_BODY_SUFFIX = '.body'
Expand Down
12 changes: 11 additions & 1 deletion packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ import getRouteFromAssetPath from '../shared/lib/router/utils/get-route-from-ass
import { stripInternalHeaders } from './internal-utils'
import { RSCPathnameNormalizer } from './future/normalizers/request/rsc'
import { PostponedPathnameNormalizer } from './future/normalizers/request/postponed'
import { ActionPathnameNormalizer } from './future/normalizers/request/action'
import { stripFlightHeaders } from './app-render/strip-flight-headers'
import {
isAppPageRouteModule,
Expand Down Expand Up @@ -373,6 +374,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
protected readonly localeNormalizer?: LocaleRouteNormalizer

protected readonly normalizers: {
readonly action: ActionPathnameNormalizer | undefined
readonly postponed: PostponedPathnameNormalizer | undefined
readonly rsc: RSCPathnameNormalizer | undefined
readonly prefetchRSC: PrefetchRSCPathnameNormalizer | undefined
Expand Down Expand Up @@ -467,6 +469,10 @@ export default abstract class Server<ServerOptions extends Options = Options> {
data: this.enabledDirectories.pages
? new NextDataPathnameNormalizer(this.buildId)
: undefined,
action:
this.enabledDirectories.app && this.minimalMode
? new ActionPathnameNormalizer()
: undefined,
}

this.nextFontManifest = this.getNextFontManifest()
Expand Down Expand Up @@ -957,7 +963,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {

let { pathname: urlPathname } = new URL(req.url, 'http://localhost')

// For ISR the URL is normalized to the prerenderPath so if
// For ISR the URL is normalized to the prerenderPath so if
// it's a data request the URL path will be the data URL,
// basePath is already stripped by this point
if (this.normalizers.data?.match(urlPathname)) {
Expand Down Expand Up @@ -1410,6 +1416,10 @@ export default abstract class Server<ServerOptions extends Options = Options> {
normalizers.push(this.normalizers.rsc)
}

if (this.normalizers.action) {
normalizers.push(this.normalizers.action)
}

for (const normalizer of normalizers) {
if (!normalizer.match(pathname)) continue

Expand Down
13 changes: 13 additions & 0 deletions packages/next/src/server/future/normalizers/request/action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { PathnameNormalizer } from './pathname-normalizer'

import { ACTION_SUFFIX } from '../../../../lib/constants'
import { SuffixPathnameNormalizer } from './suffix'

export class ActionPathnameNormalizer
extends SuffixPathnameNormalizer
implements PathnameNormalizer
{
constructor() {
super(ACTION_SUFFIX)
}
}

0 comments on commit 6f27f94

Please sign in to comment.