Skip to content

Commit

Permalink
We don't need no public dir (#5142)
Browse files Browse the repository at this point in the history
* We don't need no public dir

* Add docs

* Add changelog
  • Loading branch information
inlined committed Oct 18, 2022
1 parent 8e39796 commit 73884ec
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
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

0 comments on commit 73884ec

Please sign in to comment.