Skip to content

Commit 1faf191

Browse files
fix: publish from subdirectory (#1756)
* fix: publish from subdirectory * chore: fix nx demo * chore: remove log Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 89fb572 commit 1faf191

File tree

8 files changed

+4456
-1800
lines changed

8 files changed

+4456
-1800
lines changed

demos/nx-next-monorepo-demo/package-lock.json

+4,366-1,602
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demos/nx-next-monorepo-demo/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"@netlify/plugin-nextjs": "file:plugin-wrapper",
1313
"@nrwl/next": "15.0.11",
1414
"core-js": "^3.6.5",
15-
"next": "12.3.1",
15+
"next": "^13.0.2",
1616
"react": "18.2.0",
1717
"react-dom": "18.2.0",
1818
"regenerator-runtime": "0.13.10",

package-lock.json

+23-34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/runtime/src/helpers/files.ts

+19-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint-disable max-lines */
22
import { cpus } from 'os'
33

4-
import type { NetlifyConfig } from '@netlify/build'
54
import { yellowBright } from 'chalk'
65
import { existsSync, readJson, move, copy, writeJson, readFile, writeFile, ensureDir, readFileSync } from 'fs-extra'
76
import globby from 'globby'
@@ -60,11 +59,11 @@ export const matchesRewrite = (file: string, rewrites: Rewrites): boolean => {
6059
return matchesRedirect(file, rewrites.beforeFiles)
6160
}
6261

63-
export const getMiddleware = async (publish: string): Promise<Array<string>> => {
62+
export const getMiddleware = async (distDir: string): Promise<Array<string>> => {
6463
if (process.env.NEXT_DISABLE_NETLIFY_EDGE !== 'true' && process.env.NEXT_DISABLE_NETLIFY_EDGE !== '1') {
6564
return []
6665
}
67-
const manifestPath = join(publish, 'server', 'middleware-manifest.json')
66+
const manifestPath = join(distDir, 'server', 'middleware-manifest.json')
6867
if (existsSync(manifestPath)) {
6968
const manifest = await readJson(manifestPath, { throws: false })
7069
return manifest?.sortedMiddleware ?? []
@@ -74,32 +73,28 @@ export const getMiddleware = async (publish: string): Promise<Array<string>> =>
7473

7574
// eslint-disable-next-line max-lines-per-function
7675
export const moveStaticPages = async ({
77-
netlifyConfig,
78-
target,
76+
distDir,
7977
i18n,
8078
basePath,
79+
publishDir,
8180
}: {
82-
netlifyConfig: NetlifyConfig
83-
target: 'server' | 'serverless' | 'experimental-serverless-trace'
81+
distDir: string
8482
i18n: NextConfig['i18n']
8583
basePath?: string
84+
publishDir
8685
}): Promise<void> => {
8786
console.log('Moving static page files to serve from CDN...')
88-
const outputDir = join(netlifyConfig.build.publish, target === 'server' ? 'server' : 'serverless')
87+
const outputDir = join(distDir, 'server')
8988
const root = join(outputDir, 'pages')
90-
const buildId = readFileSync(join(netlifyConfig.build.publish, 'BUILD_ID'), 'utf8').trim()
89+
const buildId = readFileSync(join(distDir, 'BUILD_ID'), 'utf8').trim()
9190
const dataDir = join('_next', 'data', buildId)
92-
await ensureDir(join(netlifyConfig.build.publish, dataDir))
91+
await ensureDir(join(publishDir, dataDir))
9392
// Load the middleware manifest so we can check if a file matches it before moving
94-
const middlewarePaths = await getMiddleware(netlifyConfig.build.publish)
93+
const middlewarePaths = await getMiddleware(distDir)
9594
const middleware = middlewarePaths.map((path) => path.slice(1))
9695

97-
const prerenderManifest: PrerenderManifest = await readJson(
98-
join(netlifyConfig.build.publish, 'prerender-manifest.json'),
99-
)
100-
const { redirects, rewrites }: RoutesManifest = await readJson(
101-
join(netlifyConfig.build.publish, 'routes-manifest.json'),
102-
)
96+
const prerenderManifest: PrerenderManifest = await readJson(join(distDir, 'prerender-manifest.json'))
97+
const { redirects, rewrites }: RoutesManifest = await readJson(join(distDir, 'routes-manifest.json'))
10398

10499
const isrFiles = new Set<string>()
105100

@@ -128,7 +123,7 @@ export const moveStaticPages = async ({
128123
files.push(file)
129124
filesManifest[file] = targetPath
130125

131-
const dest = join(netlifyConfig.build.publish, targetPath)
126+
const dest = join(publishDir, targetPath)
132127

133128
try {
134129
await move(source, dest)
@@ -242,10 +237,10 @@ export const moveStaticPages = async ({
242237
}
243238

244239
// Write the manifest for use in the serverless functions
245-
await writeJson(join(netlifyConfig.build.publish, 'static-manifest.json'), Object.entries(filesManifest))
240+
await writeJson(join(distDir, 'static-manifest.json'), Object.entries(filesManifest))
246241

247242
if (i18n?.defaultLocale) {
248-
const rootPath = basePath ? join(netlifyConfig.build.publish, basePath) : netlifyConfig.build.publish
243+
const rootPath = basePath ? join(publishDir, basePath) : publishDir
249244
// Copy the default locale into the root
250245
const defaultLocaleDir = join(rootPath, i18n.defaultLocale)
251246
if (existsSync(defaultLocaleDir)) {
@@ -427,12 +422,13 @@ export const unpatchNextFiles = async (root: string): Promise<void> => {
427422
export const movePublicFiles = async ({
428423
appDir,
429424
outdir,
430-
publish,
425+
publishDir,
431426
}: {
432427
appDir: string
433428
outdir?: string
434-
publish: string
429+
publishDir: string
435430
}): Promise<void> => {
431+
await ensureDir(publishDir)
436432
// `outdir` is a config property added when using Next.js with Nx. It's typically
437433
// a relative path outside of the appDir, e.g. '../../dist/apps/<app-name>', and
438434
// the parent directory of the .next directory.
@@ -441,7 +437,7 @@ export const movePublicFiles = async ({
441437
// directory from the original app directory.
442438
const publicDir = outdir ? join(appDir, outdir, 'public') : join(appDir, 'public')
443439
if (existsSync(publicDir)) {
444-
await copy(publicDir, `${publish}/`)
440+
await copy(publicDir, `${publishDir}/`)
445441
}
446442
}
447443
/* eslint-enable max-lines */

packages/runtime/src/helpers/redirects.ts

+1-26
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { PrerenderManifest, SsgRoute } from 'next/dist/build'
77
import { outdent } from 'outdent'
88
import { join } from 'pathe'
99

10-
import { HANDLER_FUNCTION_PATH, HIDDEN_PATHS, ODB_FUNCTION_PATH } from '../constants'
10+
import { HANDLER_FUNCTION_PATH, ODB_FUNCTION_PATH } from '../constants'
1111

1212
import { getMiddleware } from './files'
1313
import { ApiRouteConfig } from './functions'
@@ -25,14 +25,6 @@ import {
2525
const matchesMiddleware = (middleware: Array<string>, route: string): boolean =>
2626
middleware.some((middlewarePath) => route.startsWith(middlewarePath))
2727

28-
const generateHiddenPathRedirects = ({ basePath }: Pick<NextConfig, 'basePath'>): NetlifyConfig['redirects'] =>
29-
HIDDEN_PATHS.map((path) => ({
30-
from: `${basePath}${path}`,
31-
to: '/404.html',
32-
status: 404,
33-
force: true,
34-
}))
35-
3628
const generateLocaleRedirects = ({
3729
i18n,
3830
basePath,
@@ -66,21 +58,6 @@ const generateLocaleRedirects = ({
6658
return redirects
6759
}
6860

69-
export const generateStaticRedirects = ({
70-
netlifyConfig,
71-
nextConfig: { i18n, basePath },
72-
}: {
73-
netlifyConfig: NetlifyConfig
74-
nextConfig: Pick<NextConfig, 'i18n' | 'basePath'>
75-
}) => {
76-
// Static files are in `static`
77-
netlifyConfig.redirects.push({ from: `${basePath}/_next/static/*`, to: `/static/:splat`, status: 200 })
78-
79-
if (i18n) {
80-
netlifyConfig.redirects.push({ from: `${basePath}/:locale/_next/static/*`, to: `/static/:splat`, status: 200 })
81-
}
82-
}
83-
8461
/**
8562
* Routes that match middleware need to always use the SSR function
8663
* This generates a rewrite for every middleware in every locale, both with and without a splat
@@ -243,8 +220,6 @@ export const generateRedirects = async ({
243220
join(netlifyConfig.build.publish, 'routes-manifest.json'),
244221
)
245222

246-
netlifyConfig.redirects.push(...generateHiddenPathRedirects({ basePath }))
247-
248223
if (i18n && i18n.localeDetection !== false) {
249224
netlifyConfig.redirects.push(...generateLocaleRedirects({ i18n, basePath, trailingSlash }))
250225
}

0 commit comments

Comments
 (0)