Skip to content

Commit 896b456

Browse files
authored
[eas-cli] Pass _expo/.routes.json for SSG exports as routes.json to Hosting deployments (#3211)
* Derive `routes.json` payload from static export `_expo/.routes.json` * Add CHANGELOG entry
1 parent f2c0202 commit 896b456

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ This is the log of notable changes to EAS CLI and related packages.
88

99
### 🎉 New features
1010

11+
- Support `_expo/.routes.json` metadata file exported by `@expo/cli` for headers configuration on static deployments ([#3211](https://github.com/expo/eas-cli/pull/3211) by [@kitten](https://github.com/kitten))
12+
1113
### 🐛 Bug fixes
1214

1315
### 🧹 Chores

packages/eas-cli/src/commands/deploy/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ export default class WorkerDeploy extends EasCommand {
138138
logExportedProjectInfo(projectDist);
139139

140140
async function* emitWorkerTarballAsync(params: {
141+
routesConfig: WorkerAssets.RoutesConfigEntry | null;
141142
assetMap: WorkerAssets.AssetMap;
142143
manifest: WorkerAssets.Manifest;
143144
}): AsyncGenerator<WorkerAssets.FileEntry> {
@@ -148,6 +149,8 @@ export default class WorkerDeploy extends EasCommand {
148149
for await (const workerFile of workerFiles) {
149150
yield [`server/${workerFile.normalizedPath}`, workerFile.data];
150151
}
152+
} else if (projectDist.type === 'static' && params.routesConfig) {
153+
yield ['routes.json', JSON.stringify(params.routesConfig)];
151154
}
152155
}
153156

@@ -284,12 +287,13 @@ export default class WorkerDeploy extends EasCommand {
284287
manifestResult.conflictingVariableNames.join(' ')
285288
);
286289
}
287-
assetFiles = await WorkerAssets.collectAssetsAsync(
288-
projectDist.type === 'server' ? projectDist.clientPath : projectDist.path,
289-
{ maxFileSize: MAX_UPLOAD_SIZE }
290-
);
290+
const assetPath = projectDist.type === 'server' ? projectDist.clientPath : projectDist.path;
291+
assetFiles = await WorkerAssets.collectAssetsAsync(assetPath, {
292+
maxFileSize: MAX_UPLOAD_SIZE,
293+
});
291294
tarPath = await WorkerAssets.packFilesIterableAsync(
292295
emitWorkerTarballAsync({
296+
routesConfig: await WorkerAssets.getRoutesConfigAsync(assetPath),
293297
assetMap: WorkerAssets.assetsToAssetsMap(assetFiles),
294298
manifest: manifestResult.manifest,
295299
})

packages/eas-cli/src/worker/assets.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { EnvironmentVariableEnvironment } from '../build/utils/environment';
1212
import { ExpoGraphqlClient } from '../commandUtils/context/contextUtils/createGraphqlClient';
1313
import { EnvironmentVariablesQuery } from '../graphql/queries/EnvironmentVariablesQuery';
1414

15+
const EXPO_ROUTES_PATHS = new Set(['_expo/routes.json', '_expo/.routes.json', '.expo-routes.json']);
16+
1517
/** Returns whether a file or folder is ignored */
1618
function isIgnoredName(name: string): boolean {
1719
switch (name) {
@@ -114,6 +116,8 @@ export async function collectAssetsAsync(
114116
throw new Error(
115117
`Upload of "${file.normalizedPath}" aborted: File size is greater than the upload limit (>500MB)`
116118
);
119+
} else if (EXPO_ROUTES_PATHS.has(file.normalizedPath)) {
120+
continue;
117121
}
118122
const sha512$ = computeSha512HashAsync(file.path);
119123
const contentType$ = determineMimeTypeAsync(file.path);
@@ -129,6 +133,31 @@ export async function collectAssetsAsync(
129133
return assets;
130134
}
131135

136+
export interface RoutesConfigEntry {
137+
headers?: Record<string, unknown>;
138+
redirects?: Record<string, unknown>[];
139+
}
140+
141+
export async function getRoutesConfigAsync(
142+
assetPath: string | undefined
143+
): Promise<RoutesConfigEntry | null> {
144+
if (assetPath) {
145+
for (const candidatePath of EXPO_ROUTES_PATHS) {
146+
const targetPath = path.resolve(assetPath, candidatePath);
147+
let json: unknown;
148+
try {
149+
json = JSON.parse(await fs.promises.readFile(targetPath, 'utf8'));
150+
} catch {
151+
continue;
152+
}
153+
if (typeof json === 'object' && json) {
154+
return json as RoutesConfigEntry;
155+
}
156+
}
157+
}
158+
return null;
159+
}
160+
132161
/** Mapping of normalized file paths to a SHA512 hash */
133162
export type AssetMap = Record<
134163
string,

0 commit comments

Comments
 (0)