1
1
import { existsSync } from 'node:fs'
2
+ import { readFile } from 'node:fs/promises'
2
3
import { join } from 'node:path'
3
4
5
+ import { glob } from 'fast-glob'
4
6
import { satisfies } from 'semver'
5
7
6
8
import { ApiRouteType , getAPIRoutesConfigs } from './advanced-api-routes.js'
7
9
import type { PluginContext } from './plugin-context.js'
8
10
9
11
const SUPPORTED_NEXT_VERSIONS = '>=13.5.0'
10
12
11
- const warnings = new Set < string > ( )
13
+ const verifications = new Set < string > ( )
12
14
13
15
export function verifyPublishDir ( ctx : PluginContext ) {
14
16
if ( ! existsSync ( ctx . publishDir ) ) {
@@ -71,7 +73,7 @@ export function verifyPublishDir(ctx: PluginContext) {
71
73
}
72
74
}
73
75
74
- export async function verifyNoAdvancedAPIRoutes ( ctx : PluginContext ) {
76
+ export async function verifyAdvancedAPIRoutes ( ctx : PluginContext ) {
75
77
const apiRoutesConfigs = await getAPIRoutesConfigs ( ctx )
76
78
77
79
const unsupportedAPIRoutes = apiRoutesConfigs . filter ( ( apiRouteConfig ) => {
@@ -88,11 +90,33 @@ export async function verifyNoAdvancedAPIRoutes(ctx: PluginContext) {
88
90
}
89
91
}
90
92
91
- export function verifyNoNetlifyForms ( ctx : PluginContext , html : string ) {
92
- if ( ! warnings . has ( 'netlifyForms' ) && / < f o r m [ ^ > ] * ?\s ( n e t l i f y | d a t a - n e t l i f y ) [ = > \s ] / . test ( html ) ) {
93
+ const formDetectionRegex = / < f o r m [ ^ > ] * ?\s ( n e t l i f y | d a t a - n e t l i f y ) [ = > \s ] /
94
+
95
+ export async function verifyNetlifyFormsWorkaround ( ctx : PluginContext ) {
96
+ const srcDir = ctx . resolveFromSiteDir ( 'public' )
97
+ const paths = await glob ( join ( srcDir , '**/*.html' ) )
98
+ try {
99
+ const hasWorkaround = await paths . some ( async ( path ) : Promise < boolean > => {
100
+ const html = await readFile ( path , 'utf-8' )
101
+ return formDetectionRegex . test ( html )
102
+ } )
103
+ if ( hasWorkaround ) {
104
+ verifications . add ( 'netlifyFormsWorkaround' )
105
+ }
106
+ } catch ( error ) {
107
+ ctx . failBuild ( 'Failed verifying public files' , error )
108
+ }
109
+ }
110
+
111
+ export function verifyNetlifyForms ( ctx : PluginContext , html : string ) {
112
+ if (
113
+ ! verifications . has ( 'netlifyForms' ) &&
114
+ ! verifications . has ( 'netlifyFormsWorkaround' ) &&
115
+ formDetectionRegex . test ( html )
116
+ ) {
93
117
console . warn (
94
118
'@netlify/plugin-next@5 does not support Netlify Forms. Refer to https://ntl.fyi/next-runtime-forms-migration for migration example.' ,
95
119
)
96
- warnings . add ( 'netlifyForms' )
120
+ verifications . add ( 'netlifyForms' )
97
121
}
98
122
}
0 commit comments