Skip to content

Instantly share code, notes, and snippets.

@mizdra
Last active March 3, 2026 16:31
Show Gist options
  • Select an option

  • Save mizdra/b0537e418cbb1072e137fa61c33994b8 to your computer and use it in GitHub Desktop.

Select an option

Save mizdra/b0537e418cbb1072e137fa61c33994b8 to your computer and use it in GitHub Desktop.
どうしても Next.js で Server Function から 400 を返したい者の悪あがき
"use server";
import { mock } from 'node:test';
const httpAccessFallbackModule = await import("next/dist/client/components/http-access-fallback/http-access-fallback.js");
// ref: https://github.com/vercel/next.js/blob/6f763ecfd48acc3593f8a10328aef9358fdb7c75/packages/next/src/client/components/http-access-fallback/http-access-fallback.ts
mock.module('next/dist/client/components/http-access-fallback/http-access-fallback.js', {
namedExports: {
...httpAccessFallbackModule,
isHTTPAccessFallbackError: (error: unknown): error is any => {
if (
typeof error !== 'object' ||
error === null ||
!('digest' in error) ||
typeof error.digest !== 'string'
) {
return false
}
const [prefix] = error.digest.split(';');
return (
prefix === httpAccessFallbackModule.HTTP_ERROR_FALLBACK_ERROR_CODE
);
},
},
});
function badRequest(): never {
const digest = `${httpAccessFallbackModule.HTTP_ERROR_FALLBACK_ERROR_CODE};400`;
const error = new Error(digest);
// @ts-expect-error
error.digest = digest;
throw error;
}
export async function greet(name: string): Promise<string> {
badRequest();
return `Hello, ${name}!`;
}
  • Turbopack の壁が立ちはだかり、無事敗北
    • image
  • そもそも node:test をアプリケーションコードで使ってはならない
  • モンキーパッチも極力避けるべき
  • 大人しく vercel/next.js#49302 の解決を待ちましょう
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment