Skip to content
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

We don't need no public dir #5142

Merged
merged 3 commits into from Oct 18, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
@@ -0,0 +1 @@
Fix a bug where next.js applications would fail to deploy if they did not have a public dir (#5142)
29 changes: 26 additions & 3 deletions src/frameworks/next/index.ts
Expand Up @@ -51,13 +51,19 @@ function getNextVersion(cwd: string) {
return findDependency("next", { cwd, depth: 0, omitDev: false })?.version;
}

/**
* Returns whether this codebase is a Next.js backend.
*/
export async function discover(dir: string) {
if (!(await pathExists(join(dir, "package.json")))) return;
if (!(await pathExists("next.config.js")) && !getNextVersion(dir)) return;
// TODO don't hardcode public dir
return { mayWantBackend: true, publicDirectory: join(dir, "public") };
}

/**
* Build a next.js application.
*/
export async function build(dir: string): Promise<BuildResult> {
const { default: nextBuild } = relativeRequire(dir, "next/dist/build");

Expand Down Expand Up @@ -133,6 +139,9 @@ export async function build(dir: string): Promise<BuildResult> {
return { wantsBackend, headers, redirects, rewrites };
}

/**
* Utility method used during project initialization.
*/
export async function init(setup: any) {
const language = await promptOnce({
type: "list",
Expand All @@ -148,6 +157,9 @@ export async function init(setup: any) {
);
}

/**
* Create a directory for SSG content.
*/
export async function ɵcodegenPublicDirectory(sourceDir: string, destDir: string) {
const { distDir } = await getConfig(sourceDir);
const exportDetailPath = join(sourceDir, distDir, "export-detail.json");
Expand All @@ -157,8 +169,11 @@ export async function ɵcodegenPublicDirectory(sourceDir: string, destDir: strin
if (exportDetailJson?.success) {
copy(exportDetailJson.outDirectory, destDir);
} else {
const publicPath = join(sourceDir, "public");
await mkdir(join(destDir, "_next", "static"), { recursive: true });
await copy(join(sourceDir, "public"), destDir);
if (await pathExists(publicPath)) {
await copy(publicPath, destDir);
}
await copy(join(sourceDir, distDir, "static"), join(destDir, "_next", "static"));

const serverPagesDir = join(sourceDir, distDir, "server", "pages");
Expand Down Expand Up @@ -202,6 +217,9 @@ export async function ɵcodegenPublicDirectory(sourceDir: string, destDir: strin
}
}

/**
* Create a directory for SSR content.
*/
export async function ɵcodegenFunctionsDirectory(sourceDir: string, destDir: string) {
const { distDir } = await getConfig(sourceDir);
const packageJsonBuffer = await readFile(join(sourceDir, "package.json"));
Expand All @@ -227,13 +245,18 @@ export async function ɵcodegenFunctionsDirectory(sourceDir: string, destDir: st
platform: "node",
});
}
await mkdir(join(destDir, "public"));
if (await pathExists(join(sourceDir, "public"))) {
await mkdir(join(destDir, "public"));
await copy(join(sourceDir, "public"), join(destDir, "public"));
}
await mkdirp(join(destDir, distDir));
await copy(join(sourceDir, "public"), join(destDir, "public"));
await copy(join(sourceDir, distDir), join(destDir, distDir));
return { packageJson, frameworksEntry: "next.js" };
}

/**
* Create a dev server.
*/
export async function getDevModeHandle(dir: string) {
const { default: next } = relativeRequire(dir, "next");
const nextApp = next({
Expand Down