-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathverification.ts
197 lines (178 loc) · 7.09 KB
/
verification.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* eslint-disable max-lines */
import { existsSync, promises } from 'fs'
import path, { relative, join } from 'path'
import { NetlifyConfig, NetlifyPluginUtils } from '@netlify/build'
import { yellowBright, greenBright, blueBright, redBright, reset } from 'chalk'
import { async as StreamZip } from 'node-stream-zip'
import { outdent } from 'outdent'
import prettyBytes from 'pretty-bytes'
import { satisfies } from 'semver'
import { LAMBDA_MAX_SIZE } from '../constants'
// This is when nft support was added
const REQUIRED_BUILD_VERSION = '>=18.16.0'
type FailBuild = NetlifyPluginUtils['build']['failBuild']
export const verifyNetlifyBuildVersion = ({
IS_LOCAL,
NETLIFY_BUILD_VERSION,
failBuild,
}: {
IS_LOCAL: boolean
NETLIFY_BUILD_VERSION: string
failBuild: FailBuild
}): void | never => {
// We check for build version because that's what's available to us, but prompt about the cli because that's what they can upgrade
if (IS_LOCAL && !satisfies(NETLIFY_BUILD_VERSION, REQUIRED_BUILD_VERSION, { includePrerelease: true })) {
return failBuild(outdent`
This version of the Essential Next.js plugin requires [email protected] or higher. Please upgrade and try again.
You can do this by running: "npm install -g netlify-cli@latest" or "yarn global add netlify-cli@latest"
`)
}
}
export const checkForOldFunctions = async ({ functions }: Pick<NetlifyPluginUtils, 'functions'>): Promise<void> => {
const allOldFunctions = await functions.list()
const oldFunctions = allOldFunctions.filter(({ name }) => name.startsWith('next_'))
if (oldFunctions.length !== 0) {
console.log(
yellowBright(outdent`
We have found the following functions in your site that seem to be left over from the old Next.js plugin (v3). We have guessed this because the name starts with "next_".
${reset(oldFunctions.map(({ name }) => `- ${name}`).join('\n'))}
If they were created by the old plugin, these functions are likely to cause errors so should be removed. You can do this by deleting the following directories:
${reset(
oldFunctions.map(({ mainFile }) => `- ${path.relative(process.cwd(), path.dirname(mainFile))}`).join('\n'),
)}
`),
)
}
}
export const checkNextSiteHasBuilt = ({
publish,
failBuild,
}: {
publish: string
failBuild: FailBuild
}): void | never => {
if (!existsSync(path.join(publish, 'BUILD_ID'))) {
const outWarning =
path.basename(publish) === 'out'
? `Your publish directory is set to "out", but in most cases it should be ".next".`
: `In most cases it should be set to ".next", unless you have chosen a custom "distDir" in your Next config.`
return failBuild(outdent`
The directory "${publish}" does not contain a Next.js production build. Perhaps the build command was not run, or you specified the wrong publish directory.
${outWarning}
If you are using "next export" then you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true".
`)
}
if (existsSync(path.join(publish, 'export-detail.json'))) {
failBuild(outdent`
Detected that "next export" was run, but site is incorrectly publishing the ".next" directory.
The publish directory should be set to "out", and you should set the environment variable NETLIFY_NEXT_PLUGIN_SKIP to "true".
`)
}
}
export const checkForRootPublish = ({
publish,
failBuild,
}: {
publish: string
failBuild: FailBuild
}): void | never => {
if (path.resolve(publish) === path.resolve('.')) {
failBuild(outdent`
Your publish directory is pointing to the base directory of your site. This is not supported for Next.js sites, and is probably a mistake.
In most cases it should be set to ".next", unless you have chosen a custom "distDir" in your Next config, or the Next site is in a subdirectory.
`)
}
}
export const checkZipSize = async (file: string, maxSize: number = LAMBDA_MAX_SIZE): Promise<void> => {
if (!existsSync(file)) {
console.warn(`Could not check zip size because ${file} does not exist`)
return
}
const fileSize = await promises.stat(file).then(({ size }) => size)
if (fileSize < maxSize) {
return
}
// We don't fail the build, because the actual hard max size is larger so it might still succeed
console.log(
redBright(outdent`
The function zip ${yellowBright(relative(process.cwd(), file))} size is ${prettyBytes(
fileSize,
)}, which is larger than the maximum supported size of ${prettyBytes(maxSize)}.
There are a few reasons this could happen. You may have accidentally bundled a large dependency, or you might have a
large number of pre-rendered pages included.
`),
)
const zip = new StreamZip({ file })
console.log(`Contains ${await zip.entriesCount} files`)
const sortedFiles = Object.values(await zip.entries()).sort((a, b) => b.size - a.size)
const largest = {}
for (let i = 0; i < 10 && i < sortedFiles.length; i++) {
largest[`${i + 1}`] = {
File: sortedFiles[i].name,
'Compressed Size': prettyBytes(sortedFiles[i].compressedSize),
'Uncompressed Size': prettyBytes(sortedFiles[i].size),
}
}
console.log(yellowBright`\n\nThese are the largest files in the zip:`)
console.table(largest)
console.log(
greenBright`\n\nFor more information on fixing this, see ${blueBright`https://ntl.fyi/large-next-functions`}`,
)
}
export const getProblematicUserRewrites = ({
redirects,
basePath,
}: {
redirects: NetlifyConfig['redirects']
basePath: string
}) => {
const userRewrites: NetlifyConfig['redirects'] = []
for (const redirect of redirects) {
// This is the first of the plugin-generated redirects so we can stop checking
if (redirect.from === `${basePath}/_next/static/*` && redirect.to === `/static/:splat` && redirect.status === 200) {
break
}
if (
// Redirects are fine
(redirect.status === 200 || redirect.status === 404) &&
// Rewriting to a function is also fine
!redirect.to.startsWith('/.netlify/') &&
// ...so is proxying
!redirect.to.startsWith('http')
) {
userRewrites.push(redirect)
}
}
return userRewrites
}
export const warnForProblematicUserRewrites = ({
redirects,
basePath,
}: {
redirects: NetlifyConfig['redirects']
basePath: string
}) => {
const userRewrites = getProblematicUserRewrites({ redirects, basePath })
if (userRewrites.length === 0) {
return
}
console.log(
yellowBright(outdent`
You have the following Netlify rewrite${
userRewrites.length === 1 ? '' : 's'
} that might cause conflicts with the Next.js plugin:
${reset(userRewrites.map(({ from, to, status }) => `- ${from} ${to} ${status}`).join('\n'))}
For more information, see https://ntl.fyi/next-rewrites
`),
)
}
export const warnForRootRedirects = ({ appDir }: { appDir: string }) => {
if (existsSync(join(appDir, '_redirects'))) {
console.log(
yellowBright(
`You have a "_redirects" file in your root directory, which is not deployed and will be ignored. If you want it to be used, please move it into "public".`,
),
)
}
}
/* eslint-enable max-lines */