Skip to content

Commit 33572d5

Browse files
authored
fix: Resolve SSR build failure related to #93 (#94)
* fix: Resolve SSR build failure related to [#93](#93). * test: increase test timeout for integration tests to 180 seconds
1 parent 36dcd2a commit 33572d5

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

.changeset/nine-doors-float.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"vite-plugin-react-router-amplify-hosting": patch
3+
---
4+
5+
fix: Resolve SSR build failure related to [fossamagna/react-router-amplify#93](https://github.com/fossamagna/react-router-amplify/issues/93).
6+
7+
- Revised the asset copy behavior during SSR build.
8+
- Fixed handling of the `unstable_viteEnvironmentApi` flag in `@react-router/dev`.

packages/vite-plugin-react-router-amplify-hosting/src/index.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { cp, mkdir, readFile, writeFile } from "node:fs/promises";
1+
import { copyFile, cp, mkdir, readFile, writeFile } from "node:fs/promises";
22
import path from "node:path";
33
import type {
44
BuildEnvironmentOptions,
@@ -112,26 +112,33 @@ export function amplifyHosting(): Plugin {
112112
if (!pluginConfig) {
113113
return;
114114
}
115-
if (!pluginConfig.isSsrBuild) {
115+
const isClientBuild = pluginConfig.future.unstable_viteEnvironmentApi
116+
? this.environment.name === "client"
117+
: !pluginConfig.isSsrBuild;
118+
const isServerBuild = pluginConfig.future.unstable_viteEnvironmentApi
119+
? this.environment.name === "ssr"
120+
: pluginConfig.isSsrBuild;
121+
122+
if (isClientBuild) {
116123
const staticDir = path.join(
117124
resolvedConfig.root,
118125
AMPLITY_HOSTING_STATIC_DIR,
119126
);
120127
await mkdir(staticDir, { recursive: true });
121128
const dir = options.dir ?? "";
122129
await cp(dir, staticDir, { recursive: true });
123-
}
124-
if (
125-
pluginConfig.isSsrBuild ||
126-
pluginConfig.future.unstable_viteEnvironmentApi
127-
) {
130+
} else if (isServerBuild) {
131+
// copy server.mjs to the compute default directory
128132
const computeDefaultDir = path.join(
129133
resolvedConfig.root,
130134
AMPLITY_HOSTING_COMPUTE_DEFAULT_DIR,
131135
);
132136
await mkdir(computeDefaultDir, { recursive: true });
133137
const dir = options.dir ?? "";
134-
await cp(dir, computeDefaultDir, { recursive: true });
138+
await copyFile(
139+
path.join(dir, "server.mjs"),
140+
path.join(computeDefaultDir, "server.mjs"),
141+
);
135142
// write deploy-manifest.json
136143
const reactRouterVersion = await getPackageVersion(
137144
"react-router",

packages/vite-plugin-react-router-amplify-hosting/vitest.config.integration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default defineProject({
55
test: {
66
name: `${pkg.name}-integration`,
77
include: ["integration/**/*.test.ts"],
8-
testTimeout: 120_000,
8+
testTimeout: 180_000,
99
exclude: [
1010
"**/node_modules/**",
1111
"**/dist/**",

packages/vite-plugin-react-router-amplify-hosting/vitest.config.unit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ export default defineProject({
55
test: {
66
name: `${pkg.name}-unit`,
77
include: ["**/*.test.ts"],
8-
exclude: ["integration/**"],
8+
exclude: ["integration/**", "node_modules/**", ".tmp/**"],
99
},
1010
});

0 commit comments

Comments
 (0)