Skip to content

Commit 5df4372

Browse files
committed
move adapterMetadata to common func
1 parent 50297f5 commit 5df4372

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

packages/@apphosting/adapter-nextjs/src/bin/build.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
populateOutputBundleOptions,
55
generateBuildOutput,
66
validateOutputDirectory,
7+
getAdapterMetadata,
78
} from "../utils.js";
89
import { join } from "path";
910
import { getBuildOptions, runBuild } from "@apphosting/common";
@@ -21,6 +22,8 @@ if (!process.env.FRAMEWORK_VERSION) {
2122
}
2223
await runBuild();
2324

25+
const adapterMetadata = getAdapterMetadata();
26+
2427
const { distDir } = await loadConfig(root, opts.projectDirectory);
2528
const nextBuildDirectory = join(opts.projectDirectory, distDir);
2629
const outputBundleOptions = populateOutputBundleOptions(
@@ -29,13 +32,14 @@ const outputBundleOptions = populateOutputBundleOptions(
2932
nextBuildDirectory,
3033
);
3134

32-
await addRouteOverrides(outputBundleOptions.outputDirectoryAppPath, distDir);
35+
await addRouteOverrides(outputBundleOptions.outputDirectoryAppPath, distDir, adapterMetadata);
3336

3437
await generateBuildOutput(
3538
root,
3639
opts.projectDirectory,
3740
outputBundleOptions,
3841
nextBuildDirectory,
3942
process.env.FRAMEWORK_VERSION,
43+
adapterMetadata,
4044
);
4145
await validateOutputDirectory(outputBundleOptions, nextBuildDirectory);

packages/@apphosting/adapter-nextjs/src/overrides.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1+
import { AdapterMetadata } from "@apphosting/common/dist/index.js";
12
import { loadRouteManifest, writeRouteManifest } from "./utils.js";
23

3-
interface Header {
4-
key: string;
5-
value: string;
6-
}
7-
8-
const GLOBAL_APPHOSTING_HEADERS: Header[] = [
9-
{
10-
key: "x-fah-adapter",
11-
value: "nextjs",
12-
},
13-
];
14-
15-
export async function addRouteOverrides(appPath: string, distDir: string) {
4+
export async function addRouteOverrides(
5+
appPath: string,
6+
distDir: string,
7+
adapterMetadata: AdapterMetadata,
8+
) {
169
const routeManifest = await loadRouteManifest(appPath, distDir);
1710
routeManifest.headers.push({
1811
source: "/:path*",
19-
headers: GLOBAL_APPHOSTING_HEADERS,
12+
headers: [
13+
{
14+
key: "x-fah-adapter",
15+
value: `nextjs-${adapterMetadata.adapterVersion}`,
16+
},
17+
],
2018
regex: "^(?:/((?:[^/]+?)(?:/(?:[^/]+?))*))?(?:/)?$",
2119
});
2220

packages/@apphosting/adapter-nextjs/src/utils.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { stringify as yamlStringify } from "yaml";
77
import { PHASE_PRODUCTION_BUILD, ROUTES_MANIFEST } from "./constants.js";
88
import { OutputBundleOptions, RoutesManifest } from "./interfaces.js";
99
import { NextConfigComplete } from "next/dist/server/config-shared.js";
10-
import { OutputBundleConfig, Metadata } from "@apphosting/common";
10+
import { OutputBundleConfig, Metadata, AdapterMetadata } from "@apphosting/common";
1111

1212
// fs-extra is CJS, readJson can't be imported using shorthand
1313
export const { move, exists, writeFile, readJson, readdir, readFileSync, existsSync, mkdir } =
@@ -45,10 +45,10 @@ export async function loadRouteManifest(
4545
export async function writeRouteManifest(
4646
standalonePath: string,
4747
distDir: string,
48-
manifest: RoutesManifest,
48+
customManifest: RoutesManifest,
4949
): Promise<void> {
5050
const manifestPath = join(standalonePath, distDir, ROUTES_MANIFEST);
51-
await writeFile(manifestPath, JSON.stringify(manifest));
51+
await writeFile(manifestPath, JSON.stringify(customManifest));
5252
}
5353

5454
export const isMain = (meta: ImportMeta): boolean => {
@@ -98,12 +98,13 @@ export async function generateBuildOutput(
9898
opts: OutputBundleOptions,
9999
nextBuildDirectory: string,
100100
nextVersion: string,
101+
adapterMetadata: AdapterMetadata,
101102
): Promise<void> {
102103
const staticDirectory = join(nextBuildDirectory, "static");
103104
await Promise.all([
104105
move(staticDirectory, opts.outputStaticDirectoryPath, { overwrite: true }),
105106
moveResources(appDir, opts.outputDirectoryAppPath, opts.bundleYamlPath),
106-
generateBundleYaml(opts, rootDir, nextVersion),
107+
generateBundleYaml(opts, rootDir, nextVersion, adapterMetadata),
107108
]);
108109
return;
109110
}
@@ -128,21 +129,17 @@ async function moveResources(
128129
return;
129130
}
130131

131-
/**
132-
* Create metadata needed for outputting adapter and framework metrics in bundle.yaml.
133-
*/
134-
export function createMetadata(nextVersion: string): Metadata {
132+
export function getAdapterMetadata(): AdapterMetadata {
135133
const directoryName = dirname(fileURLToPath(import.meta.url));
136134
const packageJsonPath = `${directoryName}/../package.json`;
137135
if (!existsSync(packageJsonPath)) {
138136
throw new Error(`Next.js adapter package.json file does not exist at ${packageJsonPath}`);
139137
}
140138
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
139+
141140
return {
142141
adapterPackageName: packageJson.name,
143142
adapterVersion: packageJson.version,
144-
framework: "nextjs",
145-
frameworkVersion: nextVersion,
146143
};
147144
}
148145

@@ -151,14 +148,19 @@ async function generateBundleYaml(
151148
opts: OutputBundleOptions,
152149
cwd: string,
153150
nextVersion: string,
151+
adapterMetadata: AdapterMetadata,
154152
): Promise<void> {
155153
await mkdir(opts.outputDirectoryBasePath);
156154
const outputBundle: OutputBundleConfig = {
157155
version: "v1",
158156
runConfig: {
159157
runCommand: `node ${normalize(relative(cwd, opts.serverFilePath))}`,
160158
},
161-
metadata: createMetadata(nextVersion),
159+
metadata: {
160+
...adapterMetadata,
161+
framework: "nextjs",
162+
frameworkVersion: nextVersion,
163+
},
162164
};
163165
await writeFile(opts.bundleYamlPath, yamlStringify(outputBundle));
164166
return;

0 commit comments

Comments
 (0)