Skip to content

Commit 48753bc

Browse files
authored
fix: incorrect path of middleware nft in setups using base directory (#3262)
1 parent 6451ec3 commit 48753bc

File tree

10 files changed

+100
-13
lines changed

10 files changed

+100
-13
lines changed

src/build/functions/edge.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ const copyHandlerDependenciesForNodeMiddleware = async (ctx: PluginContext) => {
210210

211211
const entry = 'server/middleware.js'
212212
const nft = `${entry}.nft.json`
213-
const nftFilesPath = join(process.cwd(), ctx.distDir, nft)
213+
const nftFilesPath = join(ctx.publishDir, nft)
214214
const nftManifest = JSON.parse(await readFile(nftFilesPath, 'utf8'))
215215

216216
const files: string[] = nftManifest.files.map((file: string) => join('server', file))

tests/smoke/deploy.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ if (nextVersionSatisfies('>=16.0.0')) {
4949
const body = await response.json()
5050
expect(body).toEqual({ proxy: true })
5151
})
52+
53+
test('pnpm monorepo with proxy / node middleware with setup using base directory instead of package path', async () => {
54+
// proxy ~= node middleware
55+
const fixture = await selfCleaningFixtureFactories.pnpmMonorepoBaseProxy()
56+
57+
const response = await fetch(fixture.url)
58+
expect(response.status).toBe(200)
59+
60+
const body = await response.json()
61+
expect(body).toEqual({ proxy: true })
62+
})
5263
}
5364

5465
test('yarn@3 monorepo with pnpm linker', async () => {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/** @type {import('next').NextConfig} */
2+
const nextConfig = {
3+
eslint: {
4+
ignoreDuringBuilds: true,
5+
},
6+
}
7+
8+
module.exports = nextConfig
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "pnpm-monorepo-base-proxy-app",
3+
"version": "0.1.0",
4+
"private": true,
5+
"scripts": {
6+
"build": "next build"
7+
},
8+
"dependencies": {
9+
"next": "latest",
10+
"react": "^18.2.0",
11+
"react-dom": "^18.2.0"
12+
}
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export default function Home({ ssr }) {
2+
return (
3+
<main>
4+
<div data-testid="smoke">SSR: {ssr ? 'yes' : 'no'}</div>
5+
</main>
6+
)
7+
}
8+
9+
export const getServerSideProps = async () => {
10+
return {
11+
props: {
12+
ssr: true,
13+
},
14+
}
15+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { NextRequest } from 'next/server'
2+
import { NextResponse } from 'next/server'
3+
4+
export async function proxy(request: NextRequest) {
5+
return NextResponse.json({ proxy: true })
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[build]
2+
base = "app"
3+
command = "pnpm run build"
4+
publish = ".next"
5+
6+
[[plugins]]
7+
package = "@netlify/plugin-nextjs"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "monorepo",
3+
"private": true
4+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
packages:
2+
- 'app'

tests/utils/create-e2e-fixture.ts

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { exec } from 'node:child_process'
55
import { existsSync } from 'node:fs'
66
import { appendFile, copyFile, mkdir, mkdtemp, readFile, rm } from 'node:fs/promises'
77
import { tmpdir } from 'node:os'
8-
import { dirname, join } from 'node:path'
8+
import { dirname, join, relative } from 'node:path'
99
import process from 'node:process'
1010
import { fileURLToPath } from 'node:url'
1111
import { cpus } from 'os'
@@ -228,6 +228,14 @@ async function installRuntime(
228228
await rm(join(isolatedFixtureRoot, 'package-lock.json'), { force: true })
229229
}
230230

231+
let relativePathToPackage = relative(
232+
join(isolatedFixtureRoot, siteRelDir),
233+
join(isolatedFixtureRoot, packageName),
234+
)
235+
if (!relativePathToPackage.startsWith('.')) {
236+
relativePathToPackage = `./${relativePathToPackage}`
237+
}
238+
231239
switch (packageManger) {
232240
case 'npm':
233241
command = `npm install --ignore-scripts --no-audit --legacy-peer-deps ${packageName} ${
@@ -248,7 +256,7 @@ async function installRuntime(
248256
env['YARN_ENABLE_SCRIPTS'] = 'false'
249257
break
250258
case 'pnpm':
251-
command = `pnpm add file:${join(isolatedFixtureRoot, packageName)} ${
259+
command = `pnpm add file:${relativePathToPackage} ${
252260
workspaceRelPath ? `--filter ./${workspaceRelPath}` : ''
253261
} --ignore-scripts`
254262
break
@@ -349,24 +357,27 @@ export async function deploySiteWithBuildbot(
349357
newZip.addLocalFolder(isolatedFixtureRoot, '', (entry) => {
350358
if (
351359
// don't include node_modules / .git / publish dir in zip
352-
entry.startsWith('node_modules') ||
353-
entry.startsWith('.git') ||
360+
entry.includes('node_modules') ||
361+
entry.includes('.git') ||
354362
entry.startsWith(publishDirectory)
355363
) {
356364
return false
357365
}
358366
return true
359367
})
360368

361-
const result = await fetch(`https://api.netlify.com/api/v1/sites/${siteId}/builds`, {
362-
method: 'POST',
363-
headers: {
364-
'Content-Type': 'application/zip',
365-
Authorization: `Bearer ${process.env.NETLIFY_AUTH_TOKEN}`,
369+
const result = await fetch(
370+
`https://api.netlify.com/api/v1/sites/${siteId}/builds?clear_cache=true`,
371+
{
372+
method: 'POST',
373+
headers: {
374+
'Content-Type': 'application/zip',
375+
Authorization: `Bearer ${process.env.NETLIFY_AUTH_TOKEN}`,
376+
},
377+
// @ts-expect-error sigh, it works
378+
body: newZip.toBuffer(),
366379
},
367-
// @ts-expect-error sigh, it works
368-
body: newZip.toBuffer(),
369-
})
380+
)
370381
const { deploy_id } = await result.json()
371382

372383
let didRunOnBuildStartCallback = false
@@ -636,6 +647,16 @@ export const fixtureFactories = {
636647
publishDirectory: 'apps/site/.next',
637648
smoke: true,
638649
}),
650+
pnpmMonorepoBaseProxy: () =>
651+
createE2EFixture('pnpm-monorepo-base-proxy', {
652+
buildCommand: 'pnpm run build',
653+
generateNetlifyToml: false,
654+
packageManger: 'pnpm',
655+
publishDirectory: '.next',
656+
runtimeInstallationPath: 'app',
657+
smoke: true,
658+
useBuildbot: true,
659+
}),
639660
dynamicCms: () => createE2EFixture('dynamic-cms'),
640661
after: () => createE2EFixture('after'),
641662
}

0 commit comments

Comments
 (0)