Unverified Commit 03f34625 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Polyfill `FormData` for Node.js < 18 (#48783)

We currently use `URLSearchParams` to represent `FormData` but it's not really the same thing. And in Node.js 16 there's no `FormData` available so we can polyfill it via Undici.
parent 2196cbe4
...@@ -9,6 +9,15 @@ import { ...@@ -9,6 +9,15 @@ import {
import RenderResult from '../render-result' import RenderResult from '../render-result'
import { ActionRenderResult } from './action-render-result' import { ActionRenderResult } from './action-render-result'
function formDataFromSearchQueryString(query: string) {
const searchParams = new URLSearchParams(query)
const formData = new FormData()
for (const [key, value] of searchParams) {
formData.append(key, value)
}
return formData
}
export async function handleAction({ export async function handleAction({
req, req,
res, res,
...@@ -77,7 +86,7 @@ export async function handleAction({ ...@@ -77,7 +86,7 @@ export async function handleAction({
} }
if (isFormAction) { if (isFormAction) {
const formData = new URLSearchParams(actionData) const formData = formDataFromSearchQueryString(actionData)
actionId = formData.get('$$id') as string actionId = formData.get('$$id') as string
if (!actionId) { if (!actionId) {
...@@ -112,7 +121,7 @@ export async function handleAction({ ...@@ -112,7 +121,7 @@ export async function handleAction({
if (!actionId) { if (!actionId) {
throw new Error('Invariant: missing action ID.') throw new Error('Invariant: missing action ID.')
} }
const formData = new URLSearchParams(actionData) const formData = formDataFromSearchQueryString(actionData)
formData.delete('$$id') formData.delete('$$id')
bound = [formData] bound = [formData]
} else { } else {
......
import './require-hook' import './require-hook'
import './node-polyfill-fetch' import './node-polyfill-fetch'
import './node-polyfill-form'
import './node-polyfill-web-streams' import './node-polyfill-web-streams'
import type { TLSSocket } from 'tls' import type { TLSSocket } from 'tls'
......
/**
* Polyfills `FormData` in the Node.js runtime.
*/
if (!(global as any).FormData) {
const undici = require('next/dist/compiled/undici')
;(global as any).FormData = undici.FormData
}
...@@ -52,7 +52,7 @@ createNextDescribe( ...@@ -52,7 +52,7 @@ createNextDescribe(
await check(() => { await check(() => {
return browser.eval('window.location.pathname + window.location.search') return browser.eval('window.location.pathname + window.location.search')
}, '/header?name=test') }, '/header?name=test&constructor=FormData')
}) })
it('should support notFound', async () => { it('should support notFound', async () => {
......
...@@ -2,7 +2,12 @@ import { redirect, notFound } from 'next/navigation' ...@@ -2,7 +2,12 @@ import { redirect, notFound } from 'next/navigation'
async function action(formData) { async function action(formData) {
'use server' 'use server'
redirect('/header?name=' + formData.get('name')) redirect(
'/header?name=' +
formData.get('name') +
'&constructor=' +
formData.constructor.name
)
} }
async function nowhere() { async function nowhere() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment