From 6f27f944c9e46a3a1868aad676f8d9fd304e6eb6 Mon Sep 17 00:00:00 2001 From: Zack Tanner <1939140+ztanner@users.noreply.github.com> Date: Wed, 17 Apr 2024 10:05:08 -0700 Subject: [PATCH] add pathname normalizer for actions (#64592) 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). Closes NEXT-3131 --- packages/next/src/lib/constants.ts | 1 + packages/next/src/server/base-server.ts | 12 +++++++++++- .../src/server/future/normalizers/request/action.ts | 13 +++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 packages/next/src/server/future/normalizers/request/action.ts diff --git a/packages/next/src/lib/constants.ts b/packages/next/src/lib/constants.ts index ed3679d06273d..825cb114c7665 100644 --- a/packages/next/src/lib/constants.ts +++ b/packages/next/src/lib/constants.ts @@ -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' diff --git a/packages/next/src/server/base-server.ts b/packages/next/src/server/base-server.ts index 3a95c154ff6ae..16d03b945da12 100644 --- a/packages/next/src/server/base-server.ts +++ b/packages/next/src/server/base-server.ts @@ -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, @@ -373,6 +374,7 @@ export default abstract class Server { protected readonly localeNormalizer?: LocaleRouteNormalizer protected readonly normalizers: { + readonly action: ActionPathnameNormalizer | undefined readonly postponed: PostponedPathnameNormalizer | undefined readonly rsc: RSCPathnameNormalizer | undefined readonly prefetchRSC: PrefetchRSCPathnameNormalizer | undefined @@ -467,6 +469,10 @@ export default abstract class Server { data: this.enabledDirectories.pages ? new NextDataPathnameNormalizer(this.buildId) : undefined, + action: + this.enabledDirectories.app && this.minimalMode + ? new ActionPathnameNormalizer() + : undefined, } this.nextFontManifest = this.getNextFontManifest() @@ -957,7 +963,7 @@ export default abstract class Server { 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)) { @@ -1410,6 +1416,10 @@ export default abstract class Server { normalizers.push(this.normalizers.rsc) } + if (this.normalizers.action) { + normalizers.push(this.normalizers.action) + } + for (const normalizer of normalizers) { if (!normalizer.match(pathname)) continue diff --git a/packages/next/src/server/future/normalizers/request/action.ts b/packages/next/src/server/future/normalizers/request/action.ts new file mode 100644 index 0000000000000..07e14dedcd658 --- /dev/null +++ b/packages/next/src/server/future/normalizers/request/action.ts @@ -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) + } +}