Skip to content

fix: publish from subdirectory #1756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5,968 changes: 4,366 additions & 1,602 deletions demos/nx-next-monorepo-demo/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion demos/nx-next-monorepo-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"@netlify/plugin-nextjs": "file:plugin-wrapper",
"@nrwl/next": "15.0.11",
"core-js": "^3.6.5",
"next": "12.3.1",
"next": "^13.0.2",
"react": "18.2.0",
"react-dom": "18.2.0",
"regenerator-runtime": "0.13.10",
Expand Down
57 changes: 23 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 19 additions & 23 deletions packages/runtime/src/helpers/files.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable max-lines */
import { cpus } from 'os'

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

export const getMiddleware = async (publish: string): Promise<Array<string>> => {
export const getMiddleware = async (distDir: string): Promise<Array<string>> => {
if (process.env.NEXT_DISABLE_NETLIFY_EDGE !== 'true' && process.env.NEXT_DISABLE_NETLIFY_EDGE !== '1') {
return []
}
const manifestPath = join(publish, 'server', 'middleware-manifest.json')
const manifestPath = join(distDir, 'server', 'middleware-manifest.json')
if (existsSync(manifestPath)) {
const manifest = await readJson(manifestPath, { throws: false })
return manifest?.sortedMiddleware ?? []
Expand All @@ -74,32 +73,28 @@ export const getMiddleware = async (publish: string): Promise<Array<string>> =>

// eslint-disable-next-line max-lines-per-function
export const moveStaticPages = async ({
netlifyConfig,
target,
distDir,
i18n,
basePath,
publishDir,
}: {
netlifyConfig: NetlifyConfig
target: 'server' | 'serverless' | 'experimental-serverless-trace'
distDir: string
i18n: NextConfig['i18n']
basePath?: string
publishDir
}): Promise<void> => {
console.log('Moving static page files to serve from CDN...')
const outputDir = join(netlifyConfig.build.publish, target === 'server' ? 'server' : 'serverless')
const outputDir = join(distDir, 'server')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have we removed serverless from here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We force builds to use server target anyway, and in Next 13 serverless breaks the build.

const root = join(outputDir, 'pages')
const buildId = readFileSync(join(netlifyConfig.build.publish, 'BUILD_ID'), 'utf8').trim()
const buildId = readFileSync(join(distDir, 'BUILD_ID'), 'utf8').trim()
const dataDir = join('_next', 'data', buildId)
await ensureDir(join(netlifyConfig.build.publish, dataDir))
await ensureDir(join(publishDir, dataDir))
// Load the middleware manifest so we can check if a file matches it before moving
const middlewarePaths = await getMiddleware(netlifyConfig.build.publish)
const middlewarePaths = await getMiddleware(distDir)
const middleware = middlewarePaths.map((path) => path.slice(1))

const prerenderManifest: PrerenderManifest = await readJson(
join(netlifyConfig.build.publish, 'prerender-manifest.json'),
)
const { redirects, rewrites }: RoutesManifest = await readJson(
join(netlifyConfig.build.publish, 'routes-manifest.json'),
)
const prerenderManifest: PrerenderManifest = await readJson(join(distDir, 'prerender-manifest.json'))
const { redirects, rewrites }: RoutesManifest = await readJson(join(distDir, 'routes-manifest.json'))

const isrFiles = new Set<string>()

Expand Down Expand Up @@ -128,7 +123,7 @@ export const moveStaticPages = async ({
files.push(file)
filesManifest[file] = targetPath

const dest = join(netlifyConfig.build.publish, targetPath)
const dest = join(publishDir, targetPath)

try {
await move(source, dest)
Expand Down Expand Up @@ -242,10 +237,10 @@ export const moveStaticPages = async ({
}

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

if (i18n?.defaultLocale) {
const rootPath = basePath ? join(netlifyConfig.build.publish, basePath) : netlifyConfig.build.publish
const rootPath = basePath ? join(publishDir, basePath) : publishDir
// Copy the default locale into the root
const defaultLocaleDir = join(rootPath, i18n.defaultLocale)
if (existsSync(defaultLocaleDir)) {
Expand Down Expand Up @@ -427,12 +422,13 @@ export const unpatchNextFiles = async (root: string): Promise<void> => {
export const movePublicFiles = async ({
appDir,
outdir,
publish,
publishDir,
}: {
appDir: string
outdir?: string
publish: string
publishDir: string
}): Promise<void> => {
await ensureDir(publishDir)
// `outdir` is a config property added when using Next.js with Nx. It's typically
// a relative path outside of the appDir, e.g. '../../dist/apps/<app-name>', and
// the parent directory of the .next directory.
Expand All @@ -441,7 +437,7 @@ export const movePublicFiles = async ({
// directory from the original app directory.
const publicDir = outdir ? join(appDir, outdir, 'public') : join(appDir, 'public')
if (existsSync(publicDir)) {
await copy(publicDir, `${publish}/`)
await copy(publicDir, `${publishDir}/`)
}
}
/* eslint-enable max-lines */
27 changes: 1 addition & 26 deletions packages/runtime/src/helpers/redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { PrerenderManifest, SsgRoute } from 'next/dist/build'
import { outdent } from 'outdent'
import { join } from 'pathe'

import { HANDLER_FUNCTION_PATH, HIDDEN_PATHS, ODB_FUNCTION_PATH } from '../constants'
import { HANDLER_FUNCTION_PATH, ODB_FUNCTION_PATH } from '../constants'

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

const generateHiddenPathRedirects = ({ basePath }: Pick<NextConfig, 'basePath'>): NetlifyConfig['redirects'] =>
HIDDEN_PATHS.map((path) => ({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed anymore, as these aren't published

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are/were HIDDEN_PATHS?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They were the files in .next that we didn't want to be publicly-visible in the deploy, such as cache, server and various json files

from: `${basePath}${path}`,
to: '/404.html',
status: 404,
force: true,
}))

const generateLocaleRedirects = ({
i18n,
basePath,
Expand Down Expand Up @@ -66,21 +58,6 @@ const generateLocaleRedirects = ({
return redirects
}

export const generateStaticRedirects = ({
netlifyConfig,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to redirect, because the files are in the right place

nextConfig: { i18n, basePath },
}: {
netlifyConfig: NetlifyConfig
nextConfig: Pick<NextConfig, 'i18n' | 'basePath'>
}) => {
// Static files are in `static`
netlifyConfig.redirects.push({ from: `${basePath}/_next/static/*`, to: `/static/:splat`, status: 200 })

if (i18n) {
netlifyConfig.redirects.push({ from: `${basePath}/:locale/_next/static/*`, to: `/static/:splat`, status: 200 })
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems i18n still loads from /_next in the root, so it's not needed

}

/**
* Routes that match middleware need to always use the SSR function
* This generates a rewrite for every middleware in every locale, both with and without a splat
Expand Down Expand Up @@ -243,8 +220,6 @@ export const generateRedirects = async ({
join(netlifyConfig.build.publish, 'routes-manifest.json'),
)

netlifyConfig.redirects.push(...generateHiddenPathRedirects({ basePath }))

if (i18n && i18n.localeDetection !== false) {
netlifyConfig.redirects.push(...generateLocaleRedirects({ i18n, basePath, trailingSlash }))
}
Expand Down
Loading