Skip to content

Commit cb56661

Browse files
fix(federated-css-react-ssr): prewarm server remotes before starting shells to avoid Node Federation remote init race (3001–3007).
1 parent 09075b8 commit cb56661

File tree

6 files changed

+116
-6
lines changed

6 files changed

+116
-6
lines changed

federated-css-react-ssr/shell-apps/css-jss/server/index.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
11
const express = require('express');
22
const initMiddleware = require('./middleware');
3+
const fetch = require('node-fetch');
34

45
const app = express();
56
const PORT = 4000;
67

7-
const done = () => {
8+
async function waitUrl(url, timeout = 120000) {
9+
const start = Date.now();
10+
// Retry until the remoteEntry.js is available over HTTP
11+
/* eslint-disable no-await-in-loop */
12+
while (true) {
13+
try {
14+
const res = await fetch(url);
15+
if (res.ok) return;
16+
} catch (_) {}
17+
18+
if (Date.now() - start > timeout) {
19+
throw new Error(`prewarm timeout for ${url}`);
20+
}
21+
await new Promise(r => setTimeout(r, 1000));
22+
}
23+
}
24+
25+
const done = async () => {
26+
// Ensure remotes are reachable before the first SSR render
27+
await Promise.all([
28+
waitUrl('http://localhost:3001/server/remoteEntry.js'),
29+
waitUrl('http://localhost:3002/server/remoteEntry.js'),
30+
]);
31+
832
app.listen(PORT, () => {
933
console.info(
1034
`[${new Date().toISOString()}]`,

federated-css-react-ssr/shell-apps/css-scss/server/index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
const express = require('express');
22
const initMiddleware = require('./middleware');
3+
const fetch = require('node-fetch');
34

45
const app = express();
56
const PORT = 4001;
67

7-
const done = () => {
8+
async function waitUrl(url, timeout = 120000) {
9+
const start = Date.now();
10+
while (true) {
11+
try {
12+
const res = await fetch(url);
13+
if (res.ok) return;
14+
} catch (_) {}
15+
if (Date.now() - start > timeout) throw new Error(`prewarm timeout for ${url}`);
16+
await new Promise(r => setTimeout(r, 1000));
17+
}
18+
}
19+
20+
const done = async () => {
21+
await Promise.all([
22+
waitUrl('http://localhost:3001/server/remoteEntry.js'),
23+
waitUrl('http://localhost:3004/server/remoteEntry.js'),
24+
]);
825
app.listen(PORT, () => {
926
console.info(
1027
`[${new Date().toISOString()}]`,

federated-css-react-ssr/shell-apps/jss-styled-components-css-module/server/index.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
const express = require('express');
22
const initMiddleware = require('./middleware');
3+
const fetch = require('node-fetch');
34

45
const app = express();
56
const PORT = 4003;
67

7-
const done = () => {
8+
async function waitUrl(url, timeout = 120000) {
9+
const start = Date.now();
10+
while (true) {
11+
try {
12+
const res = await fetch(url);
13+
if (res.ok) return;
14+
} catch (_) {}
15+
if (Date.now() - start > timeout) throw new Error(`prewarm timeout for ${url}`);
16+
await new Promise(r => setTimeout(r, 1000));
17+
}
18+
}
19+
20+
const done = async () => {
21+
await Promise.all([
22+
waitUrl('http://localhost:3005/server/remoteEntry.js'),
23+
waitUrl('http://localhost:3002/server/remoteEntry.js'),
24+
waitUrl('http://localhost:3006/server/remoteEntry.js'),
25+
]);
826
app.listen(PORT, () => {
927
console.info(
1028
`[${new Date().toISOString()}]`,

federated-css-react-ssr/shell-apps/jss-styled-components/server/index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
const express = require('express');
22
const initMiddleware = require('./middleware');
3+
const fetch = require('node-fetch');
34

45
const app = express();
56
const PORT = 4002;
67

7-
const done = () => {
8+
async function waitUrl(url, timeout = 120000) {
9+
const start = Date.now();
10+
while (true) {
11+
try {
12+
const res = await fetch(url);
13+
if (res.ok) return;
14+
} catch (_) {}
15+
if (Date.now() - start > timeout) throw new Error(`prewarm timeout for ${url}`);
16+
await new Promise(r => setTimeout(r, 1000));
17+
}
18+
}
19+
20+
const done = async () => {
21+
await Promise.all([
22+
waitUrl('http://localhost:3005/server/remoteEntry.js'),
23+
waitUrl('http://localhost:3002/server/remoteEntry.js'),
24+
]);
825
app.listen(PORT, () => {
926
console.info(
1027
`[${new Date().toISOString()}]`,

federated-css-react-ssr/shell-apps/less-scss/server/index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
const express = require('express');
22
const initMiddleware = require('./middleware');
3+
const fetch = require('node-fetch');
34

45
const app = express();
56
const PORT = 4004;
67

7-
const done = () => {
8+
async function waitUrl(url, timeout = 120000) {
9+
const start = Date.now();
10+
while (true) {
11+
try {
12+
const res = await fetch(url);
13+
if (res.ok) return;
14+
} catch (_) {}
15+
if (Date.now() - start > timeout) throw new Error(`prewarm timeout for ${url}`);
16+
await new Promise(r => setTimeout(r, 1000));
17+
}
18+
}
19+
20+
const done = async () => {
21+
await Promise.all([
22+
waitUrl('http://localhost:3007/server/remoteEntry.js'),
23+
waitUrl('http://localhost:3004/server/remoteEntry.js'),
24+
]);
825
app.listen(PORT, () => {
926
console.info(
1027
`[${new Date().toISOString()}]`,

federated-css-react-ssr/shell-apps/scss-tailwind-css/server/index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
const express = require('express');
22
const initMiddleware = require('./middleware');
3+
const fetch = require('node-fetch');
34

45
const app = express();
56
const PORT = 4005;
67

7-
const done = () => {
8+
async function waitUrl(url, timeout = 120000) {
9+
const start = Date.now();
10+
while (true) {
11+
try {
12+
const res = await fetch(url);
13+
if (res.ok) return;
14+
} catch (_) {}
15+
if (Date.now() - start > timeout) throw new Error(`prewarm timeout for ${url}`);
16+
await new Promise(r => setTimeout(r, 1000));
17+
}
18+
}
19+
20+
const done = async () => {
21+
await Promise.all([
22+
waitUrl('http://localhost:3003/server/remoteEntry.js'),
23+
waitUrl('http://localhost:3004/server/remoteEntry.js'),
24+
]);
825
app.listen(PORT, () => {
926
console.info(
1027
`[${new Date().toISOString()}]`,

0 commit comments

Comments
 (0)